Created
November 16, 2022 23:29
-
-
Save sajclarke/91ab53c830b04555508bf5775c5d2811 to your computer and use it in GitHub Desktop.
Disable yup validation based on external variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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