Reputation: 55
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!
Upvotes: 2
Views: 1252
Reputation: 101
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);
Upvotes: 1