AAmit’s LibraryLEARN · PRACTISE · REFLECT

Computers · Programming Atlas

JSON Schema Foundations

A bounded draft 2020-12 schema-language introduction: types, bounds, object and array contracts, local reuse, composition, conditional presence and annotations. The Node driver exercises an installed validator; this is not a general-purpose programming language or full-draft certification.

Audience: Learners comfortable with JSON and the existing JavaScript/Data Formats introductions.

Practice: 4 to 6 hours.

Website reference edition

This introductory material is available here for reading and practice planning. New offline workbench exports, PDF and ZIP editions for these additions are not published. The released Atlas 0.11 downloads cover the earlier 19 packs only. These pages do not run teaching code or contact an AI service.

Runtime and boundaries

Export the .json schema and its matching Check.cjs companion into a new practice folder. Run /usr/local/bin/node --max-old-space-size=64 "JSON Schema 01 Integers Check.cjs", substituting the exact exported driver name. The driver reads only its fixed schema and uses installed Ajv 8.20.0 at /usr/local/lib/node_modules/openclaw/node_modules/ajv/dist/2020.js. That host-specific application-bundled path may change and is not a portable package installation. No dependency is copied or downloaded. Only trusted authored schemas are compiled. The HTML exports text and never runs Node or Ajv.

Lessons

1. Keep parsing, schema preparation and validation separate

JSON.parse checks JSON syntax and creates values. Ajv then prepares a validator from an authored schema; only after that can it judge an instance. A malformed schema or strict-mode policy failure is different from a well-formed instance that violates a valid contract. All three stages need separate evidence. The .json file is the schema; its JavaScript companion is only the installed-engine driver.

2. Name the dialect and implementation

These examples name JSON Schema draft 2020-12 and use Ajv2020 from installed Ajv 8.20.0. A different class or draft may use different keywords, especially tuples. The draft documents are not a claim of complete conformance or an IETF final standard. Ajv is an implementation, not a second language added to the registry.

3. Types and values are independent decisions

Choose the JSON type before adding its applicable constraints. Presence does not imply a non-null value; properties does not make a key required. Numbers are represented as JavaScript numbers here, so use fixed small values and do not infer preservation of decimal spellings or large integers. Text length, bytes, UTF-16 units and grapheme clusters are different measurements.

4. Close only the intended shape

Use required for presence and additionalProperties for a deliberately closed object at its own schema location. Arrays have independent member, length and uniqueness constraints. Draft 2020-12 prefixItems describes positional members but does not require them to exist. Predict normal, missing, null, extra and boundary cases before running the companion.

5. Reuse and combine deliberately

Local $defs and fragment $ref reuse a rule without fetching a resource. allOf requires all branches, anyOf one or more, and oneOf exactly one. Overlapping oneOf branches can reject an otherwise plausible instance. Conditional and dependentRequired rules solve different problems: one examines a condition and the other reacts to key presence.

6. Describe rather than silently rewrite

title, description and default explain a contract. They do not make this driver insert defaults or repair data. coerceTypes, useDefaults and removeAdditional are explicitly false; inputs are compared before and after validation. Format support is implementation- and configuration-dependent. No format plugin is registered; the deliberately unregistered email format is a preparation failure under the stated strict policy.

7. Trust and resource limits are separate from validity

Ajv generates validator code from schemas, so this pack compiles only trusted authored schemas. It defines no loadSchema callback, calls no compileAsync and exercises only local references. Fixed tiny data, simple patterns, an execution deadline and a heap limit bound these tests; they do not make arbitrary schemas, deeply nested data or expensive patterns safe. No network, authorization, application framework or production integration is included.

8. Record the contract and the actual evidence

Capture whether preparation succeeded, which instance was checked and whether its expected outcome matched. A rejected test instance is often the correct result of a successful driver, not another failed program. Copy needed Ajv error fields before the next validation overwrites them. Preserve schema and companion hashes, exact output, runtime versions and the test date. A later engine or draft requires new evidence.

Reference cards

Dialect: $schema

Selects the stated schema dialect; not a download instruction in these drivers.

Primitive kind: type

Checks JSON kinds; this configuration does not coerce.

Value choices: enum / const

Match one listed value or one exact value.

Numeric bounds: minimum / maximum

Inclusive numeric limits; safe small values here.

Text length: minLength / maxLength

Code-point count in the tested engine, not grapheme count.

Pattern: pattern

Regular expression need not be anchored by default; use simple authored patterns.

Object members: properties

Constrains present properties without requiring them.

Presence: required

Names keys that must exist.

Object closure: additionalProperties

Controls members not covered at this schema location.

Array members: items / prefixItems

Homogeneous versus positional draft 2020-12 rules.

Array bounds: minItems / maxItems / uniqueItems

Length and complete-value uniqueness are separate.

Local reuse: $defs / $ref

Reuses already loaded authored subschemas.

Composition: allOf / anyOf / oneOf

