Skip to content

Instantly share code, notes, and snippets.

@sajclarke
Created November 16, 2022 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sajclarke/91ab53c830b04555508bf5775c5d2811 to your computer and use it in GitHub Desktop.
Save sajclarke/91ab53c830b04555508bf5775c5d2811 to your computer and use it in GitHub Desktop.
Disable yup validation based on external variable
//external variable
const disabledEndYear = true
//schema validation for yup
const schema = yup
.object({
field_name: yup.number().when([], {
is: (val: any) => {
return !disabledEndYear; //if false
},
then: yup
.number()
.label("Field name")
.typeError("Field must be a number")
.transform((value) => (isNaN(value) ? undefined : value))
.required(),
otherwise: yup
.number()
.transform((value) => (isNaN(value) ? undefined : value))
.nullable(),
}),
})
.required();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment