npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

core-ajv-schema-validator

v1.0.0

Published

Lightweight package for API testing to validate API responses against plain JSON schemas, Swagger documents, or OpenAPI documents using the Ajv JSON Schema validator. It can be used directly in any project, but also serves as the core engine for schema va

Readme

core-ajv-schema-validator

Plugin for API testing to validate API responses against plain JSON schemas, Swagger documents, or OpenAPI documents using the Ajv JSON Schema validator. It can be used directly in any project, but also serves as the core engine for schema validation in the cypress-ajv-schema-validator and playwright-ajv-schema-validator.

Overview

Main Features

  • Function validateSchema() to report JSON Schema validation errors in the response obtained from any network request.

  • The schema is provided as a JSON object and supports: plain JSON schema, OpenAPI 3 schema document and Swagger 2.0 schema document.

  • Provides the full list of errors as delivered by the Ajv, and a user-friendly view of the mismatches between the validated data and the JSON schema, highlighting where each validation error occurred and the exact reason for the mismatch

  • Relies on the Ajv JSON Schema Validator as its powerful core engine for schema validation.

About JSON Schemas and Ajv JSON Schema Validator

JSON Schema

JSON Schema is a hierarchical, declarative language that describes and validates JSON data.

OpenAPI 3 and Swagger 2.0 Schema Documents

The OpenAPI Specification (formerly Swagger Specification) are schema documents to describe your entire API (in JSON format or XML format). So a schema document will contain multiple schemas, one for each supported combination of Endpoint - Method - Expected Response Status (also called path) by that API.

Ajv JSON Schema Validator

AJV, or Another JSON Schema Validator, is a JavaScript library that validates data objects against a JSON Schema structure.

It was chosen as the core engine of the core-ajv-schema-validator plugin because of its versatility, speed, capabilities, continuous maintenance, and excellent documentation. For more information on Ajv, visit the Ajv official website.

Ajv supports validation of the following schema formats: JSON Schema, OpenAPI 3.0.1 specification, and Swagger 2.0 specification. However, Ajv needs to be provided with the specific schema to be validated for an endpoint, method, and expected response; it cannot process a full OpenAPI 3.0.1 or Swagger 2.0 schema document by itself.

The core-ajv-schema-validator plugin simplifies this by obtaining the correct schema definition for the endpoint you want to test. You just need to provide the full schema document (OpenAPI or Swagger) and the path to the schema definition of the service you want to validate for your API (Endpoint - Method - Expected Response Status).

Note: The Ajv instance used in this plugin (core-ajv-schema-validator) is configured with the options { allErrors: true, strict: false } to display all validation errors and disable strict mode.

 

Installation

npm install -D core-ajv-schema-validator

Compatibility

  • Ajv 8.17.1 or higher
  • ajv-formats 3.0.1 or higher

API Reference

validateSchema(data, schema, [path])

Function that validates the given data against the provided schema.

Parameters

  • data (required)

    • Type: any
    • Description: The data to be validated.
  • schema (required)

    • Type: object
    • Description: The schema to validate against. Supported formats are:
  • path (optional)

    • Type: object
    • Description: The path object to the schema definition in a Swagger or OpenAPI document. Not required if using a plain JSON schema.
      • path.endpoint (required for Swagger/OpenAPI documents)
        • Type: string
        • Description: The endpoint path.
      • path.method (optional)
        • Type: string
        • Description: The HTTP method. Defaults to 'GET' if not provided.
      • path.status (optional)
        • Type: integer
        • Description: The response status code. Defaults to 200 if not provided.
  • issuesStyles (optional)

    • Type: object
    • Description: An object with the icons used to flag the schema issues. If not provided, it will use the default icons defined in the plugin. Includes the following properties:
      • iconPropertyError: The icon used to flag type errors in the data.
      • iconPropertyMissing: The icon used to flag missing properties in the data.

Returns

An object containing the following properties:

  • errors

    • Type: array
    • Description: An array of validation errors as provided by Ajv, or null if the data is valid against the schema.
  • dataMismatches

    • Type: object
    • Description: The original response data with all schema mismatches directly flagged.

Throws

  • Error: If any of the required parameters are missing or if the schema or schema definition cannot be found.

Example providing a Plain JSON schema:

  //...
  const data = response.body
  const { errors, dataMismatches } = validateSchema(data, schema);
  expect(errors).to.have.length(0); // Assertion to ensure no validation errors
  console.log(dataMismatches)
  //...

Example providing an OpenAPI 3.0.1 or Swagger 2.0 schema documents, a path to the schema definition, and issuesStyles:

  const issuesStyles = {
      iconPropertyError: '🔸',
      iconPropertyMissing: '🔴'
  }
  const data = response.body

  const { errors, dataMismatches } = validateSchema(data, schema, { endpoint: '/users/{id}', method: 'GET', status: 200 }, issuesStyles);

  expect(errors).to.have.length(0); // Assertion to ensure no validation errors
  console.log(dataMismatches)

Usage Examples

Example: Using validateSchema Function in Cypress Test Framework

OpenAPI 3.0.1

import validateSchema from 'core-ajv-schema-validator';

describe('API Schema Validation Function', () => {
  it('should validate the user data using validateSchema function', () => {
    const schema = {
      "openapi": "3.0.1",
      "paths": {
        "/users/{id}": {
          "get": {
            "responses": {
              "200": {
                "content": {
                  "application/json": {
                    "schema": { "$ref": "#/components/schemas/User" }
                  }
                }
              }
            }
          }
        }
      },
      "components": {
        "schemas": {
          "User": {
            "type": "object",
            "properties": {
              "name": { "type": "string" },
              "age": { "type": "number" }
            },
            "required": ["name", "age"]
          }
        }
      }
    };

    const path = { endpoint: '/users/{id}', method: 'GET', status: 200 };

    cy.request('GET', 'https://awesome.api.com/users/1').then(response => {
      const { errors, dataMismatches } = validateSchema(response.body, schema, path);
      
      expect(errors).to.have.length(0); // Assertion to ensure no validation errors
      console.log(dataMismatches)
    });
  });
});

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Changelog

[1.0.0]

Support

If you'd like to support my work, consider buying me a coffee or contributing to a training session, so I can keep learning and sharing cool stuff with all of you. Thank you for your support!