在流星网络应用程序中,同时存在SimpleSchema和ValidatedMethod冗余?当试图重用先前定义的模式时,我得到一个语法错误。为流星集合使用SimpleSchema和ValidatedMethod时避免冗余?
这里是我的意思是: mycollection.js
export const myCollection = new Mongo.Collection('myCollection');
export const mySchema = new SimpleSchema({
a_field:String;
});
myCollection.attachSchema(mySchema);
现在的插入方法: methods.js
import {mySchema, myCollection} from mycollection.js;
export const insertMethod = new ValidatedMethod({
name:'insertMethod',
validate:new SimpleSchema({
mySchema, /*Shows a syntax error: How to avoid repeating the schema?*/
}).validator(),
run(args){
myCollection.insert(args);
}
});
对于这个简单的例子,这将是 “OK” 重写a_field:String
到验证方法的Schema。然而,对于更复杂的示例,这似乎相当多余,如果我想使用一些先前定义的模式并添加一些新的字段进行验证,而不必复制整个事情,那又如何呢?
简单模式绑定到集合时负责在客户端以及从服务器进行验证。由于客户端验证一般不受信任,验证再次发生在服务器端(视为可信)。您可以查看[简单模式验证器](https://github.com/aldeed/meteor-simple-schema#validation-options)。 – blueren