Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
14
rated 0 times [  16] [ 2]  / answers: 1 / hits: 39908  / 6 Years ago, wed, april 18, 2018, 12:00:00

I have a simple requirement. I tried to search on the internet as well as documentation but failed.

So here is what I want to achieve:



I have a schema:



const schema = Joi.object().keys({
a: Joi.string().required(),
b: Joi.string().required()
});


Now, How do I configure it such that any other key in the object would be allowed?


With this schema, it only allows two keys a and b. If I pass any other key, say, c, it throws an error saying that c is not allowed.


More From » hapi.js

 Answers
14

You can add unknown keys using object.pattern(regex, schema) this way IF you want to make sure these unknown keys are strings:





const schema = Joi.object().keys({
a: Joi.string().required(),
b: Joi.string().required()
}).pattern(/./, Joi.string());





For a general pass of all key types use object.unknown(true):



const schema = Joi.object().keys({
a: Joi.string().required(),
b: Joi.string().required()
}).unknown(true);

[#54629] Sunday, April 15, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kinsley

Total Points: 352
Total Questions: 84
Total Answers: 94

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;