JSON Schema
JSON Schema is a standard for describing and validating JSON data structures. Use libraries like Ajv to validate incoming data against a schema.
JSON Schema is a standard for describing and validating JSON data structures. Use libraries like Ajv to validate incoming data against a schema.
import Ajv from "ajv";
const ajv = new Ajv();
const schema = {
type: "object",
properties: {
name: { type: "string", minLength: 1 },
age: { type: "integer", minimum: 0, maximum: 120 },
email: { type: "string", format: "email" },
},
required: ["name", "email"],
additionalProperties: false,
};
const validate = ajv.compile(schema);
const valid = validate({ name: "Alice", email: "alice@example.com", age: 30 });
// true
validate({ name: "" }); // false — name too short, email missing
Validate all external JSON data (API responses, user input) against a schema before using it in your application.
More in JavaScript