我有一个我认为是简单的验证,但oneOf它对我来说不起作用:是的:
const schema = Yup.mixed()
.oneOf([
{
error: `EmailOrPasswordInvalid`,
},
{},
])
.required();
Run Code Online (Sandbox Code Playgroud)
... 之后...
const isValid = schema.isValidSync({ error: `EmailOrPasswordInvalid` });
console.log(isValid); // false
Run Code Online (Sandbox Code Playgroud)
我确信我错过了一些简单的东西,但无法指出它。谢谢!
小智 7
我相信您可能误解了 oneOf 函数的工作原理。
以下是从 Grepper 帖子中提取的示例(归功于 Fustinato):
// mixed.oneOf(arrayOfValues: Array<any>, message?: string | function): Schema Alias: equals
// Whitelist a set of values. Values added are automatically removed from any blacklist if they are in it. The ${values} interpolation can be used in the message argument.
// Note that undefined does not fail this validator, even when undefined is not included in arrayOfValues. If you don't want undefined to be a valid value, you can use mixed.required.
let schema = yup.mixed().oneOf(['jimmy', 42]);
await schema.isValid(42); // => true
await schema.isValid('jimmy'); // => true
await schema.isValid(new Date()); // => false
Run Code Online (Sandbox Code Playgroud)
在进一步的响应和查找中,似乎您想要做的事情可能是不可能的?源码:https: //github.com/jquense/yup/issues/1393
您是否可以只解构错误并将其作为字符串传递,然后在 oneOf 函数中检查该字符串?
| 归档时间: |
|
| 查看次数: |
23448 次 |
| 最近记录: |