Correct Way to Define Array of Enums in JSON Schema

Nov 30, 2025 · Programming · 25 views · 7.8

Keywords: JSON Schema | Enum Arrays | Data Validation

Abstract: This article provides an in-depth exploration of the technical details for correctly defining enum arrays in JSON Schema. By comparing two common approaches, it demonstrates the correctness of placing the enum keyword inside the items property. Through concrete examples, the article illustrates how to validate empty arrays, arrays with duplicate values, and mixed-value arrays, while delving into the usage rules of the enum keyword in JSON Schema specifications, including the possibility of omitting type. Additionally, extended cases show the feature of enums supporting multiple data types, offering comprehensive and practical guidance for developers.

Introduction

In modern web development and data exchange, JSON Schema serves as a powerful tool for data validation, widely used to ensure structural integrity and consistency. Among its features, defining enum arrays is a common yet often confusing technical aspect. Many developers, when attempting to define arrays that only contain specific predefined values, frequently struggle with the exact placement of the enum keyword. Based on community Q&A data and official documentation, this article systematically analyzes the correct method for defining enum arrays, aiming to help readers avoid common pitfalls and enhance development efficiency.

Problem Background and Requirements Analysis

Consider a practical scenario: we need to describe an array that can contain zero or more elements, but each element must be one of the predefined values, such as "one", "two", and "three". Validation requirements include: empty arrays like [], arrays with duplicate values like ["one", "one"], and mixed-value arrays like ["one", "three"] should pass validation, while arrays containing undefined values like "four" should be rejected. This need is prevalent in contexts such as configuration management and API parameter validation.

Comparative Analysis of Two Definition Approaches

In JSON Schema, when defining enum arrays, developers often face two choices. The first approach places the enum keyword inside the items property, as shown in the following code:

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

The second approach places enum directly at the array level, with code as follows:

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

Through in-depth analysis of JSON Schema specifications, the first approach is correct. The reason is that the items property defines the schema for each element in the array, and the enum keyword in this context restricts each element to be one of the enumerated values. In contrast, the second approach applies enum to the entire array, meaning the array itself must exactly equal one of the values in the enum list (e.g., the whole array must be ["one", "two", "three"]), which clearly does not meet the requirement of validating empty arrays or arrays with partial elements.

Validation Examples of the Correct Approach

Using the first approach, we can validate various cases from the initial requirements. For an empty array [], validation passes because items allows zero elements. For ["one", "one"], each element "one" is in the enum list, and duplicate values are permitted, so validation passes. Similarly, ["one", "three"] also satisfies the conditions. However, for ["four"], the element "four" is not in the enum list, leading to validation failure. This thoroughly demonstrates the correctness and flexibility of this method.

In-Depth Analysis of the enum Keyword

According to the latest JSON Schema specifications, the enum keyword is used to restrict a value to a fixed set of values. It must be an array with at least one element, where each element is unique. Notably, enum can be used without the type keyword, thereby supporting multiple data types. For example, in the street light color case, the schema { "properties": { "color": { "enum": ["red", "amber", "green"] } } } successfully validates { "color": "red" } but rejects { "color": "blue" }. In an extended case, after adding null and the number 42, the schema { "properties": { "color": { "enum": ["red", "amber", "green", null, 42] } } } can validate { "color": null } and { "color": 42 }, but rejects { "color": 0 }. This showcases the powerful capability of enum in handling mixed types.

Practical Recommendations and Common Pitfalls

In practical development, it is advisable to always place enum inside items to ensure individual validation of array elements. Avoid applying enum directly at the array level unless the intention is to validate the entire array's value. Furthermore, although type can be omitted to support multiple types, specifying type when the data type is clear can improve schema readability and performance. For instance, in the shape definition case, the schema { "properties": { "shape": { "enum": ["circle", "square", 1, null] } } } validates { "shape": "circle" }, { "shape": 1 }, and { "shape": null }, but rejects { "shape": "triangle" }, emphasizing the strict matching characteristic of enum values.

Conclusion

Through the analysis in this article, we have clarified the correct method for defining enum arrays in JSON Schema: placing the enum keyword inside the items property. This approach not only meets the requirement of validating zero or more predefined value elements but also accommodates scenarios with duplicate and mixed values. Combined with the feature of enum supporting multiple data types, developers can build more flexible and robust data validation schemas. It is hoped that this article provides valuable guidance for readers in their JSON Schema practices, helping to avoid common traps and improve code quality.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.