我有一个以这种方式填充的对象:
[...]
time: '',
age: '',
items: {
shirt: {selected: false, max_price: '250'},
pants: {selected: false, max_price: '250'},
dress: {selected: false, max_price: '250'},
shoe: {selected: false, max_price: '250'}
},
request: '',
[...]
我必须设置一个Schema"模板"准备推送到数据库并告诉每个变量期望什么类型。这就是我迷失的地方,我不知道如何使用类型来实现对象。
[...]
age: { type: Number},
items : {
type: String:
{selected: type: boolean, max_price: Number} // I have no idea
},
request: { type: String },
[...]
谢谢!
答案 0 :(得分:0)
// User class.
function User(userData) {
this.data = userData;
return {
save: function() {
// Let's use some validation library here (validatorjs, chaijs,..) to validate the input value against schema.
someValidator(User._schema, this.data);
...
}
delete: function() {...}
}
}
// Static (but not private) variable. To make it private, Google Search for OOP in javascript.
User._schema = {
age: { type: Number},
items : {
type: String:
{selected: type: boolean, max_price: Number} // I have no idea
},
request: { type: String },
};
// Use your object.
var newUser = new User({....});
newUser.save();