Home JSON Validate JSON Schema Validate Definition
Definition and Usage A validator takes a schema and an instance and reports whether the instance satisfies every applicable keyword. Production apps use a draft-compliant library; below you will try a minimal educational checker for type and required.
Order of checks. Parse JSON first. Then validate against the schema. Invalid JSON never reaches schema rules.
Flow
Validation Flow 1. Parse JSON.parse the instance (and schema) text2. Validate Run the draft rules for each keyword 3. Result Pass, or collect errors with paths 4. Act Accept the payload or return 400 / fix the data Code
Examples Use View Output for a quick result, or Try It Yourself to edit and run in the browser.
1. Minimal type + required check 2. Parse then validate 3. Fail on bad payload Example 1 — Minimal type + required check Educational validator for object schemas.
function miniValidate(instance, schema) {
const errors = [];
if (schema.type === "object") {
if (instance === null || typeof instance !== "object" || Array.isArray(instance)) {
errors.push("type: expected object");
return errors;
}
}
(schema.required || []).forEach(function (key) {
if (!Object.prototype.hasOwnProperty.call(instance, key)) {
errors.push("required: missing " + key);
}
});
const props = schema.properties || {};
Object.keys(props).forEach(function (key) {
if (!Object.prototype.hasOwnProperty.call(instance, key)) return;
const want = props[key].type;
const val = instance[key];
const got = val === null ? "null" : Array.isArray(val) ? "array" : typeof val;
if (want === "integer") {
if (!Number.isInteger(val)) errors.push(key + ": expected integer");
} else if (want && got !== want) {
errors.push(key + ": expected " + want);
}
});
return errors;
}
const schema = {
type: "object",
required: ["name", "age"],
properties: {
name: { type: "string" },
age: { type: "integer" }
}
};
console.log(miniValidate({ name: "Ada", age: 36 }, schema));
console.log(miniValidate({ name: "Ada" }, schema));
console.log(miniValidate({ name: "Ada", age: "36" }, schema)); []
[ 'required: missing age' ]
[ 'age: expected integer' ] How It Works Empty error list means the educational check passed. Missing required keys and wrong types produce messages. Use a real library for full draft compliance. Example 2 — Parse then validate Invalid JSON never reaches the schema.
const text = '{"name":"Ada","age":36}';
const schema = {
type: "object",
required: ["name"],
properties: { name: { type: "string" }, age: { type: "integer" } }
};
try {
const instance = JSON.parse(text);
const missing = (schema.required || []).filter(function (k) {
return !(k in instance);
});
console.log("parsed OK");
console.log("missing required:", missing);
} catch (err) {
console.log(err.name + ": " + err.message);
} parsed OK
missing required: [] How It Works Always parse first. Then apply schema checks. Syntax errors and schema errors are different problems. Example 3 — Fail on bad payload Catch a type mismatch after parse.
const instance = { productId: "1", productName: "Door", price: 12.5 };
const schema = {
properties: {
productId: { type: "integer" },
productName: { type: "string" },
price: { type: "number" }
}
};
const id = instance.productId;
const ok = Number.isInteger(id);
console.log("productId integer?", ok);
if (!ok) console.log("validation failed for productId"); productId integer? false
validation failed for productId How It Works The JSON parsed fine — keys exist. Schema still rejects a string ID when integer is required. APIs usually return 400 with the keyword that failed. Frequently Asked Questions What does a validator return? Typically a boolean valid flag plus error details listing which keyword failed and where in the instance.
Is JSON.parse enough? No. JSON.parse only checks that text is valid JSON. A schema checks that the structure and types match your contract.
Which library should I use? In JavaScript, Ajv is common. Python, Java, Go, and others have popular validators—pick one that supports your draft.
Can I validate in the browser? Yes with a JS validator library, or use an online tool. The Try It examples here show a tiny educational checker only.
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