javascriptjsonvalidationtv4

JSON tv4 object valid if true and if other object is present


is possible to validate JSON, if value of object is true, then this object is valid, and if Obj2.included == true is valid, if Obj1.included == true ?

This is small piece of schema:

'attachments': {
                    'type': 'object',
                    'properties': {
                        'ZalA': {
                            'type': 'object',
                            'properties': {
                                'included': {
                                    'type': 'boolean'
                                },
                                'version': {
                                    'type': 'integer'
                                }
                            },
                            'required': [
                                'included',
                                'version'
                            ]
                        },
                        'ZalB': {
                            'type': 'object',
                            'properties': {
                                'version': {
                                    'type': 'integer'
                                },
                                'included': {
                                    'type': 'boolean'
                                },
                                'required': [
                                    'included',
                                    'version'
                                ]
                            }
                        }
                    }
                }

I would like to check:

Is it possible to check these constraints with tv4 JSON validator ?


Solution

  • I've got a solution for you. But first of all you had a little mistake in your schema, because of required-property that was within properties:

    'ZalB': {
                'type': 'object',
                'properties': {
                         'version': {
                             'type': 'integer'
                         },
                         'included': {
                             'type': 'boolean'
                         },
                         'required': [
                             'included',
                             'version'
                         ]
                 }
     }
    

    When you use it you have to define it outside before or after properties. As you have done this with ZalA :) otherwise it does not work.

    Now to your answer, I did a little experiment with this very interesting validator and came up with this:

    // schema to be used for validating
    var schema = {
      'type': 'object',
      'properties': {
        'ZalA': {
          'type': 'object',
          'properties': {
            'included': {
              'type': 'boolean',
              'enum': [
                true
              ]
            },
            'version': {
              'type': 'integer'
            }
          },
          'required': [
            'included',
            'version'
          ]
        },
        'ZalB': {
          'type': 'object',
          'properties': {
            'version': {
              'type': 'integer'
            },
            'included': {
              'type': 'boolean',
              'enum': [
                true
              ]
            }
          },
          'required': [
            'included',
            'version'
          ]
        },
        'required': [
          'ZalA'
        ],
      }
    };
    
    // data to be checked against
    var data = {
      'ZalA': {
        'version': 1,
        'included': true
      },
      'ZalB': {
        'version': 2,
        'included': true
      }
    }
    
    tv4.validateResult(data, schema); // Object { missing=[0], valid=true, error=null}
    

    Schema has to be configured so that it matches your check-list:

    These configurations are applied for ZalB too but with one difference:

    'required': [
       'ZalA'
    ],
    

    Only ZalA is required and not ZalB at the end.

    And we are done! With these configurations all your next conditions are fulfilled:

    And if ZalB.included is granted to be false as well as true then just do this:

     'enum': [
          true, false
     ]
    

    Or omit enum-option completely so that it must be a boolean on the first place.

    This is really a good validator. Thanks for your question, I'll use it for furture projects.

    P.S. You may can spare yourself to define a second schema for ZalB and just referencing(using $ref) to the schema for ZalA, but I did not test this. On the other hand you could use this little schema:

    var schema = {
      'type': 'object',
      'properties': {
        'included': {
          'type': 'boolean',
          'enum': [
            true
          ]
        },
        'version': {
          'type': 'integer'
        }
      },
      'required': [
        'included',
        'version'
      ]
    };
    

    And use it in this way:

    // a bundle of objects to be checked
    var data = [{
        'version': 1,
        'included': true
    },{
        'version': 2,
        'included': true
    }];
    
    // iterate through each object
    for(var i=0; i < data.length;i++){
    
        var obj = data[i];
    
        // validate each object
        var result = tv4.validateResult(obj, schema);
    
        if(!result.valid){
          console.log("not valid: ",result.error);
        }
    
    }
    

    I just speak for myself but for me this is the most important side of the validator-documentation. Because it contains all options you can define for certain properties to be valided:

    http://json-schema.org/latest/json-schema-validation.html