Deo*_*s_0 4 javascript reactjs yup
我在更新版本 16 到 18 中编写的一些旧 ReactJs 代码时遇到困难,同时 Yup 包也被更新(0.26.6 -> 1.2.0),因为它的一些语法规则似乎已更改,并且我遇到奇怪的错误,我无法诊断和修复。
import helpers from '../../../Shared/validationHelpers';
const { domainName } = helpers.regEx;
export default Yup.object({
enabled: Yup.boolean(),
hostname: Yup.string().when('enabled', {
is: true,
then: Yup.string()
.matches(domainName, 'Please provide a fully qualified domain name')
.required('You must provide a hostname'),
otherwise: Yup.string().notRequired(),
}),
});
Run Code Online (Sandbox Code Playgroud)
Helpers 只是一个带有一堆正则表达式定义的文件,domainName 是用于设置域名的正则表达式。
错误发生在“is: true”上:
No overload matches this call.
Overload 1 of 4, '(keys: string | string[], builder: ConditionBuilder<StringSchema<string, AnyObject, undefined, "">>): StringSchema<string, AnyObject, undefined, "">', gave the following error.
Argument of type '{ is: boolean; then: Yup.StringSchema<string, Yup.AnyObject, undefined, "">; otherwise: Yup.StringSchema<string, Yup.AnyObject, undefined, "">; }' is not assignable to parameter of type 'ConditionBuilder<StringSchema<string, AnyObject, undefined, "">>'.
Object literal may only specify known properties, and 'is' does not exist in type 'ConditionBuilder<StringSchema<string, AnyObject, undefined, "">>'.
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢。
小智 5
由于我无法对给定的回复进行投票,我会说这工作正常。顺便说一句,这是使用字符串模式的示例:
companion_name: yup
.string()
.when('marital_status', ([marital_status], sch) => {
return ['C', 'D', 'E'].indexOf(marital_status) > -1
? sch
.trim()
.min(2)
.required()
: sch.notRequired();
}),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3414 次 |
| 最近记录: |