我正在使用strongloop loopback v3 REST API和mongoDB作为数据源。我的模型order.json
是
{
"name": "order",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"orderNo": {
"type": "string"
},
"lines": {
"type": [
{
"type": {
"description": "string",
"quantity": "number"
}
}
]
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
我设置"strict": true
使the model accepts only predefined properties。但这不适用于数组lines
中的属性。
I.E.如果您将此对象发布到API,则会出现预期的ValidationError(代码422):
{
"orderNo": "xyz",
"someOtherProp": "hello",
"lines": [
{
"description": "abc",
"quantity": 5
}
]
}
但是如果你发布这个JSON对象,则回送将对象保存到mongoDB
{
"orderNo": "xyz",
"lines": [
{
"description": "abc",
"quantity": 5,
"someOtherProp": "hello"
}
]
}
我的问题是关于是否在模型JSON中设置任何标志以验证对象数组?或者我必须通过order.js
model extension file验证我自己的嵌套文档?
将lines
定义为另一个模型,并使其与embedsMany
模型中的order
类型相关联。
线模型
{
"name": "line",
"base": "Model",
"strict": true,
"idInjection": true,
"properties": {
"description": {
"type": "string"
},
"quantity":{
"type":"number"
}
}
}
订单模型
{
"name": "order",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"orderNo": {
"type": "string"
}
},
"validations": [],
"relations": {
"lines":{
"type": "embedsMany",
"model": "line",
"property": "lines"
}
},
"acls": [],
"methods": {}
}
这样环回将验证line
模型