All, one or more, or exactly one successful branch.

Conditional presence: dependentRequired

Presence-triggered, one-way dependency.

Conditional rule: if / then / else

Adds branch constraints; does not transform data.

Composed closure: unevaluatedProperties

Closes unevaluated members after applicable subschemas.

Support file: JSON Schema run instructions.txt
Use the already installed Node 24.18.0 and Ajv 8.20.0 Ajv2020 path shown in each companion. Export one schema and its unique Check.cjs file together. Run the Check.cjs with node --max-old-space-size=64. Never run the .json as JavaScript.
The schema is the language example; JavaScript is the test driver. No schema or runtime download, remote references, custom keyword, format plugin, mutation option or async validation is used. Preparation failures are distinct from valid/invalid instance results. Use only these trusted tiny authored fixtures.

Expected schema preparation and strict-policy failures

Four deliberately authored schemas fail preparation under the stated Ajv2020 configuration. These fixtures distinguish meta-schema errors, strict-mode policy and unresolved local references. The JavaScript preparation driver runs, but no instance-validation call is made for a rejected schema. They are separate from the twelve prepared schemas and their valid/invalid instance cases.

Examples and practice

All code and values below are inert reading material. Preserve each exact filename and companion when making your own practice copy.

JSON Schema 01 - Integers and bounds

The schema describes an instance contract. A JSON integer is a number with no fractional part, not a lexical spelling rule. JavaScript numbers already lose source spelling; safe small integers are used here. A boolean or numeric string is not coerced into a number. Run the exported Check.cjs companion to see these results.

Filename: JSON Schema 01 Integers.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "integer",
  "minimum": 0,
  "maximum": 120
}

Expected output:

twenty: valid
zero: valid
boolean: invalid
numeric text: invalid
too large: invalid
Cases: 5 passed

Driver filename: JSON Schema 01 Integers Check.cjs

Companion: JSON Schema 01 Integers Check.cjs

// Fixed authored schema and tiny cases only. No schema download or input mutation.
const fs = require('node:fs');
const path = require('node:path');
const Ajv2020 = require("/usr/local/lib/node_modules/openclaw/node_modules/ajv/dist/2020.js");
const schema = JSON.parse(fs.readFileSync(path.join(__dirname, "JSON Schema 01 Integers.json"), 'utf8'));
const ajv = new Ajv2020({strict: true, allErrors: false,
  coerceTypes: false, useDefaults: false, removeAdditional: false});
const validate = ajv.compile(schema);
const cases = [["twenty", 20, true], ["zero", 0, true], ["boolean", true, false], ["numeric text", "20", false], ["too large", 121, false]];
for (const [label, value, expected] of cases) {
  const before = JSON.stringify(value);
  const actual = validate(value);
  if (actual !== expected || JSON.stringify(value) !== before) {
    throw new Error('Unexpected result or mutation: ' + label);
  }
  console.log(label + ': ' + (actual ? 'valid' : 'invalid'));
}
console.log('Cases: ' + cases.length + ' passed');

Separate expected rejection

code
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "integer",
  "minimun": 0
}
diagnostic
unknown keyword
category
strict-mode policy: misspelled keyword
stage
schema compilation or strict policy
instance validation executed
false

Expected schema preparation failure under the recorded engine and strict policy; no instance validation follows.

Authored cases

    1. twenty
    2. 20
    3. true
    1. zero
    2. 0
    3. true
    1. boolean
    2. true
    3. false
    1. numeric text
    2. 20
    3. false
    1. too large
    2. 121
    3. false
JSON Schema 02 - Text units and a bounded pattern

maxLength counts Unicode code points in this validator, not UTF-16 code units, bytes or user-perceived grapheme clusters. This pattern is deliberately simple; anchors and a separate size bound make its intent explicit. Pattern syntax and performance depend on the regular-expression implementation. No general regular-expression or denial-of-service safety claim is made.

Filename: JSON Schema 02 Strings.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "mark": {
      "type": "string",
      "minLength": 1,
      "maxLength": 1
    },
    "slug": {
      "type": "string",
      "maxLength": 12,
      "pattern": "^[a-z][a-z0-9-]*$"
    }
  },
  "required": [
    "mark",
    "slug"
  ],
  "additionalProperties": false
}

Expected output:

letter: valid
astral: valid
combining: invalid
upper-case: invalid
newline: invalid
Cases: 5 passed

Driver filename: JSON Schema 02 Strings Check.cjs

Companion: JSON Schema 02 Strings Check.cjs

// Fixed authored schema and tiny cases only. No schema download or input mutation.
const fs = require('node:fs');
const path = require('node:path');
const Ajv2020 = require("/usr/local/lib/node_modules/openclaw/node_modules/ajv/dist/2020.js");
const schema = JSON.parse(fs.readFileSync(path.join(__dirname, "JSON Schema 02 Strings.json"), 'utf8'));
const ajv = new Ajv2020({strict: true, allErrors: false,
  coerceTypes: false, useDefaults: false, removeAdditional: false});
