When a request should accept only a closed list of values, replace a broad string constraint with JSON Schema's enum keyword. It makes the permitted choices explicit rather than trusting every string a client sends. section 7.2
A search request with controlled fields
A books search request can restrict fields to values such as publishers, number_of_pages, weight, physical_format, subjects, publish_date, and physical_dimensions. A requested field outside that list fails schema validation. section 7.2
Listing 7.5 Schema for the search request with enumeration values
{
"type": "object",
"properties": {
"title": {"type": "string"},
"fields": {
"type": "array",
"items": {
"enum": [
"publishers",
"number_of_pages",
"weight",
"physical_format",
"subjects",
"publish_date",
"physical_dimensions"
]
}
}
}
}
Inspect the schema fragment that places enum inside the fields array's items schema. section 7.2
Use enum when:
- The accepted values come from a known, closed list. section 7.2
- The constrained value appears inside an array, place enum in the items schema. section 7.2
- A validator checks whether submitted data conforms to the schema and returns true or false. section 7.2
