How to validate an enum value in a json schema validation?

12,322

Your JSON Schema is missing certain attributes. Look at the example provided here on how to start the schema http://json-schema.org/example1.html.

Update your schema to the below and try

{
  "type": "object",
  "properties": {
    "transactions": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": ["BREAK"]
          }
        },
        "required": ["type"]
      }
    }
  }
}
Share:
12,322

Related videos on Youtube

sebachili
Author by

sebachili

Updated on June 04, 2022

Comments

  • sebachili
    sebachili almost 2 years

    the main problem resides on validate a json against a schema that deals with arrays. So, if I put a different value seems to be still valid?

    json schema:

    {
      "transactions" : {
        "type" : "array",
        "items" : {
          "type" : "object",
          "properties" : {
            "type" : {
              "type" : "string",
              "enum" : ["BREAK"]
            },
            "required":["type"]
          },
          "required":["items"]
        }
      }
    }
    

    Input JSON:

    {
      "transactions":[
        {
          "type":"BREAKDDDDDdddddddddddddddddddddddddddJDJDJDJDJDJDJDJ"
        }
      ]
    }
    

    result: No errors found. JSON validates against the schema.

    This is wrong as we haven't defined an enum type like "BREAKDDDDD"

    http://www.jsonschemavalidator.net/

    Any thoughts on this?