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

297
Views
How to update a schema object, after creation, inside a Joi object?

Wasn't sure the best way to phrase the question, but here is an example of what I am trying to do.

const schema = Joi.Object().keys({
   array: Joi.array().length(5)
});

Then after creation I am wanting to update the length method to be a different number.

this.schema.keys({ array: Joi.array().length(8) });

The above code does not work and I'm really just not sure what else to try. (This isn't the only thing I have tried, just where I'm currently at) I've been looking through documentation but haven't found anything helpful for updating a schema. Maybe someone knows of a way to do it?

Any help would be much appreciated!

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

0

const Joi = require('joi');

////
// update schema itself.
////
let scheme = Joi.object().keys({
  foo: Joi.array().length(5),
});

const a1 = scheme.validate({ foo: [1,2,3,4,5] });
console.log(a1.error); // undefined. valid.

scheme = scheme.keys({
  foo: Joi.array().length(8),
});

const a2 = scheme.validate({ foo: [1,2,3,4,5] });
console.log(a2.error); // array.length error occurred.

const a3 = scheme.validate({ foo: [1,2,3,4,5,6,7,8] });
console.log(a3.error); // undefined. vaild.

////
// using base scheme.
////
const base = Joi.object().keys({
  foo: Joi.array(),
});

const base_a = base.keys({
  foo: Joi.array().length(5),
});
console.log('Using base a1', (base_a.validate({ foo: [1,2,3,4,5]})).error);

const base_b = base.keys({
  foo: Joi.array().length(8),
});
console.log('Using base a2', (base_b.validate({ foo: [1,2,3,4,5]})).error);
console.log('Using base a3', (base_b.validate({ foo: [1,2,3,4,5,6,7,8]})).error);

////
// Not good but works.
////
const obj = Joi.object({
  foo: Joi.array().length(5),
});
console.log('Obj a1', (obj.validate({ foo: [1,2,3,4,5]})).error);

const merged = Object.assign(obj, Joi.object({
  foo: Joi.array().length(8),
}));

console.log('Obj a2', (merged.validate({ foo: [1,2,3,4,5]})).error);
console.log('Obj a3', (merged.validate({ foo: [1,2,3,4,5,6,7,8]})).error);
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!