jquense / yup

Dead simple Object schema validation
MIT License
22.94k stars 935 forks source link

Unable to access tests within Schema.when #2096

Closed aymaneEM closed 1 year ago

aymaneEM commented 1 year ago

Unable to access tests within Schema.when

As described in the title, I can't access the tests written inside a Schema.when function

In the example below

const validationSchema = () => yup.object().shape({
  normalField      : yup.string().required("Required field"),
  conditionalField : yup.string().when("normalField", {
    is        : true,
    then      : yup.string().matches(REGEX, "Regex not matched")).required("Required field")),
    otherwise : yup.string().notRequired()
  })
})

when I try to log the validationSchema, I can find the attribute deps: ["normalField"] on validationSchema.fields.conditionalField, but I can't find the tests matches and required anywhere in exclusiveTests or tests attributes.

Is there any way to access them?

jquense commented 1 year ago

you can use describe({...}) with arguments to get a resolved schema. Yup, don'es know what tests you might add or not add until it actually resovles one of the when branches. Also your example isn't valid in v1, make sure to double check the docs