Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!