microsoft / microsoft/playwright

[Feature]: Native JSON Schema Validation for API Testing

Open
#38,483 3 comments 14 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature-api-testing P3-collecting-feedback
Dominant language
TypeScript
Stars
96.3k
Forks
6.5k
Avg merge
1d 6h
Merged PRs (30d)
180

Description

### 🚀 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:

```node
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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing Playwright's expect utility and APIRequestContext, then compare existing assertion behavior and reporting for object mismatches. Define the scope of native JSON Schema support, including the matcher API and failure output; done means the proposed schema assertion is implemented with documented behavior and coverage for the reported use case.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.