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

泄露秘密 提交于 2020-01-05 01:52:10

问题


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:

  • if ZalA.included == true, then valid.
  • if ZalA.included == true and ZalB.included == true, then valid.
  • if ZalA.included == false and ZalB.included == true, then invalid.

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


回答1:


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:

  • if ZalA.included == true, then valid.

    'required': [
      'ZalA'
    ],
    

    Requires ZalA at the end of schema after properties so that ZalA has to be present, so you can repeat this option as often as you want in each level. But this is not enougth to fulfill your check-list. Next configurations are:

    'required': [
        'included',
        'version'
    ]
    

    plus

    'included': {
      'type': 'boolean',
      'enum': [true]
    },
    

    included-property (and actually version-property as well, it was already in your question) of ZalA must be present and true so that ZalA can be considered valid. You can define an array of different types to check whether the property has a certain value or you can use pattern-option.

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:

  • if ZalA.included == true and ZalB.included == true, then valid.
  • if ZalA.included == false and ZalB.included == true, then invalid.

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



来源:https://stackoverflow.com/questions/28274898/json-tv4-object-valid-if-true-and-if-other-object-is-present

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!