Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate JSON Schema for list of known values

I have a special enum case in my code and need to validate against it:

{
  "status": 10
}

Let's use this imaginary list of valid values:

var valid = [10, 20, 23, 27];

How can I alter my JSON Schema to validate one of these values?

{
  type: 'object',
  required: ['status'],
  properties: {
    status: { type: number },
  }
}
like image 729
jocull Avatar asked May 30 '16 15:05

jocull


1 Answers

You just define the status property as an enum:

{
    "type" : "object",
    "required" : ["status"],
    "properties" : {
        "status" : {
            "type" : "number",
            "enum" : [10, 20, 23, 27]
        }
    }
}
like image 90
jruizaranguren Avatar answered Nov 12 '22 17:11

jruizaranguren