const validate = ajv.compile(schema);
const cases = [["letter", {"mark": "é", "slug": "read-2"}, true], ["astral", {"mark": "😀", "slug": "code"}, true], ["combining", {"mark": "é", "slug": "read"}, false], ["upper-case", {"mark": "x", "slug": "Read"}, false], ["newline", {"mark": "x", "slug": "read\n"}, false]];
for (const [label, value, expected] of cases) {
  const before = JSON.stringify(value);
  const actual = validate(value);
  if (actual !== expected || JSON.stringify(value) !== before) {
    throw new Error('Unexpected result or mutation: ' + label);
  }
  console.log(label + ': ' + (actual ? 'valid' : 'invalid'));
}
console.log('Cases: ' + cases.length + ' passed');

Authored cases

    1. letter
    2. mark
      é
      slug
      read-2
    3. true
    1. astral
    2. mark
      😀
      slug
      code
    3. true
    1. combining
    2. mark
      é
      slug
      read
    3. false
    1. upper-case
    2. mark
      x
      slug
      Read
    3. false
    1. newline
    2. mark
      x
      slug
      read
      
    3. false
JSON Schema 03 - Presence, null and extra keys

properties only constrains keys that are present; required supplies presence rules. Null and absence are distinct. The integer-or-null union is explicit. additionalProperties: false closes this object at this schema location; it is not a universal rule for combining schemas.

Filename: JSON Schema 03 Objects.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "title": {
      "type": "string",
      "minLength": 1,
      "maxLength": 40
    },
    "minutes": {
      "type": [
        "integer",
        "null"
      ],
      "minimum": 0,
      "maximum": 120
    }
  },
  "required": [
    "title",
    "minutes"
  ],
  "additionalProperties": false
}

Expected output:

complete: valid
explicit null: valid
missing: invalid
extra: invalid
Cases: 4 passed

Driver filename: JSON Schema 03 Objects Check.cjs

Companion: JSON Schema 03 Objects Check.cjs

// Fixed authored schema and tiny cases only. No schema download or input mutation.
const fs = require('node:fs');
const path = require('node:path');
const Ajv2020 = require("/usr/local/lib/node_modules/openclaw/node_modules/ajv/dist/2020.js");
const schema = JSON.parse(fs.readFileSync(path.join(__dirname, "JSON Schema 03 Objects.json"), 'utf8'));
const ajv = new Ajv2020({strict: true, allErrors: false,
  coerceTypes: false, useDefaults: false, removeAdditional: false});
const validate = ajv.compile(schema);
const cases = [["complete", {"title": "Read", "minutes": 20}, true], ["explicit null", {"title": "Read", "minutes": null}, true], ["missing", {"title": "Read"}, false], ["extra", {"title": "Read", "minutes": 20, "done": true}, false]];
for (const [label, value, expected] of cases) {
  const before = JSON.stringify(value);
  const actual = validate(value);
  if (actual !== expected || JSON.stringify(value) !== before) {
    throw new Error('Unexpected result or mutation: ' + label);
  }
  console.log(label + ': ' + (actual ? 'valid' : 'invalid'));
}
console.log('Cases: ' + cases.length + ' passed');

Separate expected rejection

code
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "integer",
  "minimum": "0"
}
diagnostic
schema is invalid
category
meta-schema rejection: minimum must be numeric
stage
schema compilation or strict policy
instance validation executed
false

Expected schema preparation failure under the recorded engine and strict policy; no instance validation follows.

Authored cases

    1. complete
    2. title
      Read
      minutes
      20
    3. true
    1. explicit null
    2. title
      Read
      minutes
      null
    3. true
    1. missing
    2. title
      Read
    3. false
    1. extra
    2. title
      Read
      minutes
      20
      done
      true
    3. false
JSON Schema 04 - Homogeneous arrays

items applies one subschema to each member. Length, uniqueness and member validation are different constraints. uniqueItems compares complete JSON values; it is not a database uniqueness rule or a key-based deduplication operation. Inputs remain tiny and fixed.

Filename: JSON Schema 04 Arrays.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "minItems": 1,
  "maxItems": 3,
  "uniqueItems": true,
  "items": {
    "type": "string",
    "minLength": 1,
    "maxLength": 12
  }
}

Expected output:

two items: valid
empty: invalid
duplicate: invalid
wrong member: invalid
too many: invalid
Cases: 5 passed

Driver filename: JSON Schema 04 Arrays Check.cjs

Companion: JSON Schema 04 Arrays Check.cjs

// Fixed authored schema and tiny cases only. No schema download or input mutation.
const fs = require('node:fs');
const path = require('node:path');
const Ajv2020 = require("/usr/local/lib/node_modules/openclaw/node_modules/ajv/dist/2020.js");
const schema = JSON.parse(fs.readFileSync(path.join(__dirname, "JSON Schema 04 Arrays.json"), 'utf8'));
const ajv = new Ajv2020({strict: true, allErrors: false,
  coerceTypes: false, useDefaults: false, removeAdditional: false});
