当我运行下面的PoC时,我得到
ValidationError: Alert.one is not allowed.
ValidationError: Alert.two is not allowed.
ValidationError: Alert must be of type Array.
其中我希望Alert
中的所有字符串都是可选的。
有人能想出怎么做吗?
const Schema = require('validate');
const p_schema = new Schema({
Alert: {
each: { type: String },
required: false
},
},{strict: true});
let p = {
Alert: {
one: 'x',
two: 'x'
}
};
console.log(p_schema.validate(p));
警报的类型似乎是错误的,尽管我相信您将警报的每个值都设置为字符串,但没有指定警报类型。尝试添加type: object
,或者将Alert定义为列表而非对象。尝试以下操作:
const p_schema = new Schema({
Alert: {
type: object,
each: { type: String },
required: false
},
},{strict: true});