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]