Search code examples
javascriptreactjsyup

How can I inspect or query a Yup Schema?


I am creating a form library using React + Material UI + react-hook-form + yup. One of the parameters of my component is a yup schema to validate the form values.

Depending on the yup schema definition, I would like to draw the form components.

const schema = yup.object({
  example: yup.string(),
  exampleRequired: yup.string().required(),
};

How can I query the schema to know which field is required? Is it possible?


Solution

  • check out the describe() method in yup schemas. You should be able to look up the necessary metadata using schema.describe().fields['exampleRequired']. More information here: https://github.com/jquense/yup#schemadescribeoptions-resolveoptions-schemadescription