JSON Schema Keywords

Beginner
6 min read
Updated: Nov 2026
Reference

Definition and Usage

Beyond type, keywords restrict values: lengths, ranges, patterns, enums, and array rules. Annotations like description help readers but do not validate.

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

Array Keywords

tags.schema.json
{
  "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.

Examples

Use View Output for a quick result, or Try It Yourself to edit and run in the browser.

Example 1 — enum for status

Only listed strings are valid.

JSON
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));
Try It Yourself

How It Works

  1. enum is an exact allow-list.
  2. Values must match with equality.
  3. Use const when only one value is allowed.

Example 2 — pattern and minLength

Strings can require length and a regex.

JSON
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));
Try It Yourself

How It Works

  1. minLength rejects short strings.
  2. pattern is a regular expression.
  3. Both must pass for the string to be valid.

Example 3 — minItems and uniqueItems

Tag arrays need at least one unique string.

JSON
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"]));
Try It Yourself

How It Works

  1. minItems: 1 rejects empty arrays.
  2. uniqueItems rejects duplicates.
  3. items still requires each element to be a string.

Frequently Asked Questions

enum lists the exact allowed values. The instance must equal one entry in the list.
const allows exactly one value—like an enum with a single item.
title, description, and $comment document the schema. They do not change validation results.
Optional format values like email or date-time hint string meaning. Support varies by validator.

Next: Composition & $ref

Combine schemas with allOf, anyOf, oneOf, and references.

Composition tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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