Registry / testing / core-ajv-schema-validator

core-ajv-schema-validator

JSON →
library1.0.0jsnpmunverified

A lightweight package (v1.0.0, released 2024) for API testing that validates JSON responses against plain JSON schemas, Swagger 2.0, or OpenAPI 3 documents using the Ajv validator (v8.17.1+). It extracts the correct schema for a given endpoint method and status from complex API documents, providing both raw Ajv errors and user-friendly mismatch reports. Differentiates from bare Ajv by handling full OpenAPI/Swagger documents and simplifying setup for test frameworks like Cypress and Playwright.

npm install core-ajv-schema-validator
INSTALL
IMPORT
SIG · CORE-AJV-SCHEMA-VA
C
core-ajv-schema-validator
testingjavascriptv1.0.0
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 'core-ajv-schema-validator'
const { validateSchema } = require('core-ajv-schema-validator')
TypeScript/ESM import is recommended. CJS require is supported but may lack type definitions in some environments.
default import
import coreAjvSchemaValidator from 'core-ajv-schema-validator'
const coreAjvSchemaValidator = require('core-ajv-schema-validator')
Default import exports an object with validateSchema as a method, but named import is preferred.
types
import type { PathObject } from 'core-ajv-schema-validator'
import { Path } from 'core-ajv-schema-validator'
PathObject is the TypeScript interface for the path parameter. Only available since v1.0.0.

Shows basic usage of validateSchema with a plain JSON schema and logging result.

import { validateSchema } from 'core-ajv-schema-validator'; import Ajv from 'ajv'; import addFormats from 'ajv-formats'; const ajv = new Ajv({ allErrors: true, strict: false }); addFormats(ajv); const data = { name: 'John', age: 30 }; const schema = { type: 'object', properties: { name: { type: 'string' }, age: { type: 'number' } }, required: ['name', 'age'] }; // Validate against plain JSON schema const result = validateSchema(data, schema); console.log(result.valid); // true or false console.log(result.errors); // array of error objects if invalid
Debug
Known issues
gotchaThe package creates its own Ajv instance with options { allErrors: true, strict: false }. If you need different options, you must fork or modify the source.
fix
Not configurable; consider wrapping the function or using a custom validation approach.
affects: >=1.0.0
breakingAjv version 8.17.1 or higher is required. Using older Ajv versions may cause incompatibility.
fix
Run npm install ajv@latest ajv-formats@latest
affects: >=1.0.0
gotchaWhen using Swagger or OpenAPI documents, the path parameter is required and must include endpoint and method (and optionally status). Omitting it will cause validation to fail silently or throw an error.
fix
Always provide a valid path object with endpoint and method.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: ajv.addFormat is not a function
ajv-formats is not installed or initialized.
fix
npm install ajv-formats && addFormats(ajv) before using validateSchema
Cannot read properties of undefined (reading 'properties')
Path parameter is missing when using an OpenAPI/Swagger document.
fix
Provide a path object: { endpoint: '/users', method: 'get', status: 200 }
Error: schema must be an object
Passed null or non-object as schema.
fix
Ensure schema is a valid JSON object (not a string or undefined).
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
ajvrequiredCore validation engine
ajv-formatsrequiredAdds format validation support (required by Ajv)
Agent activity
5 hits · last 30 days
node
4
OpenAI (training)
1
Resources
core-ajv-schema-validator — npm install core-ajv-schema-validator · libregistry