Postman has built-in support for schema validation. You define your schema as a JavaScript object in the Tests tab, then validate the response against it. There are two libraries available: tv4 (built-in, older) and Ajv (modern, recommended for new projects).
tv4 (Tiny Validator version 4) comes pre-installed in Postman. You do not need to import anything. It has been available since the early days of Postman scripting.
// Define the schema for a single user object
const userSchema = {
type: "object",
required: ["id", "name", "email", "phone"],
properties: {
id: { type: "integer", minimum: 1 },
name: { type: "string", minLength: 1 },
email: { type: "string" },
phone: { type: "string" },
website: { type: "string" },
company: {
type: "object",
required: ["name"],
properties: {
name: { type: "string" },
catchPhrase: { type: "string" },
bs: { type: "string" }
}
}
},
additionalProperties: true
};
pm.test("Response matches user schema", function () {
const response = pm.response.json();
const isValid = tv4.validate(response, userSchema);
if (!isValid) {
console.log("Schema error:", tv4.error.message);
console.log("At path:", tv4.error.dataPath);
}
pm.expect(isValid).to.be.true;
});tv4 does NOT check the "required" keyword by default. You must pass the checkRecursive option: tv4.validate(response, schema, true). Without this, missing required fields will pass silently. This is the number one tv4 gotcha.
// WRONG — required fields are not checked
pm.test("Schema validation (broken)", function () {
const isValid = tv4.validate(pm.response.json(), schema);
pm.expect(isValid).to.be.true; // passes even if required fields are missing!
});
// CORRECT — pass checkRecursive=true to enforce required
pm.test("Schema validation (fixed)", function () {
const isValid = tv4.validate(pm.response.json(), schema, true);
if (!isValid) {
console.log("Error:", tv4.error.message, "at", tv4.error.dataPath);
}
pm.expect(isValid).to.be.true;
});Ajv (Another JSON Schema Validator) is newer, faster, and follows the JSON Schema spec more closely. Postman includes it in recent versions. Use Ajv for new test suites.
// Import Ajv — available in Postman sandbox
const Ajv = require("ajv");
const ajv = new Ajv({ allErrors: true }); // allErrors shows ALL failures, not just the first
const userSchema = {
type: "object",
required: ["id", "name", "email"],
properties: {
id: { type: "integer", minimum: 1 },
name: { type: "string", minLength: 1 },
email: { type: "string", format: "email" },
isActive: { type: "boolean" }
},
additionalProperties: false
};
pm.test("Response matches schema (Ajv)", function () {
const validate = ajv.compile(userSchema);
const response = pm.response.json();
const isValid = validate(response);
if (!isValid) {
console.log("Validation errors:", JSON.stringify(validate.errors, null, 2));
}
pm.expect(isValid).to.be.true;
});When the API returns an array of objects (like GET /users), you need to validate the array itself AND each item inside it.
// Schema for an array of users
const usersArraySchema = {
type: "array",
minItems: 1,
items: {
type: "object",
required: ["id", "name", "email"],
properties: {
id: { type: "integer", minimum: 1 },
name: { type: "string", minLength: 1 },
email: { type: "string" }
}
}
};
pm.test("All users match schema", function () {
const Ajv = require("ajv");
const ajv = new Ajv({ allErrors: true });
const validate = ajv.compile(usersArraySchema);
const response = pm.response.json();
const isValid = validate(response);
if (!isValid) {
console.log("Errors:", JSON.stringify(validate.errors, null, 2));
}
pm.expect(isValid).to.be.true;
});Store your schemas in Postman environment or collection variables. This way, you define the schema once and reuse it across multiple requests. Set it in the collection Pre-request: pm.collectionVariables.set("userSchema", JSON.stringify(schema)). Read it in Tests: JSON.parse(pm.collectionVariables.get("userSchema")).
Q: How do you perform schema validation in Postman? What libraries are available?
A: Postman supports two schema validation libraries: tv4 (Tiny Validator, built-in) and Ajv (Another JSON Schema Validator, requires import). tv4 is simpler but has a gotcha — it does not check "required" fields by default unless you pass checkRecursive=true. Ajv is modern, supports draft-07, and checks required fields by default. You define the schema as a JavaScript object in the Tests tab, then validate the response against it. For array responses, set the root type to "array" with an "items" schema for each element.
Key Point: Postman offers tv4 (built-in, simpler) and Ajv (modern, stricter) for schema validation. Always use allErrors:true with Ajv and checkRecursive with tv4 to catch all issues.