Determining what schemas an object member value should validate against - sgpinkus/json-schema GitHub Wiki

THIS WIKI IS OBSOLETE. PLEASE SEE THE NEW JSON-SCHEMA-ORG/JSON-SCHEMA-SPEC REPOSITORY.


NOTE: This algorithm was included in Draft 04. Adding the more verbose wording to the web site is tracked at https://github.com/json-schema-org/json-schema-org.github.io/issues/25


Implied keywords

For one object member, the set of schemas which the associated value must be valid against depends on the member itself and the following keywords:

  • properties,
  • patternProperties,
  • additionalProperties.

NOTE: the default value for these keywords is an empty object (which MUST be considered as an empty schema for additionalProperties).

NOTE 2: in the text below, the following terminology will be used:

  • the member is the object instance member's name;
  • the value is the value associated with that member.

Algorithm

We start with an empty set of schemas, which we will call s.

Schemas from properties

If one member of the properties keyword value has the same value than the target member, the corresponding value of properties is added to s.

Schemas from patternProperties

For the recall: members of patternProperties are ECMA 262 regular expressions (see pattern). For convenience, these members will be referred to as regexes from now on.

For each regex, if it matches the target member successfully, then the value associated to that regex is also added to s.

This means that several schemas may be added to the set at this step!

additionalProperties

At this stage, the value of additionalProperties is added to the set if and only s was previously empty.

Example

(note: the set s above will be materialized as a JSON array in this section)

Consider this schema:

{
    "type": "object",
    "properties": {
        "p1": { "type": "string" }
    },
    "patternProperties": {
        "p": { "minLength": 10 },
        "1": { "format": "host-name" }
    },
    "additionalProperties": {
        "disallow": "boolean"
    }
}

And this object instance:

{
    "p1": "json-schema.org",
    "p2": "slippery slope",
    "x": null
}

For member p1

  • properties in the schema has a member by the same name. The associated schema is therefore added to the set which is now:
[ { "type": "string" } ]
  • both regexes in patternProperties successfully match p1, so schemas associated with both of these regexes are added to the set, which is now:
[ { "type": "string" }, { "minLength": 10 }, { "format": "host-name" } ]
  • the set is not empty at this point, therefore we don't need to add the schema associated with additionalProperties. The final set of schemas which the value must validate against is therefore:
[ { "type": "string" }, { "minLength": 10 }, { "format": "host-name" } ]

For member p2

  • there is no member by the same name in properties. At this step, the set is therefore empty:
[]
  • regex p matches p2 successfully, not regex 1. The schema associated with regex p is therefore added to the set:
[ { "minLength": 10 } ]
  • as in the previous example, as the set is not empty at this point, additionalProperties needs not be considered; the final set of schemas which the value must validate against is therefore:
[ { "minLength": 10 } ]

For member x

  • there is no member by the same name in properties. At this step, the set is therefore empty:
[]
  • none of the regexes in patternProperties match x successfully. The set is still empty:
[]
  • the set being empty at this point, this time, the schema associated with additionalProperties must be added; the final set of schemas which the value must validate against is therefore:
[ { "disallow": "boolean" } ]
⚠️ **GitHub.com Fallback** ⚠️