const validate = ajv.compile(schema);
const cases = [["two items", ["read", "code"], true], ["empty", [], false], ["duplicate", ["read", "read"], false], ["wrong member", ["read", 20], false], ["too many", ["a", "b", "c", "d"], false]];
for (const [label, value, expected] of cases) {
  const before = JSON.stringify(value);
  const actual = validate(value);
  if (actual !== expected || JSON.stringify(value) !== before) {
    throw new Error('Unexpected result or mutation: ' + label);
  }
  console.log(label + ': ' + (actual ? 'valid' : 'invalid'));
}
console.log('Cases: ' + cases.length + ' passed');

Authored cases

    1. two items
      1. read
      2. code
    2. true
    1. empty
      1. false
      1. duplicate
        1. read
        2. read
      2. false
      1. wrong member
        1. read
        2. 20
      2. false
      1. too many
        1. a
        2. b
        3. c
        4. d
      2. false
    JSON Schema 05 - A fixed tuple

    In draft 2020-12, prefixItems describes positional members; items: false disallows later members. minItems still matters because a prefix subschema does not require its position to exist. This pack does not mix earlier tuple syntax into the same validator instance.

    Filename: JSON Schema 05 Tuple.json

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "type": "array",
      "prefixItems": [
        {
          "type": "string",
          "minLength": 1,
          "maxLength": 12
        },
        {
          "type": "integer",
          "minimum": 0,
          "maximum": 120
        }
      ],
      "items": false,
      "minItems": 2,
      "maxItems": 2
    }
    

    Expected output:

    pair: valid
    missing: invalid
    extra: invalid
    swapped: invalid
    Cases: 4 passed

    Driver filename: JSON Schema 05 Tuple Check.cjs

    Companion: JSON Schema 05 Tuple Check.cjs

    // Fixed authored schema and tiny cases only. No schema download or input mutation.
    const fs = require('node:fs');
    const path = require('node:path');
    const Ajv2020 = require("/usr/local/lib/node_modules/openclaw/node_modules/ajv/dist/2020.js");
    const schema = JSON.parse(fs.readFileSync(path.join(__dirname, "JSON Schema 05 Tuple.json"), 'utf8'));
    const ajv = new Ajv2020({strict: true, allErrors: false,
      coerceTypes: false, useDefaults: false, removeAdditional: false});
    const validate = ajv.compile(schema);
    const cases = [["pair", ["read", 20], true], ["missing", ["read"], false], ["extra", ["read", 20, false], false], ["swapped", [20, "read"], false]];
    for (const [label, value, expected] of cases) {
      const before = JSON.stringify(value);
      const actual = validate(value);
      if (actual !== expected || JSON.stringify(value) !== before) {
        throw new Error('Unexpected result or mutation: ' + label);
      }
      console.log(label + ': ' + (actual ? 'valid' : 'invalid'));
    }
    console.log('Cases: ' + cases.length + ' passed');
    

    Authored cases

      1. pair
        1. read
        2. 20
      2. true
      1. missing
        1. read
      2. false
      1. extra
        1. read
        2. 20
        3. false
      2. false
      1. swapped
        1. 20
        2. read
      2. false
    JSON Schema 06 - Reuse with a local reference

    $defs gives the authored subschema a reusable location. The fragment reference is resolved within this already-loaded schema; it performs no network request. References express shared validation rules, not shared mutable data. Dynamic references and remote schema loading are outside this pack.

    Filename: JSON Schema 06 Definitions.json

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "$defs": {
        "minutes": {
          "type": "integer",
          "minimum": 0,
          "maximum": 120
        }
      },
      "type": "object",
      "properties": {
        "planned": {
          "$ref": "#/$defs/minutes"
        },
        "actual": {
          "$ref": "#/$defs/minutes"
        }
      },
      "required": [
        "planned",
        "actual"
      ],
      "additionalProperties": false
    }
    

    Expected output:

    both: valid
    bad planned: invalid
    bad actual: invalid
    missing: invalid
    Cases: 4 passed

    Driver filename: JSON Schema 06 Definitions Check.cjs

    Companion: JSON Schema 06 Definitions Check.cjs

    // Fixed authored schema and tiny cases only. No schema download or input mutation.
    const fs = require('node:fs');
    const path = require('node:path');
    const Ajv2020 = require("/usr/local/lib/node_modules/openclaw/node_modules/ajv/dist/2020.js");
    const schema = JSON.parse(fs.readFileSync(path.join(__dirname, "JSON Schema 06 Definitions.json"), 'utf8'));
    const ajv = new Ajv2020({strict: true, allErrors: false,
      coerceTypes: false, useDefaults: false, removeAdditional: false});
    const validate = ajv.compile(schema);
    const cases = [["both", {"planned": 30, "actual": 20}, true], ["bad planned", {"planned": -1, "actual": 20}, false], ["bad actual", {"planned": 30, "actual": "20"}, false], ["missing", {"planned": 30}, false]];
    for (const [label, value, expected] of cases) {
      const before = JSON.stringify(value);
      const actual = validate(value);
      if (actual !== expected || JSON.stringify(value) !== before) {
        throw new Error('Unexpected result or mutation: ' + label);
      }
      console.log(label + ': ' + (actual ? 'valid' : 'invalid'));
    }
    console.log('Cases: ' + cases.length + ' passed');
    

    Separate expected rejection

    code
    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "$ref": "#/$defs/absent"
    }
    
    diagnostic
    can't resolve reference
    category
    unresolved local reference: no instance validation performed
    stage
    schema compilation or strict policy
    instance validation executed
    false

    Expected schema preparation failure under the recorded engine and strict policy; no instance validation follows.

    Authored cases

      1. both
      2. planned
        30
        actual
        20
      3. true
      1. bad planned
      2. planned
        -1
        actual
        20
      3. false
      1. bad actual
      2. planned
        30
        actual
        20
      3. false
      1. missing
      2. planned
        30
      3. false
    JSON Schema 07 - Exactly one branch

    oneOf requires exactly one successful branch. Six satisfies both branches and is rejected; five satisfies neither. anyOf would accept one or more branches and allOf requires every branch. No branch transforms the instance.

    Filename: JSON Schema 07 One of.json

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "type": "integer",
      "oneOf": [
        {
          "multipleOf": 2
        },
        {
          "multipleOf": 3
        }
      ]
    }
    

    Expected output:

    two: valid
    three: valid
    six: invalid
    five: invalid
    Cases: 4 passed

    Driver filename: JSON Schema 07 One of Check.cjs

    Companion: JSON Schema 07 One of Check.cjs

    // Fixed authored schema and tiny cases only. No schema download or input mutation.
    const fs = require('node:fs');
    const path = require('node:path');
    const Ajv2020 = require("/usr/local/lib/node_modules/openclaw/node_modules/ajv/dist/2020.js");
    const schema = JSON.parse(fs.readFileSync(path.join(__dirname, "JSON Schema 07 One of.json"), 'utf8'));
    const ajv = new Ajv2020({strict: true, allErrors: false,
      coerceTypes: false, useDefaults: false, removeAdditional: false});
    const validate = ajv.compile(schema);
    const cases = [["two", 2, true], ["three", 3, true], ["six", 6, false], ["five", 5, false]];
    for (const [label, value, expected] of cases) {
      const before = JSON.stringify(value);
      const actual = validate(value);
      if (actual !== expected || JSON.stringify(value) !== before) {
        throw new Error('Unexpected result or mutation: ' + label);
      }
      console.log(label + ': ' + (actual ? 'valid' : 'invalid'));
    }
    console.log('Cases: ' + cases.length + ' passed');
    

    Authored cases

      1. two
      2. 2
      3. true
      1. three
      2. 3
      3. true
      1. six
      2. 6
      3. false
      1. five
      2. 5
      3. false
    JSON Schema 08 - Conditional requirements

    The base schema checks the allowed kind and required fields. Each branch repeats the applicable integer type for the explicit Ajv strict policy. if chooses which additional constraints apply; it does not add or change data. The condition includes presence explicitly to avoid an absent discriminator accidentally satisfying a properties-only condition.

    Filename: JSON Schema 08 Conditional.json

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "type": "object",
      "properties": {
        "kind": {
          "enum": [
            "read",
            "break"
          ]
        },
        "minutes": {
          "type": "integer",
          "minimum": 0,
          "maximum": 120
        }
      },
      "required": [
        "kind",
        "minutes"
      ],
      "additionalProperties": false,
      "if": {
        "properties": {
          "kind": {
            "const": "read"
          }
        },
        "required": [
          "kind"
        ]
      },
      "then": {
        "properties": {
          "minutes": {
            "type": "integer",
            "minimum": 1
          }
        }
      },
      "else": {
        "properties": {
          "minutes": {
            "type": "integer",
            "maximum": 0
          }
        }
      }
    }
    

    Expected output:

    read: valid
    zero reading: invalid
    break: valid
    long break: invalid
    missing kind: invalid
    Cases: 5 passed

    Driver filename: JSON Schema 08 Conditional Check.cjs

    Companion: JSON Schema 08 Conditional Check.cjs

    // Fixed authored schema and tiny cases only. No schema download or input mutation.
    const fs = require('node:fs');
    const path = require('node:path');
    const Ajv2020 = require("/usr/local/lib/node_modules/openclaw/node_modules/ajv/dist/2020.js");
    const schema = JSON.parse(fs.readFileSync(path.join(__dirname, "JSON Schema 08 Conditional.json"), 'utf8'));
    const ajv = new Ajv2020({strict: true, allErrors: false,
      coerceTypes: false, useDefaults: false, removeAdditional: false});
    const validate = ajv.compile(schema);
    const cases = [["read", {"kind": "read", "minutes": 20}, true], ["zero reading", {"kind": "read", "minutes": 0}, false], ["break", {"kind": "break", "minutes": 0}, true], ["long break", {"kind": "break", "minutes": 10}, false], ["missing kind", {"minutes": 0}, false]];
    for (const [label, value, expected] of cases) {
      const before = JSON.stringify(value);
      const actual = validate(value);
      if (actual !== expected || JSON.stringify(value) !== before) {
        throw new Error('Unexpected result or mutation: ' + label);
      }
      console.log(label + ': ' + (actual ? 'valid' : 'invalid'));
    }
    console.log('Cases: ' + cases.length + ' passed');
    

    Authored cases

      1. read
      2. kind
        read
        minutes
        20
      3. true
      1. zero reading
      2. kind
        read
        minutes
        0
      3. false
      1. break
      2. kind
        break
        minutes
        0
      3. true
      1. long break
      2. kind
        break
        minutes
        10
      3. false
      1. missing kind
      2. minutes
        0
      3. false
    JSON Schema 09 - An annotation is not an edit

    default is descriptive metadata here: the absent optional minutes field stays absent. The driver explicitly disables default insertion, coercion and property removal and checks that every input stays unchanged. Ajv error details are mutable per validation call; copy the needed fields immediately when reporting a result. This pack avoids format plugins and asynchronous validation.

    Filename: JSON Schema 09 Annotations.json

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "title": "Practice note",
      "description": "A bounded authored record",
      "type": "object",
      "properties": {
        "title": {
          "type": "string",
          "minLength": 1,
          "maxLength": 40
        },
        "minutes": {
          "type": "integer",
          "minimum": 0,
          "maximum": 120,
          "default": 25
        }
      },
      "required": [
        "title"
      ],
      "additionalProperties": false
    }
    

    Expected output:

    missing optional: valid
    number: valid
    numeric text: invalid
    extra: invalid
    Cases: 4 passed

    Driver filename: JSON Schema 09 Annotations Check.cjs

    Companion: JSON Schema 09 Annotations Check.cjs

    // Fixed authored schema and tiny cases only. No schema download or input mutation.
    const fs = require('node:fs');
    const path = require('node:path');
    const Ajv2020 = require("/usr/local/lib/node_modules/openclaw/node_modules/ajv/dist/2020.js");
    const schema = JSON.parse(fs.readFileSync(path.join(__dirname, "JSON Schema 09 Annotations.json"), 'utf8'));
    const ajv = new Ajv2020({strict: true, allErrors: false,
      coerceTypes: false, useDefaults: false, removeAdditional: false});
    const validate = ajv.compile(schema);
    const cases = [["missing optional", {"title": "Read"}, true], ["number", {"title": "Read", "minutes": 25}, true], ["numeric text", {"title": "Read", "minutes": "25"}, false], ["extra", {"title": "Read", "tag": "x"}, false]];
    for (const [label, value, expected] of cases) {
      const before = JSON.stringify(value);
      const actual = validate(value);
      if (actual !== expected || JSON.stringify(value) !== before) {
        throw new Error('Unexpected result or mutation: ' + label);
      }
      console.log(label + ': ' + (actual ? 'valid' : 'invalid'));
    }
    console.log('Cases: ' + cases.length + ' passed');
    

    Separate expected rejection

    code
    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "type": "string",
      "format": "email"
    }
    
    diagnostic
    unknown format
    category
    strict-mode policy: no email format plugin registered
    stage
    schema compilation or strict policy
    instance validation executed
    false

    Expected schema preparation failure under the recorded engine and strict policy; no instance validation follows.

    Authored cases

      1. missing optional
      2. title
        Read
      3. true
      1. number
      2. title
        Read
        minutes
        25
      3. true
      1. numeric text
      2. title
        Read
        minutes
        25
      3. false
      1. extra
      2. title
        Read
        tag
        x
      3. false
    JSON Schema exercise A - A bounded study record

    The solution makes all three fields required and closes the object. It rejects null rather than treating it as false. Bounds and types do not establish authorization, storage integrity or correctness of a real workflow.

    Filename: JSON Schema Exercise A Record.json

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "type": "object",
      "properties": {
        "title": {
          "type": "string",
          "minLength": 1,
          "maxLength": 40
        },
        "minutes": {
          "type": "integer",
          "minimum": 0,
          "maximum": 120
        },
        "done": {
          "type": "boolean"
        }
      },
      "required": [
        "title",
        "minutes",
        "done"
      ],
      "additionalProperties": false
    }
    

    Expected output:

    valid: valid
    empty title: invalid
    missing done: invalid
    null done: invalid
    extra: invalid
    Cases: 5 passed

    Driver filename: JSON Schema Exercise A Record Check.cjs

    Companion: JSON Schema Exercise A Record Check.cjs

    // Fixed authored schema and tiny cases only. No schema download or input mutation.
    const fs = require('node:fs');
    const path = require('node:path');
    const Ajv2020 = require("/usr/local/lib/node_modules/openclaw/node_modules/ajv/dist/2020.js");
    const schema = JSON.parse(fs.readFileSync(path.join(__dirname, "JSON Schema Exercise A Record.json"), 'utf8'));
    const ajv = new Ajv2020({strict: true, allErrors: false,
      coerceTypes: false, useDefaults: false, removeAdditional: false});
    const validate = ajv.compile(schema);
    const cases = [["valid", {"title": "Read", "minutes": 20, "done": false}, true], ["empty title", {"title": "", "minutes": 20, "done": false}, false], ["missing done", {"title": "Read", "minutes": 20}, false], ["null done", {"title": "Read", "minutes": 20, "done": null}, false], ["extra", {"title": "Read", "minutes": 20, "done": false, "x": 1}, false]];
    for (const [label, value, expected] of cases) {
      const before = JSON.stringify(value);
      const actual = validate(value);
      if (actual !== expected || JSON.stringify(value) !== before) {
        throw new Error('Unexpected result or mutation: ' + label);
      }
      console.log(label + ': ' + (actual ? 'valid' : 'invalid'));
    }
    console.log('Cases: ' + cases.length + ' passed');
    

    Authored cases

      1. valid
      2. title
        Read
        minutes
        20
        done
        false
      3. true
      1. empty title
      2. title
        minutes
        20
        done
        false
      3. false
      1. missing done
      2. title
        Read
        minutes
        20
      3. false
      1. null done
      2. title
        Read
        minutes
        20
        done
        null
      3. false
      1. extra
      2. title
        Read
        minutes
        20
        done
        false
        x
        1
      3. false

    Practice: Write a schema requiring a nonempty title of at most 40 code points, integer minutes from 0 through 120 and a boolean done flag. Reject extra properties and test absent versus null values.

    JSON Schema exercise B - Conditional presence

    dependentRequired is triggered by presence, even when done is false. It is one-way: minutes does not require done. A value-dependent completion rule would need a different contract.

    Filename: JSON Schema Exercise B Presence.json

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "type": "object",
      "properties": {
        "title": {
          "type": "string",
          "minLength": 1,
          "maxLength": 40
        },
        "done": {
          "type": "boolean"
        },
        "minutes": {
          "type": "integer",
          "minimum": 0,
          "maximum": 120
        }
      },
      "required": [
        "title"
      ],
      "additionalProperties": false,
      "dependentRequired": {
        "done": [
          "minutes"
        ]
      }
    }
    

    Expected output:

    title only: valid
    done with time: valid
    missing time: invalid
    time only: valid
    Cases: 4 passed

    Driver filename: JSON Schema Exercise B Presence Check.cjs

    Companion: JSON Schema Exercise B Presence Check.cjs

    // Fixed authored schema and tiny cases only. No schema download or input mutation.
    const fs = require('node:fs');
    const path = require('node:path');
    const Ajv2020 = require("/usr/local/lib/node_modules/openclaw/node_modules/ajv/dist/2020.js");
    const schema = JSON.parse(fs.readFileSync(path.join(__dirname, "JSON Schema Exercise B Presence.json"), 'utf8'));
    const ajv = new Ajv2020({strict: true, allErrors: false,
      coerceTypes: false, useDefaults: false, removeAdditional: false});
    const validate = ajv.compile(schema);
    const cases = [["title only", {"title": "Read"}, true], ["done with time", {"title": "Read", "done": false, "minutes": 0}, true], ["missing time", {"title": "Read", "done": false}, false], ["time only", {"title": "Read", "minutes": 20}, true]];
    for (const [label, value, expected] of cases) {
      const before = JSON.stringify(value);
      const actual = validate(value);
      if (actual !== expected || JSON.stringify(value) !== before) {
        throw new Error('Unexpected result or mutation: ' + label);
      }
      console.log(label + ': ' + (actual ? 'valid' : 'invalid'));
    }
    console.log('Cases: ' + cases.length + ' passed');
    

    Authored cases

      1. title only
      2. title
        Read
      3. true
      1. done with time
      2. title
        Read
        done
        false
        minutes
        0
      3. true
      1. missing time
      2. title
        Read
        done
        false
      3. false
      1. time only
      2. title
        Read
        minutes
        20
      3. true

    Practice: When done is present, require minutes. Keep done optional and demonstrate that false still triggers the dependency, while minutes alone is allowed.

    JSON Schema exercise C - Compose without losing closure

    unevaluatedProperties observes properties evaluated by successful applicable subschemas, unlike closing a base schema too early with additionalProperties. This deliberately small allOf composition does not establish every annotation interaction, reference cycle or draft feature.

    Filename: JSON Schema Exercise C Composition.json

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "type": "object",
      "properties": {
        "title": {
          "type": "string",
          "minLength": 1,
          "maxLength": 40
        }
      },
      "required": [
        "title"
      ],
      "allOf": [
        {
          "properties": {
            "minutes": {
              "type": "integer",
              "minimum": 0,
              "maximum": 120
            }
          },
          "required": [
            "minutes"
          ]
        }
      ],
      "unevaluatedProperties": false
    }
    

    Expected output:

    valid: valid
    missing title: invalid
    missing minutes: invalid
    extra: invalid
    Cases: 4 passed

    Driver filename: JSON Schema Exercise C Composition Check.cjs

    Companion: JSON Schema Exercise C Composition Check.cjs

    // Fixed authored schema and tiny cases only. No schema download or input mutation.
    const fs = require('node:fs');
    const path = require('node:path');
    const Ajv2020 = require("/usr/local/lib/node_modules/openclaw/node_modules/ajv/dist/2020.js");
    const schema = JSON.parse(fs.readFileSync(path.join(__dirname, "JSON Schema Exercise C Composition.json"), 'utf8'));
    const ajv = new Ajv2020({strict: true, allErrors: false,
      coerceTypes: false, useDefaults: false, removeAdditional: false});
    const validate = ajv.compile(schema);
    const cases = [["valid", {"title": "Read", "minutes": 20}, true], ["missing title", {"minutes": 20}, false], ["missing minutes", {"title": "Read"}, false], ["extra", {"title": "Read", "minutes": 20, "x": 1}, false]];
    for (const [label, value, expected] of cases) {
      const before = JSON.stringify(value);
      const actual = validate(value);
      if (actual !== expected || JSON.stringify(value) !== before) {
        throw new Error('Unexpected result or mutation: ' + label);
      }
      console.log(label + ': ' + (actual ? 'valid' : 'invalid'));
    }
    console.log('Cases: ' + cases.length + ' passed');
    

    Authored cases

      1. valid
      2. title
        Read
        minutes
        20
      3. true
      1. missing title
      2. minutes
        20
      3. false
      1. missing minutes
      2. title
        Read
      3. false
      1. extra
      2. title
        Read
        minutes
        20
        x
        1
      3. false

    Practice: Compose a title constraint with a required minutes constraint using allOf, then disallow unevaluated properties. Test both missing fields and an unrelated key.

    Prompt templates

    Planning text only; no AI service or export control is activated here.

    Build a bounded schema
    For {language}, help with {goal}. Runtime: {runtime}. Constraints: {constraints}. Provide an original draft 2020-12 schema and a separate local validation driver. State parser, preparation and instance-validation stages. Include expected outcomes without claiming execution.
    Debug the correct validation stage
    For {language}, investigate {goal}. Runtime: {runtime}. Constraints: {constraints}. Ask for the smallest schema, instance, options and exact error. Distinguish malformed JSON, invalid schema, strict-policy failure and instance rejection. Do not weaken the contract just to make a test pass.
    Test presence and boundaries
    For {language}, design tests for {goal}. Runtime: {runtime}. Constraints: {constraints}. Cover missing, null, wrong types, empty, boundary and extra fields. Record expected validity separately from successful driver execution. Keep inputs tiny and authored.
    Refactor shared rules
    For {language}, refactor {goal}. Runtime: {runtime}. Constraints: {constraints}. Use local definitions only and preserve accepted and rejected instances. Explain how closure interacts with composition. No remote reference loading or generated custom keywords.
    Document implementation limits
    For {language}, document {goal}. Runtime: {runtime}. Constraints: {constraints}. State the draft, validator version, strict options, mutation policy, supported format policy and untested features. Keep schema validity distinct from application security.
    Explain and practice
    For {language}, teach {goal}. Runtime: {runtime}. Constraints: {constraints}. Give one original small schema, a prediction exercise, the separate solution and a local driver. Explain why each rejected instance fails. Do not count frameworks or complete-language mastery.

    Recorded test scope

    status
    passed
    runtime
    Node.js 24.18.0 / V8 13.6.233.17-node.50; Ajv 8.20.0 Ajv2020; JSON Schema draft 2020-12 subset; macOS 27.0.1 arm64; Darwin 27.0.0
    operating system
    macOS 27.0.1 arm64; Darwin 27.0.0
    tested on
    2026-10-26
    assertion count
    64
    prepared schemas
    12
    driver executions
    12
    instance cases
    53
    expected valid instances
    20
    expected invalid instances
    33
    expected preparation failures
    4
    limitations
    1. Trusted authored fixed schemas and data only; resource bounds are not an arbitrary-code sandbox.
    2. No loadSchema, compileAsync, network or filesystem write in teaching drivers.
    3. Valid/invalid instances are expectations within successful drivers, not separate programs or languages.

    These are dated source-example results, not website browser, export, PDF or cross-platform acceptance.

    Further reading

    References are the reviewed source snapshot; they have not been rechecked at website publication time.