Enumerated Values

How do you constrain a property to have a fixed set of values? Enumerated values are used to solve this problem.

Consider we add a field hobbies to the document. The hobbies field can have only three possible values: reading, writing, and painting.

1{
2  "name": "John Doe",
3  "age": 25,
4  "hobbies": "reading"
5}

To define the enumerated values in JSON Schema, use the enum keyword.

Example:

1{
2  "department": {
3    "enum": ["engineering", "marketing", 
4            "sales", "HR", "finance"]
5  }
6}

Now, try to modify the hobbies property on the side editor with enum constrain to only allow reading, writing, and painting values.

Good to Know

Values defined in enum are case-sensitive.

So reading and Reading are considered different values.

Loading...
Output
Please click the button or use
Shift+Enter
to view the output