Search code examples
jsonschemajsonpathjson-schema-validatorjsonpointerjson-ref

Using a $ref and other properties within a JSON Schema


In a JSON Schema is is valid to have a $ref and then other properties within the same schema, for example.

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "title": "My schema",
    "properties": {
        "scripts": {
            "$ref": "#/definitions/scriptsBase",
            "description": "More docs.",
            "minLength": 10
        }
    },
    "definitions": {
        "scriptsBase": {
            "type": "string",
            "description": "Base Description",
            "minLength": 5
        }
    }
}

If this is allowable, then what are the rules when it comes to resolving properties defined in the $refed and the $refing schemas (in this example minLength and description. But potentially this could become much more complex if allOf etc where defined in both.


Solution

  • Found the answer in json schema property description and "$ref" usage, basically if a $ref exists all other properties are ignored.

    https://datatracker.ietf.org/doc/html/draft-pbryan-zyp-json-ref-03#section-3

    1. Syntax

    A JSON Reference is a JSON object, which contains a member named
    "$ref", which has a JSON string value. Example:

    { "$ref": "http://example.com/example.json#/foo/bar" }

    If a JSON value does not have these characteristics, then it SHOULD NOT be interpreted as a JSON Reference.

    The "$ref" string value contains a URI [RFC3986], which identifies the location of the JSON value being referenced. It is an error
    condition if the string value does not conform to URI syntax rules.
    Any members other than "$ref" in a JSON Reference object SHALL be
    ignored.