Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
Is there's a way to validate unknown keys with JOI on nested payloads?

I have a middleware that validates a incoming request using Joi.

export default (schema: any) => async (req: Request, res: Response, next: NextFunction) => {
  try {
    const validation = schema.validate(req, { abortEarly: false })

    if (validation.error) throw validation.error

    return next()
  } catch (error) {
    return res.status(400).json({ error: error.details })
  }
}

For testing, propouse, my schema is as follow

const schema = Joi.object({
  headers: Joi.object()
    .keys({
      page: Joi.string().required()
    })
    .unknown(true)
    .label('headers'),
  body: Joi.object({
    page: Joi.string().required()
  })
})

I'm testing this function with Postman. When I do not send the header it fails but show this following errors:

  • 'user-agent': 'PostmanRuntime/7.29.0'
  • accept: '/',
  • 'accept-encoding': 'gzip, deflate, br',
  • connection: 'keep-alive'

I want to be able to validate only the 'page' field on the schema, and ignore the anyn other field above.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Change your validation and add stripUnknown option to true:

const validation = schema.validate(req, { abortEarly: false, stripUnknown:true })
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda