我有一个使用 zod 的应用程序,但我想使用不同库(validator.js)中的一些方法,zod 文档说:
查看 validator.js 以获得许多其他有用的字符串验证函数。
不确定这是否意味着这个功能是在 zod 上实现的,或者我还必须安装 validator.js,在其他情况下我如何一起使用这两个库?找不到任何例子。
谢谢!
我正在使用猫鼬4.9.0.虽然下面的代码有效,但我收到如下警告:
(node:24769)DeprecationWarning:在mongoose> = 4.9.0中不推荐使用隐式异步自定义验证器(带有2个参数的自定义验证器).有关详细信息,请参阅http://mongoosejs.com/docs/validation.html#async-custom-validators.
我怀疑错误来自模型验证器.
const mongoose = require('mongoose');
const isEmail = require('validator/lib/isEmail');
const Schema = mongoose.Schema;
const userSchema = new Schema({
email: {
type: String,
unique: true,
required: true,
validate: [{ validator: isEmail, msg: 'Invalid email.' }],
},
});
module.exports = mongoose.model('User', userSchema);
Run Code Online (Sandbox Code Playgroud)
我似乎唯一的自定义验证器isEmail来自validator库,它给出一个字符串值返回它是否有效.
我有一个表格,如果一个人的送货地址与其帐单地址不同,他们将选择一个单选按钮说“不”。
我需要能够检查 this 的值是什么,如果它是“否”,请检查展开的表单。如果是“是”,我会跳到下面的下一个验证。“是”部分正在工作,但“否”部分总是返回无效。有没有办法用这个库在这样的条件中嵌套验证?
这是表格的屏幕截图:
这是我针对这种情况的验证代码:
router.post('/testData2/', [
body('billingSameAsShipping', "Please Fill All Fields Of Your Billing Address").custom((value) => {
if (value === 'no') {
[
body('billingFirstName', "Please Enter Your First Name").isLength({
min: 1
}),
body('billingLastName', "Please Enter Your Last Name").isLength({
min: 1
}),
body('billingAddress1', "Please Enter Your Billing Address").isLength({
min: 1
}),
body('billingZip', "Please Enter Your Billing ZipCode").isLength({
min: 1
}),
body('billingCity', "Please Enter Your Billing City").isLength({
min: 1
}),
body('billingState', "Please Enter Your Billing State").isLength({
min: 1
})
] …Run Code Online (Sandbox Code Playgroud)