js-validator-livr icon indicating copy to clipboard operation
js-validator-livr copied to clipboard

How to describe a rule for a list of different objects?

Open apenkov opened this issue 5 years ago • 3 comments

I am trying to describe an object of this kind:

{
    operations: [
        {
            properties:{
            	type:"1",
              	group: "11",
              	data:{data1:{},
                      data2:{}
                     }
            }
        },
        {
            properties:{
            	type:"2",
              	group: "12",
              	tools:["tools1","tools2"]
            }
        },
        {
            properties:{
            	type:"3",
              	group: "13",
              	objList:[{},{}]
            }
        },
        {
            properties:{
            	type:"4",
              	group: "14"
            }
        }
    ]
}

How can i use "variable_object" for such an object?

apenkov avatar Sep 14 '19 06:09 apenkov

In general, can I validate the array as an ordered array, that is, set validation for each element?

apenkov avatar Sep 14 '19 07:09 apenkov

You can use different rules depending on type field. Here is an example

Does it solve the issue?

koorchik avatar Sep 16 '19 18:09 koorchik

In this example, we specify the rules depending on the value of one of the fields, but in the example above we have an array that consists of identical nested objects that differ only at the next level of nesting, and I can’t describe such an array

apenkov avatar Sep 16 '19 19:09 apenkov