Couscous Merguez
Couscous Merguez

Reputation: 13

enum list as object property in json schema

I'm looking to find a way to declare objects defined in an enum list.

Here is what I want to check :

{
  "object1": {
    "subobject1": {
      "value": 123
    },
    "subobject2": {
      "value": 456
    }
  },
  "object2": {
    "subobject3": {
      "value": 789
    }
  },
  "object3": {
    "subobject4": {
      "value": 123
    }
  },
  "object4": {
    "subobject5": {
      "value": 234
    }
  }
}

Here is my schema that I want to use for validation :

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "definitions": {
        "list1": {
            "enum": [
                "object1",
                "object2",
                "object3",
                "object4"
            ],
       "list2": {
            "enum": [
                "subobject1",
                "subobject2",
                "subobject3",
                "subobject4",
                "subobject5"
            ]
        }

        }
    },
    "properties": {
        "type": {
            "anyOf": [
                {
                    "$ref": "#/definitions/list1"
                }
            ]
        }
    },
    "additionalProperties": false
}

But I get the following error :

Property 'object1' has not been defined and the schema does not allow additional properties.

I really want to be very restrictive and be able to declare only the one that as listed in the enum because I know that if I remove "additionalProperties": false I can add any propertry I want and it works.

Upvotes: 1

Views: 1478

Answers (1)

palvarez
palvarez

Reputation: 1598

The instance you gave as example can be validated with the following schema

{
    "type": "object",
    "definitions": {
      "list1": {
        "properties": {
          "subobject1": {"type": "object"},
          "subobject2": {"type": "object"},
          "subobject3": {"type": "object"},
          "subobject4": {"type": "object"},
          "subobject5": {"type": "object"}
        }
      }
   },
   "properties": {
     "object1": {"type": "object", "$ref": "#/definitions/list1"},
     "object2": {"type": "object", "$ref": "#/definitions/list1"},
     "object3": {"type": "object", "$ref": "#/definitions/list1"},
     "object4": {"type": "object", "$ref": "#/definitions/list1"}
   },
   "additionalProperties": false
}

Maybe it's not what you wanted? Am I close?

Upvotes: 1

Related Questions