API Response Schema Validation in Playwright + Typescript
Introduction
API Response Schema Validation in Playwright + Typescript
Introduction
API Response Schema Validation in TypeScript automation ensures tests validate not only response values but also the full structure of API payloads. In this blog, I’ll show how to use AJV, one of the most popular and performant JSON schema validators, to supercharge your Playwright API tests.
Before diving into API Response Schema Validation, I recommend checking out my earlier post on Playwright for API Testing — POC and Framework Setup with TypeScript*. It lays the groundwork to start writing api tests.*
Why Schema Validation?
While TypeScript offers compile-time safety for your code, it doesn’t validate the shape of JSON responses coming from APIs at runtime.
Schema validation:
-Catches contract breaks early -Protects against backend changes or regressions -Increases confidence in integration tests -Makes tests self-documenting and auditable
Installation
Install AJV using below command
npm install ajv
After running above command, package.json should list AJV as a dev dependency:
“devDependencies”: {
“ajv”: “⁸.17.1”,
}
Writing and Using JSON Schemas
Define your response schema as a JSON file (I have saved the response schema in “src/response-schemas/registration/user-registration.json”)
Example API Response Schema:
{
"type": "object",
"properties": {
"status": { "type": "string" },
"applicantID": { "type": "string" }
},
"required": ["status", "applicantID"]
}
Validating API Responses in Playwright Tests
Here’s a real example from our test available at (‘tests/integrationTests/registration/UserRegistration.spec.ts’):
import Ajv from 'ajv';
import path from "path";
import fs from "fs";
// Call the API and get api response body
const schemaPath = path.resolve(
process.cwd(),
"src",
"response-schemas",
"registration",
"user-registration.json"
);
const schema = JSON.parse(fs.readFileSync(schemaPath, "utf-8"));
const ajv = new Ajv();
const validate = ajv.compile(schema);
const isValid = validate(responseBody);
expect(isValid).toBeTruthy(); // Fails if the response does not match the schema
Explanation
- ajv.compile(schema): AJV compiles the schema and produces a high-performance validator function.
- validate(responseBody): validator is run against the API response body. If the structure doesn’t match, it returns false.
Best Practices
- Keep Schemas Versioned: Store your JSON schemas in the repo for version control and review.
- Test Both Success and Error Responses: Validate all possible response structures, not just the happy path.
- Combine with TypeScript Types: Use types for request building and AJV for response validation for end-to-end safety.
Conclusion
Schema validation with AJV is a must-have for any API automation project that values stability and contract enforcement. Combined with Playwright and TypeScript, it ensures your tests are both powerful and maintainable.
See the full implementation:
(https://github.com/reachtonavakanth/demoproject_playwright_ts)
Happy Testing and Validating!
메타데이터
- post_id
- 52bb207f2bee
- slug
- api-response-schema-validation-in-playwright-typescript-52bb207f2bee
- url
- https://medium.com/@mailtonavakanth/api-response-schema-validation-in-playwright-typescript-52bb207f2bee
- canonical_url
- https://medium.com/@mailtonavakanth/api-response-schema-validation-in-playwright-typescript-52bb207f2bee
- author_url
- https://medium.com/@mailtonavakanth
- status
- ok
- fetched_at
- 2026-07-17 19:48:49