Javascript 当嵌套文档存在时,如何验证该嵌套文档的属性是否存在?
user.schema.jsJavascript 当嵌套文档存在时,如何验证该嵌套文档的属性是否存在?,javascript,mongoose,Javascript,Mongoose,user.schema.js var Schema = require('mongoose').Schema; var uniqueValidator = require('mongoose-unique-validator'); var _ = require('lodash'); var userSchema = new Schema({ local: { username: String, // should exist when local exists role:
var Schema = require('mongoose').Schema;
var uniqueValidator = require('mongoose-unique-validator');
var _ = require('lodash');
var userSchema = new Schema({
local: {
username: String, // should exist when local exists
role: String,
hashedPassword: { type: String, select: false }
},
facebook: {
id: String,
token: { type: String, select: false }
},
twitter: {
id: String,
token: { type: String, select: false }
},
google: {
id: String,
token: { type: String, select: false }
}
});
userSchema.path('local').validate(function(local) {
var empty = _.isEmpty(local);
if (empty) {
return true;
}
else if (!empty && local.username) {
return true;
}
else if (!empty && !local.username) {
return false;
}
}, 'Local auth requires a username.');
module.exports = userSchema;
我试图验证当local
不为空时,username
是否存在。即,当使用本地身份验证时,应显示用户名
// should validate
user = {
local: {
username: 'foo';
hashedPassword: 'sfsdfs'
}
};
// shouldn't validate
user = {
local: {
hashedPassword: 'sdfsdfs'
}
};
// should validate (because local isn't being used)
user = {
local: {},
facebook {
...
}
};
我得到这个错误:
/Users/azerner/code/mean-starter/server/api/users/user.schema.js:51
userSchema.path('local').validate(function(local) {
^
TypeError: Cannot read property 'validate' of undefined
似乎无法获取对象的路径。我了解到模式有一个路径
属性。当我console.log(userSchema.path)
时:
因此,似乎存在像local.username
和facebook.token
这样的路径,但不存在像local
和facebook
这样的“顶级”路径
如果我尝试验证local.username
路径,它的工作方式与我希望的不一样
userSchema.path('local.username').validate(function(username) {
return !!username
}, 'Local auth requires a username.');
只有当存在local.username
时,才会应用验证。我想验证它是否存在。因此,当它不存在时,不会应用验证,因此它被认为是有效的并被保存
我还尝试了以下方法,但结果与local.username
方法相同(当用户名不存在时,验证不会被命中,并且会被标记为有效)
看起来您正在尝试创建自定义验证。不确定您是否实现了它所需的所有功能。看起来是这样的:
// make sure every value is equal to "something"
function validator (val) {
return val == 'something';
}
new Schema({ name: { type: String, validate: validator }});
// with a custom error message
var custom = [validator, 'Uh oh, {PATH} does not equal "something".']
new Schema({ name: { type: String, validate: custom }});
// adding many validators at a time
var many = [
{ validator: validator, msg: 'uh oh' }
, { validator: anotherValidator, msg: 'failed' }
]
new Schema({ name: { type: String, validate: many }});
// or utilizing SchemaType methods directly:
var schema = new Schema({ name: 'string' });
schema.path('name').validate(validator, 'validation of `{PATH}` failed with
value `{VALUE}`');
下面是链接:Adam,为什么不尝试一个预验证钩子,有条件地将错误传递给下一个函数。我想这会给你你想要的灵活性。如果不起作用,请告诉我
比如说
schema.pre('validate', function(next) {
if(/*your error case */){ next('validation error text') }
else { next() }
})
这将导致mongoose向试图保存文档的人发送ValidationError
// make sure every value is equal to "something"
function validator (val) {
return val == 'something';
}
new Schema({ name: { type: String, validate: validator }});
// with a custom error message
var custom = [validator, 'Uh oh, {PATH} does not equal "something".']
new Schema({ name: { type: String, validate: custom }});
// adding many validators at a time
var many = [
{ validator: validator, msg: 'uh oh' }
, { validator: anotherValidator, msg: 'failed' }
]
new Schema({ name: { type: String, validate: many }});
// or utilizing SchemaType methods directly:
var schema = new Schema({ name: 'string' });
schema.path('name').validate(validator, 'validation of `{PATH}` failed with
value `{VALUE}`');
schema.pre('validate', function(next) {
if(/*your error case */){ next('validation error text') }
else { next() }
})