How can I validate an event using yup validatesync?
The input is an object that contains another object with 3 fields. The first field is required, and either field 2 or 3 should be present in the input, or use the default value.
In the code snippet below, the default value is not being used when the field is empty in the input.
For example: if field 2 is present, then field 3 is optional in the input but it should still use the default value from the schema.
obj = { "node": { "node1": { "field1": "value1", "field2": "value2", "field3": "value3" } } }
validate = () => yup.object({
node1: yup.object({
node2: yup.object().shape({
field1: yup.string().required(),
field2: yup.string().when('field3', {
is: (field3: string) => field3.length === 0,
then: yup.string().required(),
otherwise: yup.string().default('my default1'),
}),
field3: yup.string().when('field2', {
is: (field2: string) => !field2 || field2.length === 0,
then: yup.string().required(message),
otherwise: yup.string().default('my default2'),
}),
},
[ [ 'field2', 'field3' ] ]
).required(),
}), })