Ajv Examples You Can Edit and Run
5 small Ajv examples written by AgentGid from the official docs, one feature each. Every one runs in your browser: change the code and see the result at once, or describe a change and let AI rewrite it. The screenshots are taken from the same code by our server, which also checks that each example starts without errors.
Ajv: usage and stats 151 AI-built projects use it
Compiling a schema with AjvAdd a sample such as { foo: "7" } or make bar required and watch which lines turn invalid.
Collecting all errors with AjvFix one field in data at a time and watch the allErrors list shrink while the default count stays at one until everything passes.
One-call validation with ajv.validateRemove the saved.push line and print ajv.errors after the loop to see that only the last result is left.
Reusing schemas with addSchema and $refChange the zip pattern in the address schema once and both home and work addresses follow it.
Custom formats in AjvChange a channel to 300 or a name to start with a digit to see the format error for that field.
Try them all in one place
import Ajv from "ajv"
const ajv = new Ajv()
const schema = {
type: "object",
properties: {
foo: { type: "integer" },
bar: { type: "string" },
},
required: ["foo"],
additionalProperties: false,
}
// compile once, then reuse the function for every object
const validate = ajv.compile(schema)
const samples = [
{ foo: 1, bar: "abc" },
{ foo: 42 },
{ bar: "abc" },
{ foo: 1.5 },
{ foo: 2, bar: 7 },
{ foo: 3, extra: true },
]
for (const data of samples) {
const valid = validate(data)
console.log(JSON.stringify(data).padEnd(22), valid ? "valid" : "invalid")
if (!valid) console.log(" ", ajv.errorsText(validate.errors))
}
Add a sample such as { foo: "7" } or make bar required and watch which lines turn invalid. Code written by AgentGid [1]. How it works, with a screenshot →
Add a sample such as { foo: "7" } or make bar required and watch which lines turn invalid. Code written by AgentGid [1]. How it works, with a screenshot →Fix one field in data at a time and watch the allErrors list shrink while the default count stays at one until everything passes. Code written by AgentGid [3]. How it works, with a screenshot →Remove the saved.push line and print ajv.errors after the loop to see that only the last result is left. Code written by AgentGid [2]. How it works, with a screenshot →Change the zip pattern in the address schema once and both home and work addresses follow it. Code written by AgentGid [2]. How it works, with a screenshot →Change a channel to 300 or a name to start with a digit to see the format error for that field. Code written by AgentGid [4]. How it works, with a screenshot →Checked Oct 12, 2026: each example starts without errors in Chromium with ajv 8.20.0. Needs WebGL.
Sources
Explanations written by AgentGid from the official docs; the numbers in brackets point here. The code marked "written by AgentGid" is ours and free to copy.