JSON Schema - specify field is required based on v

2019-01-17 10:23发布

问题:

Wondering if this is possible with schema draft 03. I've gotten dependencies working elsewhere, I think there is possibly just some creative use of them required in order to use them for specifying the required property of some field.

My current best attempt (which doesn't work) should give you some idea of what I'm after. I want a value required by default, and optional when another field has a particular value.

{
    "description"   : "An address...",
    "type" : "object",
    "properties" : {
        "postcode": {
            "type" : "string",
            // postcode should be required by default
            "required" : true,      
            // postcode shouldn't be required if the country is new zealand 
            "dependencies" : {
                "country" : {
                    "enum" : ["NZ", "NZL", "NEW ZEALAND"]
                },
                "postcode" : {
                    "required" : false      
                }
            }
        },
        "country": {
            "type" : "string",
            "enum" : [
                // various country codes and names...
            ],
            "default" : "AUS"
        }
    }
}

回答1:

This is definitely possible with version 3 of the draft. Since you have a complete list of allowed countries, then you could do something like this:

{
    "type": [
        {
            "title": "New Zealand (no postcode)",
            "type": "object",
            "properties": {
                "country": {"enum": ["NZ", "NZL", "NEW ZEALAND"]}
            }
        },
        {
            "title": "Other countries (require postcode)",
            "type": "object",
            "properties": {
                "country": {"enum": [<all the other countries>]},
                "postcode": {"required": true}
            }
        }
    ],
    "properties": {
        "country": {
            "type" : "string",
            "default" : "AUS"
        },
        "postcode": {
            "type" : "string"
        }
    }
}

So you actually define two sub-types for your schema, one for countries that require a postcode, and one for countries that do not.

EDIT - the v4 equivalent is extremely similar. Simply rename the top-level "type" array to "oneOf".



回答2:

If anybody is looking for a solution for draft 4 you can use dependencies keyword together with a enum keyword:

{
    "type": "object",
    "properties": {
        "play": {
            "type": "boolean"
        },
        "play-options": {
            "type": "string"
        }
    },
    "dependencies": {
        "play-options": {
            "properties": {
                "play": {
                     "enum": [true]
                }
            }
        }
    }
}

In this wayplay-options will always require play value to be true.



回答3:

I just looked over the 03 version of the spec and I don't think what you are describing is possible. It's definitely not "Simple Dependency" and the description of "Schema Dependency" doesn't mention any way to consider the value of the property.

It sounds like what you need is "Conditional Schema Dependency".

There's some discussion of what's possible with Simple and Schema dependencies here: http://groups.google.com/group/json-schema/msg/8145690ebb93963b

You might ask that group if there are plans to support conditional dependencies.



回答4:

Try using dependencies with 'id' :

{
  "title": "Test dependncies",
  "readOnly": false,
  "$schema": "http://json-schema.org/draft-04/hyper-schema",
  "description": "This a test to show how we can use dependencies in json schema",
  "properties":{
      "graduate": {
          "title":"Graduate?",
          "type":"string",
          "enum":["Yes","No"],
           "options": {
           "dependencies":[
              {"id":"minimumEligibilityAge","value":"Yes"},
              {"id":"courseName","value":"No"}
              ]
           }
      },
      "minimumEligibilityAge":{
          "id":"minimumEligibilityAge",
          "title":"Enter Age",
          "type":"number",
          "options":{"hide_display":true}
          },
      "courseName":{
          "id":"courseName",
          "title":"Enter Graduation Course Name",
          "type":"string"
      }
  },
  "type": "object"
}

If value of graduate is Yes, it requires minimumEligibilityAge. If value of graduate is No, it requires courseName