Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joi object validation: How to validate values with unknown key names?

I have an object with key names I cannot possibly know - they are created by user. However I do know what values they (keys) are going to store, and they (values) are going to be ISO strings. How do I validate those values? And, optionally, how do I validate uknown object's keys, i.e.:

 key: Joi.string().min(2).max(25) 

What I have already tried was based on Joi API docs :

Another benefits of using Joi.object([schema]) instead of a plain JS object is >that you can set any options on the object like allowing unknown keys, e.g:

const schema = Joi.object({ arg: Joi.string().valid('firstname', 'lastname', 'title', 'company', 'jobtitle'), value: Joi.string(), }).pattern(/firstname|lastname/, Joi.string().min(2));

What I understood from the example is that arg key represents Joi.object()'s key, and value represents it's value.

My example:

campaign: Joi.object({   arg: Joi.string().valid( 'unknown' ),   value: Joi.date().iso(), }).pattern( /unknown/, Joi.string().min(2).max(25) ) 

My input;

campaign: { g_ad_adwords: "2017-01-19T11:33:26.205Z" } 

My error:

"campaign" fails because ["g_ad_adwords" is not allowed]

like image 682
wscourge Avatar asked Jan 19 '17 12:01

wscourge


People also ask

Should I use Joi or express validator?

Joi can be used for creating schemas (just like we use mongoose for creating NoSQL schemas) and you can use it with plain Javascript objects. It's like a plug n play library and is easy to use. On the other hand, express-validator uses validator. js to validate expressjs routes, and it's mainly built for express.

What is Joi validator?

Hapi Joi is an object schema description language and validator for JavaScript objects. With Hapi Joi, we create blueprints or schemas for JavaScript objects (an object that stores information) to ensure validation of key information.


1 Answers

Try this. It'll basically accept any key within an object campaign and the value must validate against Joi.date().iso()

campaign: Joi.object().pattern(/^/, Joi.date().iso()) 

This however will match any key. You can restrict this by padding out the regex a little. e.g. only word characters between 2 and 25 chars

campaign: Joi.object().pattern(/\w{2,25}/, Joi.date().iso()) 

UPDATE

Regarding the example in the Joi docs, I haven't tested it but here's my interpretation. I can understand that it's not the most straightforward example they could have given...

const schema = Joi.object({     arg: Joi.string().valid('firstname', 'lastname', 'title', 'company', 'jobtitle'),     value: Joi.string(), }).pattern(/firstname|lastname/, Joi.string().min(2)); 

The objects to validate must contain the two attributes arg and valuewhere arg's value can be one of 'firstname', 'lastname', 'title', 'company', 'jobtitle' and value is just a string.

{     arg: 'firstname',     value: 'john' }  {     arg: 'lastname',     value: 'smith' }  {     arg: 'jobtitle',     value: 'brewer' } 

However it will also allow the object to have the attributes firstname and lastname where both of their values is a string with more than two characters. So the above examples could be condensed into a single valid object.

{     firstname: 'john',     lastname: 'smith',     arg: 'jobtitle',     value: 'brewer' } 
like image 165
Ankh Avatar answered Sep 21 '22 03:09

Ankh