来自 Yup 自定义验证器的消息 returns 未定义路径和引用的名称
Message from Yup custom validator returns undefined for the names of path and reference
我正在尝试为 yup 验证库编写一个自定义测试,用于 node/express 应用程序,测试两个字段是否相同 - 用例例如测试密码和确认密码字段是否匹配。逻辑有效,但方法提供的消息无效。
自定义验证器代码修改自:https://github.com/jquense/yup/issues/97#issuecomment-306547261
自定义验证器
yup.addMethod(yup.string, 'isMatch', function (ref, msg) {
return this.test({
name: 'isMatch',
message: msg || `${this.path} must be equal to ${this.reference}`,
params: {
reference: ref.path
},
test: function (value) {
return value === this.resolve(ref);
}
});
});
示例使用
const schema = yup.object().shape({
password: yup.string().min(8),
passwordConfirm: yup.string().isMatch(yup.ref('password'))
})
const payload = {
password: 'correctPassword',
passwordConfirm: 'incorrectPassword'
}
schema.validate(payload)
上述方法从逻辑的角度来看是符合预期的。但是在返回的错误信息中,this.path
和this.reference
的值都是undefined
(即undefined must be equal to undefined
)。它应该显示为 passwordConfirm must be equal to password
.
我必须在 path
和 reference
前面添加 this.
,否则节点会崩溃并出现 path/reference is not defined
.
的 ReferenceError
您无需编写自定义验证函数来检查两个字段是否匹配,您可以使用内置验证器.oneOf
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.
看这里ref: oneOf(arrayOfValues: Array, message?: string | function): Schema Alias: equals
因此您可以按如下方式重写您的架构(我在密码字段中也添加了 required):
const schema = yup.object().shape({
password: yup.string().min(8).required('Required!'),
passwordConfirm: yup.string().oneOf([Yup.ref('password')], 'Password must be the same!').required('Required!')
})
我正在尝试为 yup 验证库编写一个自定义测试,用于 node/express 应用程序,测试两个字段是否相同 - 用例例如测试密码和确认密码字段是否匹配。逻辑有效,但方法提供的消息无效。
自定义验证器代码修改自:https://github.com/jquense/yup/issues/97#issuecomment-306547261
自定义验证器
yup.addMethod(yup.string, 'isMatch', function (ref, msg) {
return this.test({
name: 'isMatch',
message: msg || `${this.path} must be equal to ${this.reference}`,
params: {
reference: ref.path
},
test: function (value) {
return value === this.resolve(ref);
}
});
});
示例使用
const schema = yup.object().shape({
password: yup.string().min(8),
passwordConfirm: yup.string().isMatch(yup.ref('password'))
})
const payload = {
password: 'correctPassword',
passwordConfirm: 'incorrectPassword'
}
schema.validate(payload)
上述方法从逻辑的角度来看是符合预期的。但是在返回的错误信息中,this.path
和this.reference
的值都是undefined
(即undefined must be equal to undefined
)。它应该显示为 passwordConfirm must be equal to password
.
我必须在 path
和 reference
前面添加 this.
,否则节点会崩溃并出现 path/reference is not defined
.
您无需编写自定义验证函数来检查两个字段是否匹配,您可以使用内置验证器.oneOf
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.
看这里ref: oneOf(arrayOfValues: Array, message?: string | function): Schema Alias: equals
因此您可以按如下方式重写您的架构(我在密码字段中也添加了 required):
const schema = yup.object().shape({
password: yup.string().min(8).required('Required!'),
passwordConfirm: yup.string().oneOf([Yup.ref('password')], 'Password must be the same!').required('Required!')
})