[Feature]: Native JSON Schema Validation for API Testing
🚀 Feature Request
Currently, Playwright is excellent for API testing via APIRequestContext, but it lacks built-in support for validating JSON Schemas. To verify if a response matches a specific contract/structure, developers are forced to install and configure third-party libraries (such as ajv, zod, or joi). This adds extra dependencies and boilerplate code just to assert the shape of the data.
Example
I would like to see native support for Schema Validation directly within Playwright assertions. Ideally, a new matcher like toMatchSchema(schema) could be added to the expect utility.
Example of desired usage:
test('should validate user response schema', async ({ request }) => {
const response = await request.get('/api/users/1');
const userSchema = {
type: "object",
properties: {
id: { type: "integer" },
name: { type: "string" },
email: { type: "string" }
},
required: ["id", "name", "email"]
};
// Proposed native assertion
await expect(response).toMatchSchema(userSchema);
});
Motivation
Motivation:
Playwright is positioning itself as a complete testing framework (E2E, API, and Component Testing). Currently, while making API requests is simple, validating the structure of the response requires external dependencies (like ajv, zod, or joi) and significant boilerplate code. The main motivation for native support is Error Reporting. When using external libraries, validation failures often result in generic boolean errors or require complex custom logic to format the error output. A native toMatchSchema assertion would allow Playwright to generate clean, readable diffs directly in the HTML Report and CLI, highlighting exactly which property violated the schema, just like it currently does for object comparisons.