Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 863
sebachili Avatar asked Mar 09 '23 20:03

sebachili


1 Answers

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"]
      }
    }
  }
}
like image 78
AshwinKrishna Avatar answered Mar 24 '23 19:03

AshwinKrishna