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

1.4K
Views
Allow only specific values for key in Joi schema

Is there any other way to set specific values in Joi validation schema for key except regex pattern?

My example schema:

const schema = joi.object().keys({
    query: joi.object().keys({
        // allow only apple and banana
        id: joi.string().regex(/^(apple|banana)$/).required(),
    }).required(),
})
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can also use valid like

const schema = joi.object().keys({
  query: joi.object().keys({
    // allow only apple and banana
    id: joi.string().valid('apple','banana').required(),
  }).required(),
})

Reference: https://github.com/hapijs/joi/blob/v13.1.2/API.md#anyvalidvalue---aliases-only-equal

over 4 years ago · Santiago Trujillo Report

0

If you want to accept one or more values (apple, banana or both) you can use allow: https://joi.dev/api/?v=17.3.0#anyallowvalues

over 4 years ago · Santiago Trujillo Report

0

You can try with .valid() to allow joi only with that specific string .

const schema = joi.object().keys({
  query: joi.object().keys({
    role: joi.string().valid('admin','student').required(), // joi would allow only if role is admin or student.
  }).required(),
})

This is absolutely equal to giving "enam" field in mongoose ODM for representing objects in mongodb.

over 4 years ago · Santiago Trujillo 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!