How to adjust the schema of a child based on a siblings child with `yup`?

I have the following yup schema:

const schema = y.object({
  foo: y.object({
    label: y.string().required(),
    value: y.boolean().required(),
  }),
  bar: y.object({
    label: y.string().required(),
    value: y.number().positive())
  }),
});

How can I make bar.value required when foo.value is true, and bar.value not required and stripped away when foo.value is false?


I’ve found I can use when with dot-notation, but I have to use it on bar, not bar.value, to access the sibling foo, and I don’t know how to adjust a “sub-schema”, and haven’t been able to find any examples of this from searching or in the yup docs either.

const schema = y.object({
  foo: y.object({
    label: y.string().required(),
    value: y.boolean().required(),
  }),
  bar: y.object({
    label: y.string().required(),
    value: y.number().positive())
  }).when('foo.value', {
    is: true,
    /**
     * Here `schema` is the object-schema for `bar`, but how do I
     * adjust/change one of its "child"-schemas, in this case the `value` one?
     */
    then: (schema) => schema.???.required(),
    otherwise: (schema) => schema.???.notRequired().strip(),
  }),
});