joi nested schema 1

joi nested schema

const schemaA = Joi.string()
const schemaB = Joi.object({ keyB1: schemaA, keyB2: Joi.number() })
const schemaC = Joi.object({
  keyC1: Joi.string(),
  keyC2: schemaB  
})

Joi.validate({ keyC1: 'joi', keyC2: { keyB1: 'rocks!', keyB2: 3 } }, schemaC)

Here is what the above code is Doing:
1. We define a schemaA that is a string.
2. We define a schemaB that is an object with two keys: keyB1 and keyB2.
3. We define a schemaC that is an object with two keys: keyC1 and keyC2.
4. We validate an object against schemaC.

The validation passes because the object we’re validating has the same structure as schemaC.

The keyC2 property of the object we’re validating is an object with two keys: keyB1 and keyB2.
This matches the structure of schemaB.

The keyB1 property of the object we’re validating is a string.
This matches the structure of schemaA.

The keyB2 property of the object we’re validating is a number.
This matches the structure of Joi.number().

The keyC1 property of the object we’re validating is a string.
This matches the structure of Joi.string().

Similar Posts