SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
807 stars 171 forks source link

[Question] How to create a checker for an object property that is an array of objects? #331

Closed LaurentDardenne closed 5 years ago

LaurentDardenne commented 5 years ago

Hi, how to define a checker for an object property that is an array of objects ?

        "MyProperty" : {
              "type": "array",
              "items": [
               {
                 "type": "object",
                 "properties": {
                    "Name":  { "type": "string"},
                    "PartName":  { "type": "string"},
                    "Reason":  { "type": "string"},
                    "Result":  { "type": "string"},
                    "Severity":  { "type": "string"}
                 }
               }
            ]
        },

I have this checkers:

{
"checkers":[{
   "name":"checkContent","args":[
       {"path":"$", "type":"object", "mandatoryFields": ["name", "MyProperties"]},
       {"path":"$.MyProperty", "type":"array", "nullable": true},
       {"path":"$.MyProperty.[*]", "type":"object", "mandatoryFields": ["Name","PartName","Reason","Result","Severity"]},
   ]
}]
}
!!! how to define a checker for the nested properties ("Name","PartName","Reason","Result","Severity) ? !!!

Windows Server 2008 R2 MongoDB Server v4.0 RestHeart v3.5.0

ujibang commented 5 years ago

Hi for what I see MyProperty is an object with a nested items array of objects

so you should define the following json path expressions:

...,
{"path":"$.MyProperty", type":"object", mandatoryFields": ["type","items"] },
{"path":"$.MyProperty.type", type":"string" },
{"path":"$.MyProperty.items", type":"array" },
{"path":"$.MyProperty.items.[*] "type":"object", "mandatoryFields": ["Name","PartName","Reason","Result","Severity"] },
{"path":"$.MyProperty.items.[*].Name", "type":"string },
{"path":"$.MyProperty.items.[*].PartName", "type":"string },
...
ujibang commented 5 years ago

You might also consider using validation with JSON Schema

LaurentDardenne commented 5 years ago

Thank you :-)