Accessing the "final" schema and extracting it's rules

Viewed 49

Let's say I have the following schema (one of the examples in Yup's documentation):

let schema = object({
  isSpecial: boolean(),
  isBig: boolean(),
  count: number().when(['isBig', 'isSpecial'], {
    is: true, // alternatively: (isBig, isSpecial) => isBig && isSpecial
    then: yup.number().min(5),
    otherwise: yup.number().min(0),
  }),
});

const result = schema.validateSync({
  isBig: true,
  isSpecial: true,
  count: 10,
});

The count's min value depends on the value of isBig. That value is decided at runtime, when calling validate(). Is there a way I can somehow access the "final" schema and programatically check what Yup decided to enforce?

Something like:

const { result, finalSchema } = schema.validateSync({
  isBig: true,
  isSpecial: true,
  count: 10,
});

console.log(finalSchema.count.min) // 5
0 Answers
Related