jquense / yup

Dead simple Object schema validation
MIT License
22.72k stars 925 forks source link

number() along with max() method does remove error even after correcting the error #2209

Closed vishal-gits closed 4 months ago

vishal-gits commented 4 months ago

phone: yup.number().max(10, "Max 10 dijits"),

this is a part of a validation schema used in react native in TextInput

export const validationSchema = yup.object({
  firstName: yup.string().required("Required").max(15, "Max 15 characters"),
  lastName: yup.string().required("Required").max(15, "Max 15 characters"),
  AddressLine1: yup.string().required("Required"),
  AddressLine2: yup.string(),
  city: yup.string().required("Required").max(15, "Max 15 characters"),
  province: yup.string().max(15, "Max 25 characters"),
  postalCode: yup
    .number()
    .required("Required")
    .max(6, "max6 digits")
    .typeError("Provide in postalcode number format"),
  phone: yup.number().max(10, "Max 10 dijits"),
  company: yup.string().max(15, "Max 25 characters"),
  email: yup.string().email().required(),
});

Now when phone number is entered in numbers, whether it is less than or more than 10 , the validator gives an error- "Max 10 digits" and it does not remove the error , even after correcting it. Same is with postalCode.

Also if typeError() is given to phone(which is not specified required), and when nothing is entered for phone, then also it attaches a typeError to it. Ideally if nothing is entered and it is not specified "required", it should not give an error

jquense commented 4 months ago

max is the number amount, not the number of digits. max(10) means the number can't be greater than 10

vishal-gits commented 4 months ago

Ok thanks, in case of strings, it represents the number of characters, but in case of numbers, it is the number amount. So is there a method available in yup to limit the number of integers in the input box.

What could be a solution to restrict for an input field of postalcode, so that it is a number of 5/6 digits.

Another Issue I am facing is -----(kindly confirm, if seperate issue needs to be raised for this) Also if typeError() is given to phone(which is not specified required), and when nothing is entered for phone, then also it attaches a typeError to it. Ideally if nothing is entered and it is not specified "required", it should not give an error.