简体   繁体   English

如何在subschema Meteor js 1.8中声明simpleschema或

[英]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.

相关问题 如何使用Meteor AutoForm和SimpleSchema引用afFormGroup中的emails。$。address? - How to reference emails.$.address within afFormGroup using Meteor AutoForm and SimpleSchema? 流星js:使用validatedmethod simpleschema获取完整的验证消息 - Meteor js: get full validation message with validatedmethod simpleschema 流星js simpleschema带有模式的索引已存在不同的选项 - meteor js simpleschema Index with pattern already exists with different options 如何告诉SimpleSchema更新当前的Meteor.user? - How do I tell SimpleSchema to update the current Meteor.user? 如何在实现SimpleSchema的Meteor中遍历包含对象的数组 - How to iterate through an array containing objects in Meteor implementing SimpleSchema 如何通过autoValue将Meteor.userId()添加到SimpleSchema? - How to add Meteor.userId() to SimpleSchema via autoValue? Meteor.js&Collection2:在子模式中插入新对象的更新方法 - Meteor.js & Collection2: Update Method that Inserts New Object in subschema 流星SimpleSchema重复嵌套对象 - Meteor SimpleSchema Repeated Nested Objects meteor.js“ tsega:bootstrap3-datetimepicker”使用SimpleSchema“ type:Date”记录数据 - meteor.js “tsega:bootstrap3-datetimepicker” record data using the SimpleSchema “type: Date” 如何从Meteor.js中重新启动Meteor服务器 - How to restart Meteor server from within Meteor.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM