JSON schema : “allof” with “additionalProperties”=false

898 views
Skip to first unread message

mike...@gmail.com

unread,
Mar 27, 2014, 10:54:23 AM3/27/14
to json-...@googlegroups.com

Suppose we have schema following schema (from tutorial here ):

{
  "$schema": "http://json-schema.org/draft-04/schema#",

  "definitions": {
    "address": {
      "type": "object",
      "properties": {
        "street_address": { "type": "string" },
        "city":           { "type": "string" },
        "state":          { "type": "string" }
      },
      "required": ["street_address", "city", "state"]
    }
  },

  "type": "object",

  "properties": {
    "billing_address": { "$ref": "#/definitions/address" },
    "shipping_address": {
      "allOf": [
        { "$ref": "#/definitions/address" },
        { "properties":
          { "type": { "enum": [ "residential", "business" ] } },
          "required": ["type"]
        }
      ]
    } 

  }
}

And here is valid instance :

{
      "shipping_address": {
        "street_address": "1600 Pennsylvania Avenue NW",
        "city": "Washington",
        "state": "DC",
        "type": "business"
      }
}

I need to ensure that any additional fields for shipping_address will be invalid As I know for this purpose exist flag additionalProperties which should be set "false" But when I'm setting "additinalProprties":false as following :

"shipping_address": {
          "allOf": [
            { "$ref": "#/definitions/address" },
            { "properties":
              { "type": { "enum": [ "residential", "business" ] } },
              "required": ["type"]
            }
          ],
          "additionalProperties":false
        } 

But in this case I got validation error (checked here):

[ {
  "level" : "error",
  "schema" : {
    "loadingURI" : "#",
    "pointer" : "/properties/shipping_address"
  },
  "instance" : {
    "pointer" : "/shipping_address"
  },
  "domain" : "validation",
  "keyword" : "additionalProperties",
  "message" : "additional properties are not allowed",
  "unwanted" : [ "city", "state", "street_address", "type" ]
} ] 

The question is : how should I to limit fields for shipping_address part only ? Thanks in advance

Francis Galiegue

unread,
Mar 27, 2014, 3:46:58 PM3/27/14
to json-...@googlegroups.com
Hello,

This is the most often touted problem and question with regards to
JSON Scema at this moment.

What you have to understand is that all schemas are evaluated on their
own and have no knowledge of one another. The only way right now to
overcome this is to have "required" and "additionalProperties" in the
same schema...

Two of my proposals for draft v5 would cure that. Namely,
"strictProperties" and "merge". Your schemas would then be:

{
"$schema": "http://json-schema.org/draft-05/schema#",

"definitions": {
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" }
},
"strictProperties": true,
}
},

"type": "object",

"properties": {
"billing_address": { "$ref": "#/definitions/address" },
"shipping_address": {
"merge": {
"source": { "$ref": "#/definitions/address" },
"with": {
"properties": {
"type": { "enum": [ "residential", "business" ] }
}
}
}
}
}
}



--
Francis Galiegue, fgal...@gmail.com
JSON Schema in Java: http://json-schema-validator.herokuapp.com
Reply all
Reply to author
Forward
0 new messages