最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is number().min() not working in Yup validation? - Stack Overflow

programmeradmin2浏览0评论

Here is my Yup schema. The validation on asset works fine, but I'm not sure why it is not working on the amount?

  const validationSchema = Yup.object().shape({
      asset: Yup.string().required("Required!"),
      amount: Yup.number()
        .required()
        .min(5, "Must be more than 5")
        .positive(),
    });

Here is my Yup schema. The validation on asset works fine, but I'm not sure why it is not working on the amount?

  const validationSchema = Yup.object().shape({
      asset: Yup.string().required("Required!"),
      amount: Yup.number()
        .required()
        .min(5, "Must be more than 5")
        .positive(),
    });
Share Improve this question edited Oct 4, 2022 at 16:47 Talha Jutt 563 silver badges13 bronze badges asked Nov 21, 2021 at 18:40 ClancinioClancinio 9427 gold badges27 silver badges45 bronze badges 2
  • 3 You need to drop positive() in order for it work. – Hassan Imam Commented Nov 21, 2021 at 18:57
  • Ahhhhhh thank you very much :) – Clancinio Commented Nov 21, 2021 at 19:10
Add a ment  | 

2 Answers 2

Reset to default 1

You may add number.min(1, '') & number.max(1,''). To specify the minimum and maximum quantity also

<Typography color="error.main">
   { errors?.bonus_add }
</Typography>

...

 bonus_add: yup.number()
    .test('numbers', 'Допустимы только цифры', value => !value || /^[0-9]+$/.test(value))
    .test('lessThanTen', 'Поле “Количество бонусов” должно содержать не более 9 символов', value => !value || value.toString().length < 10)
    .required('Обязательное поле'),
发布评论

评论列表(0)

  1. 暂无评论