【发布时间】:2016-12-01 16:12:49
【问题描述】:
在流星网络应用程序中,是否同时使用 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);
}
});
对于这个简单的示例,将a_field:String 重写为已验证方法的架构是“可以的”。但是对于更复杂的示例,这似乎是多余的,如果我想使用一些先前定义的架构并添加一些新字段进行验证,而不必复制整个内容呢?
【问题讨论】:
-
绑定到集合时的简单模式负责在客户端和服务器端进行验证。由于客户端验证通常不受信任,因此验证再次发生在服务器端(被认为是受信任的)。你可以看看simple schema validators。
标签: validation meteor simple-schema meteor-methods