Vinoth Babu
Vinoth Babu

Reputation: 6852

Json Schema validation: require at least one of two fields

I am using the following Json Schema validation package:

https://github.com/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?

Upvotes: 2

Views: 3032

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97140

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 complete.

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

Newtonsoft's online JSON schema validator

Upvotes: 7

Related Questions