Registry / testing / playwright-ajv-schema-validator

playwright-ajv-schema-validator

JSON →
library1.0.2jsnpmunverified

A Playwright plugin for API response schema validation against JSON schemas, OpenAPI 3, and Swagger 2 documents. Current version 1.0.2, stable release with monthly minor updates. Built on Ajv validator. Key differentiators: integrates with pw-api-plugin and standard Playwright API requests, provides clear error reporting in HTML reports and Trace Viewer, supports environment variables to disable validation or toggle logging. No breaking changes noted yet.

npm install playwright-ajv-schema-validator
INSTALL
IMPORT
SIG · PLAYWRIGHT-AJV-SCH
P
playwright-ajv-schema-validator
testingjavascriptv1.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

validateSchema
✓ import { validateSchema } from 'playwright-ajv-schema-validator';
✗ const validateSchema = require('playwright-ajv-schema-validator');
Named export; use ESM only as package is ESM-first. CommonJS require may fail.
default
✓ import playwrightAjvSchemaValidator from 'playwright-ajv-schema-validator';
✗ const playwrightAjvSchemaValidator = require('playwright-ajv-schema-validator').default;
Default export only available as ESM; not recommended as the primary export is validateSchema.
validateSchema type
✓ import type { ValidateSchemaFn } from 'playwright-ajv-schema-validator';
TypeScript types are included; import type for type-checking.

Shows how to validate a Playwright API response against a JSON schema using validateSchema function.

import { test, expect } from '@playwright/test'; import { validateSchema } from 'playwright-ajv-schema-validator'; import path from 'path'; import fs from 'fs'; const schemaPath = path.resolve(__dirname, 'schemas', 'user.json'); const schema = JSON.parse(fs.readFileSync(schemaPath, 'utf8')); test('POST /users response schema validation', async ({ request, fixtures }) => { const response = await request.post('https://api.example.com/users', { data: { name: 'John' } }); const data = await response.json(); const validationResult = validateSchema(fixtures, data, schema); expect(validationResult.passed).toBeTruthy(); if (!validationResult.passed) { console.error('Schema errors:', validationResult.errors); } });
Debug
Known issues
gotchavalidateSchema requires 'fixtures' object; passing undefined or null will cause runtime error
fix
Provide the test's fixtures argument, or use an empty object {} if not needed.
affects: >=1.0.0
gotchaThe 'path' parameter in validateSchema is for JSONPath, not file path. Common confusion with file paths.
fix
Use dot-notation or bracket notation JSONPath, e.g., '$.data.users'.
affects: >=1.0.0
gotchaEnvironment variable DISABLE_SCHEMA_VALIDATION must be set to 'true' (string) to disable; boolean true won't work in Playwright config
fix
Set process.env.DISABLE_SCHEMA_VALIDATION = 'true' before tests.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: fixtures.testInfo is not a function
The 'fixtures' object passed to validateSchema is not the Playwright test fixtures but an incorrect object
fix
Pass the first argument of your test callback, which is the 'fixtures' object provided by Playwright.
Cannot find module 'playwright-ajv-schema-validator'
Package not installed or not resolvable due to ESM/CJS mismatch
fix
Ensure package is installed: npm install playwright-ajv-schema-validator. If using CJS, switch to ESM or use dynamic import.
Error: schema must be an object or array
The 'schema' argument passed to validateSchema is not a valid JSON schema object
fix
Ensure schema is a parsed JSON object, not a string or undefined.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies
playwrightrequiredPeer dependency: requires Playwright for API request handling and test runner
ajvrequiredUsed for JSON schema validation engine
core-ajv-schema-validatorrequiredCore validation logic wrapper
Agent activity
6 hits · last 30 days
node
6
Resources