验证具有oneOf关键字的json模式 [英] validation of json schema having oneOf keyword

查看:228
本文介绍了验证具有oneOf关键字的json模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web应用程序具有以下json模式.

I have the following json schema for my web app.

{  
  "type":"object",
    "properties": {
     "person_identifier":{
        "type":"object",
        "oneOf":[
               {"$ref":"#/person_identifier/rememberme_id"},
               {"$ref":"#/person_identifier/email"},
               {"$ref":"#/person_identifier/account_number"}
        ],
        "email":{
            "type":"string"
        },
        "rememberme_id":{
            "type":"string"
        },
        "account_number":{
            "type":"string"
        }    
       }
    }
}

我的目标是仅接受对我的应用程序的api请求中的三个"person_identifier"电子邮件,rememberme_id或account_number之一.我无法验证上述架构是否会强制执行所需的约束. 我尝试在jsfiddle.com上进行验证,但是它表明我的架构未正确验证输入. 这是我在jsfiddle.com上进行验证的代码:

My goal is to accept only one of the three "person_identifier"-email, rememberme_id or account_number in the api request to my application. I am not able to validate that the above schema will enforce the required constraints. I have tried validating on jsfiddle.com but it shows that my schema is not validating input correctly. Here is my code for validation on jsfiddle.com:

var data, schema;

schema = {  
     person_identifier:{
           type:'object',
           'oneOf':[
               {$ref:'#/person_identifier/rememberme_id'},
               {$ref:'#/person_identifier/email'},
               {$ref:'#/person_identifier/buyer_account_number'}
           ],
        'email':{
            type:'string'
        },
        'rememberme_id':{
            type:'string'
        },
        'account_number':{
            type:'string'
        }    
       }
};

data = {
  person_identifier: {
    email: 'abc@abc.com',
    rememberme_id: '1345'
  }
};

alert('Validation: ' + tv4.validate(data, schema, true));

例如,jsfiddle将数据验证为正确地满足了该模式(当它不应该满足该模式时).仅应允许一个输入标识符.我已经介绍了有关使用oneOf的几乎所有可用问题和文档.关于我做错了什么的任何指示?谢谢.

For example, jsfiddle validates the data as correctly satisfying the schema when it should not. Only one input identifier should be allowed. I have referred almost all available questions and documentation on using oneOf. Any pointers as to what I am doing incorrectly? Thank you.

推荐答案

使用 http://geraintluff .github.io/tv4/try/ 我能够为我的问题提出两个答案.以下是链接: Answer2

Using http://geraintluff.github.io/tv4/try/ I was able to formulate two answers for my question. Here are the links: Answer1 and Answer2

我的问题中的模式不正确.该模式意味着匹配oneOf(所有三个引用),电子邮件,rememberme_id,buyer_account_number.数据(电子邮件和Rememberme_id)与person_identifier的电子邮件和Rememberme_id子资源(分别)匹配.它们与oneOf关键字分开出现,因此oneOf从未奏效.

The schema in my question was incorrect. The schema meant match oneOf(all three references), email, rememberme_id, buyer_account_number. The data (email and rememberme_id) matched email and rememberme_id sub-resources (individually) of person_identifier. They were present separate along with the oneOf keyword, so the oneOf never worked.

使用来自 http://www.asbjornenge.com/wwc/json_schema.html的简单说明 我明白我的错误.对于复杂的oneOf示例,与上一个链接中提到的示例不同,我们可以在所有旨在受oneOf关键字限制的子方案中使用通用的属性"(我上面发布的答案链接中的"identifierType").至关重要的是要有一个共同而又与众不同的因素,使一个工作成为可能.同样,我们所引用的oneOf中的子模式应该完全存在于oneOf数组(Answer2)内部,或者作为引用传递(Answer1).它们不能与oneOf逗号分隔,因为它破坏了目的(我犯的错误).我将@cloudfeet的答案提到了 this 问题.

Using simple explanation from http://www.asbjornenge.com/wwc/json_schema.html I understood my mistake. For complex oneOf examples, unlike the one mentioned in the previous link, we can use a common "property" in all sub-schemas intended to be restricted by the oneOf keyword ("identifierType" in the answer links I have posted above). It is crucial to have this common yet differentiating factor which makes oneOf work. Also, the sub-schemas in oneOf that we refer to should be present entirely inside the oneOf array(Answer2) or be passed as a reference(Answer1). They cannot be comma separated along with oneOf as it defeats the purpose (the mistake which I was making). I referred the answer by @cloudfeet to this question.

希望这会有所帮助.

这篇关于验证具有oneOf关键字的json模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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