14

For the given schema below, is it possible to ensure that at least one property contains a value (ie, minLength is 1):

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "fundRaiseId": {
            "type": "string"
        },
        "productTypeId": {
            "type": "string"
        },
        "businessLineId": {
            "type": "string"
        }
    }
}

So this would pass validation:

{
 "fundRaiseId": "x"
}

And this would fail as no values are present:

{
  "fundRaiseId": "",
  "productTypeId": "",
  "businessLineId": ""
}
Ric
  • 12,855
  • 3
  • 30
  • 36

2 Answers2

22

I would try something like

{
    "allOf": [{
        "type": "object",
        "properties": {
            "fundRaiseId": {
                "type": "string"
            },
            "productTypeId": {
                "type": "string"
            },
            "businessLineId": {
                "type": "string"
            }
        }
    }, {
        "anyOf": [{
            "properties": {
                "fundRaiseId": {
                    "$ref": "#/definitions/nonEmptyString"
                }
            }
        }, {
            "properties": {
                "productTypeId": {
                    "$ref": "#/definitions/nonEmptyString"
                }
            }
        }, {
            "properties": {
                "businessLineId": {
                    "$ref": "#/definitions/nonEmptyString"
                }
            }
        }]
    }],
    "definitions": {
        "nonEmptyString": {
            "type": "string",
            "minLength": 1
        }
    }
}

Explanation: the JSON to be validated should conform to 2 root-level schemas, one is your original definition (3 string properties). The other one contains 3 additional sub-schemas, each defining one of your original properties as non-empty string. These are wrapped in an "anyOf" schema, so at least one of these should match, plus the original schema.

erosb
  • 2,943
  • 15
  • 22
  • Great idea, however, the following is valid against the schema `{ }` whereas I need at least one property to contain a value as you have defined in your definition of `nonEmptyString` – Ric Jul 20 '16 at 10:29
  • Then change the anyOf subschemas to eg. `{ "properties": { "fundRaiseId": { "$ref": "#/definitions/nonEmptyString" } }, "required" : ["fundRaiseId"] }` (so use the "required" keyword) – erosb Jul 20 '16 at 10:30
  • I changed `anyOf` to `oneOf` and have got it working although all properties need to be present this more than suffices. – Ric Jul 20 '16 at 12:14
  • this can match a string with whitespaces only right? – Jeffy Mathew Apr 07 '22 at 13:14
21

Is it a requirement that you allow the values to be empty? You can write a much cleaner schema if you requiring that all the strings are non-empty.

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "fundRaiseId": { "$ref": "#/definitions/non-empty-string" },
        "productTypeId": { "$ref": "#/definitions/non-empty-string" },
        "businessLineId": { "$ref": "#/definitions/non-empty-string" }
    },
    "anyOf": [
        { "required": ["fundRaiseId"] },
        { "required": ["productTypeId"] },
        { "required": ["businessLineId"] }
    ],
    "definitions": {
        "non-empty-string": {
            "type": "string",
            "minLength": 1
        },
    }
}
Jason Desrosiers
  • 22,479
  • 5
  • 47
  • 53