Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to define array of enums in JSON schema

I want to describe with JSON schema array, which should consist of zero or more predefined values. To make it simple, let's have these possible values: one, two and three.

Correct arrays (should pass validation):

[] ["one", "one"] ["one", "three"] 

Incorrect:

["four"] 

Now, I know the "enum" property should be used, but I can't find relevant information where to put it.

Option A (under "items"):

{     "type": "array",     "items": {         "type": "string",         "enum": ["one", "two", "three"]     } } 

Option B:

{     "type": "array",     "items": {         "type": "string"     },     "enum": ["one", "two", "three"] } 
like image 323
senasi Avatar asked Jun 18 '15 19:06

senasi


People also ask

How are enums represented in JSON?

So how will I represent Enum type in JSON data? An enum in any language represents a set of possible values but in an object it only takes 1 value. In JSON is the same, you can define a set of values in a JSON Schema, but then in JSON data it takes only one of those values.

How are arrays represented in JSON?

A JSON array contains zero, one, or more ordered elements, separated by a comma. The JSON array is surrounded by square brackets [ ] . A JSON array is zero terminated, the first index of the array is zero (0). Therefore, the last index of the array is length - 1.

What is $ref in JSON Schema?

In a JSON schema, a $ref keyword is a JSON Pointer to a schema, or a type or property in a schema. A JSON pointer takes the form of A # B in which: A is the relative path from the current schema to a target schema. If A is empty, the reference is to a type or property in the same schema, an in-schema reference.


1 Answers

Option A is correct and satisfy your requirements.

{     "type": "array",     "items": {         "type": "string",         "enum": ["one", "two", "three"]     } } 
like image 87
jruizaranguren Avatar answered Sep 30 '22 06:09

jruizaranguren