I'm trying to validate a number field that is optional, and therefore allowed to be empty. If there is a value in the field, it must be a positive number.
const schema = yup.object().shape({
gpa: yup.number()
.when('gpa', {
is: (value) => value?.length > 0,
then: yup.number().positive(numberPositiveMessage).typeError(numberMessage),
otherwise: yup.number().notRequired().nullable(true).transform(value => (isNaN(value) ? undefined : value))
},
[
['gpa', 'gpa'],
]
);
It allows the rest of the form to validate when the field is empty, and when there is a positive number in there, but if I enter a negative number or a string it does not return any errors like it should.