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

116
Views
Joi validate specific fields based on larger schema

Say I have the following schema

const schema = Joi.object({
  title: Joi.string().required(),
  start_date: Joi.date().iso().required(),
  end_date: Joi.date().iso().greater(Joi.ref('start_date')).required()
});

In one occasion I'd like to validate the entire schema against an object

const obj = {
  title: 'abc',
  start_date: '2021-11-09',
  end_date: '2021-11-10'
}
const validationResult = schema.validate(obj);

And on the other I'd like to validate against part of the schema with my partial object (I don't want to be alarmed about a missing title, for instance)

const obj = {
  start_date: '2021-11-09',
  end_date: '2021-11-11'
}
const validationResult = schema/*sudo*/.onlyFields(['start_date', 'end_date']).validate(obj);

Is it possible? Workaround is having 2 schemas - titleSchema and dateSchema and concatinating

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Solved using the schema.extract api

function joiSubSchema(base, fields) {
  return fields.reduce((schema, field) => {
    return schema.concat(Joi.object({
      [field]: base.extract(field)
    }));
  }, Joi.object());
}
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!