如果为true且存在其他对象,则JSON tv4对象有效 [英] JSON tv4 object valid if true and if other object is present

查看:70
本文介绍了如果为true且存在其他对象,则JSON tv4对象有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以验证JSON,如果object的值为true,则此对象有效;如果Obj2.included == true,则有效,如果Obj1.included == true吗?

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 ?

这是一小段架构:

'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'
                                ]
                            }
                        }
                    }
                }

我要检查:

  • 如果ZalA.included == true,则有效.
  • 如果ZalA.included == true和ZalB.included == true,则有效.
  • 如果ZalA.included == false和ZalB.included == true,则无效.

是否可以使用tv4 JSON验证程序检查这些约束?

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

推荐答案

我为您提供了一个解决方案.但是首先,由于必需的属性位于属性中,因此您的模式存在一些错误:

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'
                     ]
             }
 }

使用它时,必须在属性之前或之后定义它.使用 ZalA :)完成此操作后,否则将无法正常工作.

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:

  • 如果ZalA.included == true,则有效.

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

'required': [
  'ZalA'
],

在属性后的模式末尾需要ZalA,以便必须存在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'
]

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

ZalA的

包含-属性(实际上也是版本-属性,它已经在您的问题中),必须存在,并且 true ,因此ZalA可以被认为是有效的.您可以定义不同类型的数组来检查属性是否具有特定值,或者可以使用 pattern -option.

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.

这些配置也适用于ZalB,但有一个区别:

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

'required': [
   'ZalA'
],

只需要ZalA,而最后不需要ZalB.

Only ZalA is required and not ZalB at the end.

我们完成了!通过这些配置,您的所有下一个条件都得到满足:

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

  • 如果ZalA.included == true和ZalB.included == true,则有效.
  • 如果ZalA.included == false和ZalB.included == true,则无效.

如果将ZalB.included授予为false和true,则只需执行以下操作:

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.您可能会花很多时间为ZalB定义第二个架构,而只是引用(使用 $ ref )ZalA的架构,但是我没有对此进行测试.另一方面,您可以使用以下小模式:

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'
  ]
};

并以这种方式使用它:

// 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

这篇关于如果为true且存在其他对象,则JSON tv4对象有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