JSON Schema Validation - dependencies example

4,414 views
Skip to first unread message

chan...@gmail.com

unread,
Nov 26, 2013, 11:12:37 AM11/26/13
to json-...@googlegroups.com
dependencies - http://tools.ietf.org/html/draft-fge-json-schema-validation-00#page-14

Can someone post few examples (schema & property dependencies) of how "dependencies" is used in JSON schema? Not able to find any dependencies example in http://json-schema.org.

Thanks.

turnit...@gmail.com

unread,
Nov 26, 2013, 5:36:38 PM11/26/13
to json-...@googlegroups.com
http://spacetelescope.github.io/understanding-json-schema/reference/object.html?highlight=dependencies

This guide is linked to from http://json-schema.org. The highlighted section which is partially reproduced below, describes an easy use case for dependencies

If we want to say that credit_card is optional and also require that billing_address is always present when we do have a credit_card, we might do the following.
{
  "type": "object",

  "properties": {
    "name": { "type": "string" },
    "credit_card": { "type": "number" },
    "billing_address": { "type": "string" }
  },

  "required": ["name"],

  "dependencies": {
    "credit_card": ["billing_address"]
  }
}

The following is good:
{ "name": "John Doe", "credit_card": 5555555555555555, "billing_address": "555 Debtor's Lane" }

The following is also good:
{
"name": "John of the Doe"
}

The following fails because there is a credit_card but not a billing_address:
{ "name": "John Doe", "credit_card": 5555555555555555 }

There are more examples in the link

espi...@gmail.com

unread,
Dec 2, 2013, 3:31:38 PM12/2/13
to json-...@googlegroups.com
We have an involved use case which we have been trying to use dependencies for.
I'm not sure if this is the correct use.  But here is the case.

We are trying to specify validation rules based on values.  In this case is different combinations of values may be valid.
What I am trying to say by the oneOf is that one of these dependencies should be true.
I then have the following cases:

conditions:
    1.If ID=B, and levelID=f, and nounID validates against regex (if nounID is 1000101 and either 1000102 or 1000103) and selected=true
    2. If ID=B, and levelID=e then disallow monkeys
    3.oneOf must be true (If ID=B, levelID=e1, nounID=1000101, selected=true), (If ID=B, levelID=e1, nounID=1000102, selected=true), (If ID=B, levelID=e1, nounID=1000103, selected=true)

{"appContext": {
        "selected": {
            "dependencies": {
                "oneOf":[
     {"allOf": [
                        {"ID": {"enum": ["B"]},
                        "levelID": {"enum": ["f"]},
                        "nounId": {"pattern": "1000101,(1000102|1000103)"},
                        "selected": {"enum": [true]}}]},
                    {"allOf": [
                        {"ID": {"enum": ["B"]},
                        "levelID": {"enum": ["e"]},
                        "disallow":[{"monkeys":{"type":"array"}}]}]},
                    {"oneOf": [
                        {"ID": {"enum": ["B"]},
                        "levelID": {"enum": ["e1"]},
                        "nounId": {"pattern": "1000101"},
                            "selected": {"enum": [true]}},
                        {"ID": {"enum": ["B"]},
                        "levelID": {"enum": ["e1"]},
                        "nounId": {"pattern": "1000102)"},
                            "selected": {"enum": [true]}},
                        {"ID": {"enum": ["B"]},
                        "levelID": {"enum": ["e1"]},
                        "nounId": {"pattern": "1000103)"},
                        "selected": {"enum": [true]}}]}]}}}}

Please let me know your comments on this approach.

Geraint

unread,
Dec 3, 2013, 5:24:52 AM12/3/13
to json-...@googlegroups.com, espi...@gmail.com
I haven't got my head around every aspect of this example, as it's quite complicated.

However:  the "dependencies" keyword only changes behaviour based on the presence/absence of a particular property in an object.  It cannot do more complex "if/then" clauses.

For that, I'm afraid you will have to wait for the v5 "switch" keyword.  Unfortunately, I've had an incredibly busy couple of months (including changing jobs) so the v5 spec is behind schedule.  I'm hoping to get a few draft proposals for it out this month.

Geraint
Reply all
Reply to author
Forward
0 new messages