英文:
Mongoose - required validator not running by function
问题
以下是您要翻译的内容:
我将一个函数传递给了mongoose模式中的required验证器,但如果我不传递任何值给字段,它不会触发。
在文档中说:
验证器不会在未定义的值上运行。唯一的例外是required验证器。
参考链接:https://mongoosejs.com/docs/validation.html
如果我传递一个函数,它不会应用吗?
编辑 - 共享代码:
我的字段定义:
field: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Model',
required: !isBlock,
autopopulate: true
}
未触发的函数:
function isBlock() {
return this.type === 'block'
}
英文:
I'm passing a function to required validator in mongoose schema, but it's not firing if i don't pass any value to the field.
In documentation says the following:
> Validators are not run on undefined values. The only exception is the required validator.
Reference: https://mongoosejs.com/docs/validation.html
If I pass a function is it not applied?
EDIT - Sharing the code:
My field definition:
<!-- language: lang-js -->
field: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Model',
required: !isBlock,
autopopulate: true
}
<!-- end snippet -->
The function that isn't firing:
<!-- language: lang-js -->
function isBlock() {
return this.type === 'block'
}
答案1
得分: 1
问题出在函数调用中的'!'操作符上,将代码更改为下面这样,我解决了我的问题。
required: function () { return !isBlock(this.type) }
英文:
Debugging I saw that the problem was with the '!' operator in the function call, changing the code to the under I solved my problem.
required: function () { return !isBlock(this.type) }
答案2
得分: 0
以下是您提供的代码的中文翻译:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const cryptoRandomString = require('crypto-random-string');
const userSchema = new Schema({
firstName: {
trim: true,
type: String,
required: [true, "必须提供firstName!"],
validate(value) {
if (value.length < 2) {
throw new Error("firstName 无效!");
}
}
},
lastName: {
trim: true,
type: String,
required: [true, "必须提供lastName!"],
validate(value) {
if (value.length < 2) {
throw new Error("lastName 无效!");
}
}
},
email: {
unique: [true, "电子邮件已注册"],
type: String,
required: [true, "必须提供电子邮件"]
},
mobile: {
unique: [true, "手机号码已存在"],
type: String,
required: [true, "必须提供手机号码"],
validate(value) {
if (value.length !== 10) {
throw new Error("手机号码无效!");
}
}
},
password: {
type: String,
required: [true, "必须提供密码"],
validate(value) {
if (value.length < 6) {
throw new Error("密码必须至少为6个字符!");
}
}
}
});
请注意,我已将代码中的错误消息翻译成中文。
英文:
I am posting a reference code match with this otherwise Show your code.
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const cryptoRandomString = require('crypto-random-string');
const userSchema = new Schema({
firstName: {
trim: true,
type: String,
required: [true, "firstName is required!"],
validate(value) {
if (value.length < 2) {
throw new Error("firstName is invalid!");
}
}
},
lastName: {
trim: true,
type: String,
required: [true, "lastName is required!"],
validate(value) {
if (value.length < 2) {
throw new Error("lastName is invalid!");
}
}
},
email: {
unique: [true, "Email already registered"],
type: String,
required: [true, "Email is required"]
},
mobile: {
unique: [true, "Mobile Number alraedy available"],
type: String,
required: [true, "Mobile Number is required"],
validate(value) {
if (value.length !== 10) {
throw new Error("Mobile Number is invalid!");
}
}
},
password: {
type: String,
required: [true, "Password is required"],
validate(value) {
if (value.length < 6) {
throw new Error("Password must be atleast 6 characters!");
}
}
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论