简体   繁体   中英

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 $ref ed and the $ref ing schemas (in this example minLength and description . But potentially this could become much more complex if allOf etc where defined in both.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM