JSON schema : “allof” with “additionalProperties”

前端 未结 4 1209
滥情空心
滥情空心 2020-12-13 12:08

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

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

  \"definitions\": {
    \"addr         


        
4条回答
  •  一整个雨季
    2020-12-13 12:36

    additionalProperties applies to all properties that are not accounted-for by properties or patternProperties in the immediate schema.

    This means that when you have:

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

    additionalProperties here applies to all properties, because there is no sibling-level properties entry - the one inside allOf does not count.

    One thing you could do is to move the properties definition one level up, and provide stub entries for properties you are importing:

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

    This means that additionalProperties will not apply to the properties you want.

提交回复
热议问题