ozzieisaacs
ozzieisaacs

Reputation: 843

Joi - Mix of known and unknown keys

I am trying to create a joi schema where I have a bunch of known and unknown keys.

{
  dogname: 'doggo', 
  catname: 'attack',
  dogage: 51,
  catage: 98,
  key51: '',
  key73: '',
  key47: ''
}

Basically the first 4 keys are always there but the last keys are key with a suffix of some random number 0-100. I know you can do a regex pattern but I want full unique validation of the first 4 keys.

Upvotes: 0

Views: 806

Answers (1)

Ankh
Ankh

Reputation: 5738

I've assumed the actual validation for each field but this will validate both the defined fields and variable fields that fall into the pattern of key[0-100]: 'string'.

The key aspect of this to take away is the usage of .pattern().

Joi.object().keys({
    dogname: Joi.string(),
    catname: Joi.string(),
    dogage: Joi.number().integer().positive(),
    catage: Joi.number().integer().positive()
}).pattern(/^key[0]|[1-9][0-9]?|100/, Joi.string());

Upvotes: 1

Related Questions