Understanding the "additionalProperties" keyword in JSON Schema draft version 4

Hammerite picture Hammerite · May 9, 2013 · Viewed 26.5k times · Source

Link to the specification: http://json-schema.org/latest/json-schema-validation.html#anchor64

Section 5.4.4.2 states:

Successful validation of an object instance against these three keywords depends on the value of "additionalProperties": if its value is boolean true or a schema, validation succeeds; ...

Section 5.4.4.3 states:

If "additionalProperties" is absent, it may be considered present with an empty schema as a value.

Ok, so if "additionalProperties" is absent, it counts as being present with an empty schema. And if it's a schema (of any kind), then the object validates successfully regardless of any other consideration.

But this is contradicted by the assertion in section 5.4.4.5, "Example", that the given instance fails to validate against the given schema (which doesn't specify anything for "additionalProperties").

Can someone explain where and in what way I'm misinterpreting the specification?

Answer

Optimal Prime picture Optimal Prime · Jun 27, 2013

You have found an error in the spec, so your not actually misinterpreting something.

There is an updated version (from two days later) of the internet draft on the IETF website, where this example is different.

see: http://tools.ietf.org/html/draft-fge-json-schema-validation-00#page-13

As the document is an internet draft, most likely the version on http://datatracker.ietf.org/ is the correct version.

Status of This Memo

This Internet-Draft is submitted in full conformance with the
provisions of BCP 78 and BCP 79.

Internet-Drafts are working documents of the Internet Engineering
Task Force (IETF)
. Note that other groups may also distribute
working documents as Internet-Drafts. The list of current Internet-
Drafts is at http://datatracker.ietf.org/drafts/current/.

Internet-Drafts are working documents of the Internet Engineering
Task Force (IETF).

Additionally, the two versions have different dates, and expiry dates:

  • version you link - written: January 30, 2013 and Expires: August 3, 2013.
  • version on ietf - written on: February 1, 2013 and Expires: August 5, 2013

On the IETF version:

This schema will be used as an example:

   {
       "properties": {
           "p1": {}
       },
       "patternProperties": {
           "p": {},
           "[0-9]": {}
       },
       "additionalProperties": false

This is the instance to validate:

{ "p1": true, "p2": null, "a32&o": "foobar", "": [], "fiddle": 42, "apple": "pie" }

The three property sets are:

   s  [ "p1", "p2", "a32&o", "", "fiddle", "apple" ]

   p  [ "p1" ]

   pp [ "p", "[0-9]" ]

Applying the two steps of the algorithm:

      after the first step, "p1" is removed from "s";

      after the second step, "p2" (matched by "p"), "a32&o" (matched by
      "[0-9]") and "apple" (matched by "p") are removed from "s".

The set "s" still contains two elements, "" and "fiddle". Validation therefore fails.