Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow any other key in Joi [duplicate]

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.

like image 254
Anand Undavia Avatar asked Apr 18 '18 10:04

Anand Undavia


People also ask

How do I allow an empty string in Joi validation?

Joi. string(). allow(''). allow(null) should have worked.

How do I change the default value in Joi schema?

Setting Default Valuesconst schema = Joi. object({ forename: Joi. string(). required(), middleName: Joi.


1 Answers

The correct answer is actually to use object.unknown(true).

const schema = Joi.object().keys({   a: Joi.string().required(),   b: Joi.string().required() }).unknown(true); 
like image 187
Niels Keurentjes Avatar answered Oct 05 '22 17:10

Niels Keurentjes