javascript - 根据允许的值/字符串数组验证字符串
问题描述
在Yup
中,是否有任何内置检查来根据允许值数组验证字符串?还是我必须为此编写自己的测试函数?
const fruit = 'apple';
const allowedFruits = ['orange', 'cherry'];
// I want to check if fruit is one of the allowedFruits. Fruit is coming from a text field / user input.
更新:
我想强调这个问题是关于Yup
验证库的,而不是如何用纯 JavaScript 来做。
解决方案
尝试使用:
const fruit = 'apple';
const fruit1 = 'orange';
const allowedFruits = ['orange', 'cherry'];
console.log(allowedFruits.includes(fruit));
console.log(allowedFruits.includes(fruit1));
推荐阅读
- reactjs - reactjs中如何获取并显示上传文件的日期?
- wordpress - apache 上的 nginx proxy_pass wordpress 与两个分离的服务器
- javascript - 从外部触发时无法更新 React 组件状态
- python - 使用 Python 和 ViewState 进行抓取
- google-sheets - 基于具有多个值的单元格过滤表列
- haskell - 在 Haskell 中打印反斜杠和双引号 ('\"')
- json - JsonUnsupportedObjectError(将对象转换为可编码对象失败:'CustomContact' 的实例) - 尝试了一切
- javascript - 如何使用 html2canvas 前后拍摄画布
- python - 使用python在postgres中将具有混合数据类型的列保存为str和jsonb
- vb.net - 如何检测另一个应用程序的窗口是否打开?