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"] }
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.
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.
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.
Option A is correct and satisfy your requirements.
{ "type": "array", "items": { "type": "string", "enum": ["one", "two", "three"] } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With