javascript - 仅正数 - formik/yup 模式
问题描述
我做了这样一个架构:
const schema = yup.object().shape({
seats: yup
.number()
.label('seats')
.required('pls enter'),
});
此外,我想检查该数字是正数还是大于 0。有什么方法可以将这样的条件添加到架构中?
解决方案
您可以使用该test()
方法并在那里添加自定义验证:
number: Yup.number()
.required('ERROR: The number is required!')
.test(
'Is positive?',
'ERROR: The number must be greater than 0!',
(value) => value > 0
)
https://github.com/jquense/yup#mixedtestname-string-message-string--function-test-function-schema
推荐阅读
- php - 如何在主页中添加与博客页面相同的帖子,我为每个页面选择静态页面?
- python - 如何为我自己的用户定义类覆盖用于可变参数函数中的 kwargs 的 `**` 运算符?
- android - 无法从 ImageView 获取位图作为 BitmapDrawable
- android - 从 ROM 获取设备驱动程序
- vue.js - 如何使用 eslint 在所有导入中强制 .vue 扩展?
- google-cloud-platform - 是否可以在不删除的情况下停止 Google Cloud 上的 Kubeflow?
- gnuplot - 我想用整数参数拟合一个函数
- php - 如何通过获取唯一 ID/表 ID 以及数据 CODEIGNITER 来编辑数据库中的列
- mongoose - 猫鼬找到带有 id 的子文档,CoreMongooseArray vs DocumentArray
- php - PHP 命令行:以命令、子命令和参数的形式解析参数的有效方法