I am validating a field that can only take a positive decimal number. My Yup schema looks as follow:
yup.object().shape({
Duration: yup
.number()
.typeError('Please enter a duration. The field cannot be left blank.')
.positive('Must be a positive number.'),
})
As a result,
I could change the typeError() message for something more generic but I was wondering if there was a more comprehensive way to handle the scenario.
So, when the user enters
Here's my take on a more comprehensive way to handle the given scenario. We will use the required and minimum schemas.
yup.object().shape({
Duration: yup
.number("Must be a number type") // Validates for numerical value
.positive("Must be a positive value") // Validates against negative values
.required("Please enter a duration. The field cannot be left blank.") // Sets it as a compulsory field
.min(1, "Hey! Your duration must be greater than or equal to 1!"), // Sets a minimum value});
});
These newly added schemas help to:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With