最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Json Schema validation: require at least one of two fields - Stack Overflow

programmeradmin0浏览0评论

I am using the following Json Schema validation package:

Using that validation package, I want to validate in the following scenario:

Fields - A, B

Validation Condition - If A value is sent then B is not required. If B value is sent, then A is not required. But we need at least one of both field values in the form.

Can anyone help me on this?

I am using the following Json Schema validation package:

https://github./hasbridge/php-json-schema

Using that validation package, I want to validate in the following scenario:

Fields - A, B

Validation Condition - If A value is sent then B is not required. If B value is sent, then A is not required. But we need at least one of both field values in the form.

Can anyone help me on this?

Share Improve this question edited Feb 16, 2017 at 6:30 Robby Cornelissen 97.5k23 gold badges150 silver badges175 bronze badges asked Feb 16, 2017 at 5:52 Vinoth BabuVinoth Babu 6,85010 gold badges40 silver badges56 bronze badges 4
  • You want to know how to write a JSON Schema that requires at least A or B? – Robby Cornelissen Commented Feb 16, 2017 at 5:56
  • I cant see no code to help you debug. – Zim84 Commented Feb 16, 2017 at 5:57
  • @RobbyCornelissen - Yes you are right – Vinoth Babu Commented Feb 16, 2017 at 6:01
  • May i know why down vote? Could you please explain that and raise down vote? – Vinoth Babu Commented Feb 16, 2017 at 6:06
Add a ment  | 

1 Answer 1

Reset to default 7

This is a JSON Schema that tests for the presence of property A and/or B:

{
  "properties": {
    "A": {},
    "B": {}
  },
  "anyOf": [{
    "required" : ["A"]
  }, {
    "required" : ["B"]
  }]
}

Whether or not your PHP library supports this syntax is a different matter since the github page states that [...] it is not yet feature plete.

Here's a screenshot of testing it against Newtonsoft's online JSON schema validator:

发布评论

评论列表(0)

  1. 暂无评论