[英]How to declare simpleschema OR within subschema Meteor js 1.8
In my data schema I would like to have a nested object in an object. 在我的数据模式中,我想在一个对象中有一个嵌套对象。 The nested object should be validated as well, but be able to contain different types of data. 嵌套对象也应经过验证,但能够包含不同类型的数据。 So there are several schema's for the same object data. 因此,对于相同的对象数据,存在多个模式。
To clarify this: Object > Object.data > [DataSchema A || 要澄清这一点:对象> Object.data> [DataSchema A || DataSchema B || 数据模式B || DataSchema C]. DataSchema C]。 There is a variable in Object: Object.Type which corresponds to what data schema should be used. 在Object:Object.Type中有一个变量,它对应于应使用的数据模式。
How do I apply the correct nested schema to the object. 如何将正确的嵌套模式应用于对象。
I have tried to google the problem and the function if's, don't seem to provide the functionality for this. 我试图用谷歌搜索问题和功能,如果没有,似乎没有为此提供功能。 Right now my solution is to have every variable in the subschemas optional but it's an ugly solution. 现在,我的解决方案是使子方案中的每个变量都是可选的,但这是一个丑陋的解决方案。
export const DataSchema1 = ({ /* some variables */ });
export const DataSchema2 = ({ /* some different variables */ });
export const DataSchema3 = ({ /* some different variables */ });
export const DataSchema = [
DataSchema1,
DataSchema2,
DataSchema3
];
export const ObjectSchema = new SimpleSchema({
objectType: {
// Actually is enum, to verify options. Not relevant.
type: String
},
data: {
type: DataSchema
}
});
Expect to be able to call the meteor check function to validate an object to this schema. 期望能够调用流星检查功能来验证此架构的对象。 Either based on the type variable or apply an or to the array of DataSchema. 基于类型变量,或者将或应用于DataSchema数组。
Thank you guys in advance! 预先谢谢你们! First stackoverflow post. 第一个stackoverflow帖子。
Welcome to StackOverflow! 欢迎来到StackOverflow!
SimpleSchema has a helper called oneOf
, which lets you compose types together: SimpleSchema有一个名为oneOf
的助手 ,它使您可以将类型组合在一起:
export const ObjectSchema = new SimpleSchema({
objectType: {
// Actually is enum, to verify options. Not relevant.
type: String,
allowedValues: [/* you can put your enum values here */]
},
data: {
type: SimpleSchema.oneOf([DataSchema1, DataSchema2, DataSchema3])
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.