1

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.

  • Your schema suggests you're expecting to see your JSON data with a root property of `type`, for starters. Also, if you're only using one reference in an object, you can remove the `anyOf` wrapper. If you want to update your question to reflect these changes, I can help further. – Relequestual May 17 '19 at 15:36
  • Do you really need to use an enum in the definition? Or you just want something that validates your data? – palvarez May 20 '19 at 08:31

1 Answers1

1

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?

palvarez
  • 1,508
  • 2
  • 8
  • 18