【发布时间】:2017-07-06 17:12:14
【问题描述】:
这就是我使用带有有条件必填字段 (module) 的 SimpleSchema 进行验证的方式。因此,仅当 type 的值为 'start' 时才需要这样做
客户
const module = 'articles',
_id = 'bmphCpyHZLhTc74Zp'
console.log(module, _id)
// returns as expected 'articles' and 'bmphCpyHZLhTc74Zp'
example.call(
{
type : 'start',
module: module,
_id : _id
},
(error, result) => {
if (error) console.log(error)
}
)
服务器
example = new ValidatedMethod({
name : 'example',
validate: new SimpleSchema({
_id : { type: SimpleSchema.RegEx.Id },
type: {
type : String,
allowedValues: ['start', 'stop'] },
module: {
type : String,
optional : true,
allowedValues: ['articles'],
custom : function() {
if (this.field('type').value === 'start') return 'required'
return null
}
}
}).validator(),
run({ type, _id, module }) {
console.log(_id, module)
}
})
但我确实收到了错误"validation-error",原因是"Module is required"。
我不明白,正如您所见,module 完全有价值!
【问题讨论】:
标签: javascript meteor simple-schema