Home JSON Keywords JSON Schema Keywords Definition
Definition and Usage Beyond type, keywords restrict values: lengths, ranges, patterns, enums, and array rules. Annotations like description help readers but do not validate.
Restrictions
Value Restrictions enum / const Allow only listed value(s) minLength / maxLength / pattern Constrain strings minimum / maximum / exclusive* Constrain numbers $comment / description Notes for editors — not instance data comments Arrays
Array Keywords {
"type": "array",
"items": { "type": "string" },
"minItems": 1,
"uniqueItems": true
} items describes each element; minItems and uniqueItems tighten the list—just like the official product tags example.
Code
Examples Use View Output for a quick result, or Try It Yourself to edit and run in the browser.
1. enum for status 2. pattern and minLength 3. minItems and uniqueItems Example 1 — enum for status Only listed strings are valid.
const schema = { type: "string", enum: ["open", "closed", "pending"] };
function enumOk(value, list) {
return list.indexOf(value) !== -1;
}
console.log(enumOk("open", schema.enum));
console.log(enumOk("done", schema.enum)); How It Works enum is an exact allow-list.Values must match with equality. Use const when only one value is allowed. Example 2 — pattern and minLength Strings can require length and a regex.
const schema = {
type: "string",
minLength: 3,
pattern: "^[A-Za-z]+$"
};
function stringOk(value, schema) {
if (typeof value !== "string") return false;
if (value.length < schema.minLength) return false;
return new RegExp(schema.pattern).test(value);
}
console.log(stringOk("Ada", schema));
console.log(stringOk("A1", schema));
console.log(stringOk("ab", schema)); How It Works minLength rejects short strings.pattern is a regular expression.Both must pass for the string to be valid. Example 3 — minItems and uniqueItems Tag arrays need at least one unique string.
const schema = {
type: "array",
items: { type: "string" },
minItems: 1,
uniqueItems: true
};
function tagsOk(arr) {
if (!Array.isArray(arr) || arr.length < schema.minItems) return false;
if (!arr.every(function (x) { return typeof x === "string"; })) return false;
return new Set(arr).size === arr.length;
}
console.log(tagsOk(["home", "green"]));
console.log(tagsOk([]));
console.log(tagsOk(["home", "home"])); How It Works minItems: 1 rejects empty arrays.uniqueItems rejects duplicates.items still requires each element to be a string.Frequently Asked Questions What is enum? enum lists the exact allowed values. The instance must equal one entry in the list.
What is const? const allows exactly one value—like an enum with a single item.
Do annotations validate? title, description, and $comment document the schema. They do not change validation results.
Where do string formats live? Optional format values like email or date-time hint string meaning. Support varies by validator.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
10 people found this page helpful
Helpful Share Copy link Suggestion