I have a field that should only accept integers because my backend can't accept float type. When using Yup.number().integer()
to achieve this, it works fine for whole numbers like 2, 3, 100, etc. However, when I enter a value like 1.0, Yup treats it as an integer instead of a decimal and does not throw a validation error.
Can we configure Yup.number().integer()
to consider 1.0 as a decimal? Or is there a different approach I should take to validate integer values in Yup? Any insights or suggestions would be greatly appreciated. Thank you!
I have tried to use test() with Number.isInteger, but it's not working
const integerValidation = (value) => {
if (!Number.isInteger(value)) {
return false;
}
return true;
};
const schema = Yup.object().shape({
yourIntegerField: Yup.number().test('integer', 'Please enter an integer value', integerValidation),
});