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 🙏

© 2024 – Pkg Stats / Ryan Hefner

cypress-ajv-schema-validator

v1.1.1

Published

Lightweight API schema validator for Plain JSON schemas, Swagger schema documents, or OpenAPI schema documents using Ajv JSON Schema Validator

Downloads

694

Readme

cypress-ajv-schema-validator

A Cypress plugin for API testing to validate the API response against Plain JSON schemas, Swagger documents, or OpenAPI documents using Ajv JSON Schema validator.

Overview

For a detailed guide on how to use this plugin, check out my blog post, "CYPRESS-AJV-SCHEMA-VALIDATOR Plugin: The Brave Vigilante for Your API Contracts," on my Cypress blog. There you'll find comprehensive information about setting up and getting the most out of this plugin.

Main Features

  • Cypress command cy.validateSchema() and utility function validateSchema() to report JSON Schema validation errors in the response obtained from any network request with cy.request().

  • The command cy.validateSchema() is chainable and returns the original API response yielded.

  • Schema is provided as a JSON object, that could come from a Cypress fixture.

  • It uses the Ajv JSON Schema Validator as its core engine.

  • Support schemas provided as plain JSON schema, OpenAPI 3.0.1 schema document and Swagger 2.0 schema document.

  • Provides in the Cypress log a summary of the schema errors as well as a list of the individual errors in the schema validation.

  • By clicking on the summary of schema errors in the Cypress log, the console will output:

    • Number of schema errors.
    • Full list of schema errors as provided by Ajv.
    • A nested tree view of the validated data, clearly indicating the errors and where they occurred in an easy-to-understand format.

 

⭐⭐⭐⭐⭐ Note: This plugin complements Filip Hric cypress-plugin-api and Gleb Bahmutov @bahmutov/cy-api plugins to perform JSON schema validations.

Example usage with these two API plugins: cy.api('/users/1').validateSchema(schema);

To see an example of cypress-ajv-schema-validator working with the cypress-plugin-api plugin for the Swagger PetStore API, check the sample test test-petstore-with-cypress-plugin-api.js.

 

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.0.1 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 cypress-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 cypress-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 (cypress-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 cypress-ajv-schema-validator

Compatibility

  • Cypress 12.0.0 or higher
  • Ajv 8.16.0 or higher
  • ajv-formats 3.0.1 or higher

Configuration

Add the following lines either to your cypress/support/commands.js to include the custom command and function globally, or directly in the test file that will host the schema validation tests:

For cy.validateSchema() Custom Command

import 'cypress-ajv-schema-validator';

For validateSchema() Function

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

API Reference

Custom Commands

cy.validateSchema(schema, path)

Validates the response body against the provided schema.

Parameters
  • schema (object): The schema to validate against. Supported formats are plain JSON schema, Swagger, and OpenAPI documents.
  • path (object, optional): This second parameter only applies to Swagger or OpenAPI documents. It represents the path to the schema definition in a Swagger or OpenAPI document and is determined by three properties:
    • endpoint (string, optional): The endpoint path.
    • method (string, optional): The HTTP method. Defaults to 'GET'.
    • status (integer, optional): The response status code. If not provided, defaults to 200.
Returns
  • Cypress.Chainable: The response object wrapped in a Cypress.Chainable.
Throws
  • Error: If any of the required parameters are missing or if the schema or schema definition is not found.

Example providing a Plain JSON schema:

cy.request('GET', 'https://awesome.api.com/users/1')
  .validateSchema(schema);

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

cy.request('GET', 'https://awesome.api.com/users/1')
  .validateSchema(schema, { endpoint: '/users/{id}', method: 'GET', status: 200 });

Using the path defined by { endpoint, method, status }, the plugin will automatically take the schema $ref for that definition, find it in the components section, and use it in the schema validation.

Path to the schema definition

Functions

validateSchema(data, schema, path)

Validates the given data against the provided schema.

Parameters
  • data (any): The data to be validated.
  • schema (object): The schema to validate against.
  • path (object, optional): The path object to the schema definition in a Swagger or OpenAPI document.
    • endpoint (string, optional): The endpoint path.
    • method (string, optional): The HTTP method. Defaults to 'GET'.
    • status (integer, optional): The response status code. Defaults to 200.
Returns
  • Array: An array of validation errors, or null if the data is valid against the schema.
Throws
  • Error: If any of the required parameters are missing or if the schema or schema definition is not found.

Example providing a Plain JSON schema:

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

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

cy.request('GET', 'https://awesome.api.com/users/1').then(response => {
  const data = response.body
  const errors = validateSchema(data, schema, { endpoint: '/users/{id}', method: 'GET', status: 200 });
  expect(errors).to.have.length(0); // Assertion to ensure no validation errors
});

Usage Examples

For detailed usage examples, check the document USAGE-EXAMPLES.md.

The examples included are for using:

  • cy.validateSchema() command with a Plain JSON schema.

  • cy.validateSchema() command with an OpenAPI 3.0.1 schema document.

  • cy.validateSchema() command with a Swagger 2.0 schema document.

  • validateSchema() function with an OpenAPI 3.0.1 schema document.

  • cy.validateSchema() command in conjunction with cy.api() from the cypress-plugin-api or @bahmutov/cy-api plugins.

Validation Results

Here are some screenshots of schema validation tests run in Cypress.

Test Passed

When a test passes, the Cypress log will show the message: "✔️ PASSED - THE RESPONSE BODY IS VALID AGAINST THE SCHEMA.".

Test Passed

Test Failed

When a test fails, the Cypress log will show the message: "❌ FAILED - THE RESPONSE BODY IS NOT VALID AGAINST THE SCHEMA"; indicating the total number of errors: (Number of schema errors: N).

Also, the Cypress log will show an entry for each of the individual schema validation errors as provided by Ajv. The errors that correspond to missing fields in the data validated are marked with the symbol 🗑️, and the rest of the errors with the symbol 👉.

Test Failed Overview

Detailed Error View in the Console

If you open the Console in the browser DevTools, and click on the summary line for the schema validation error in the Cypress log, the console will display detailed information about all the errors. This includes:

  • The total number of errors
  • The full list of errors as provided by the Ajv.
  • A user-friendly view of the validated data, highlighting where each validation error occurred and the exact reason for the mismatch.

Test Failed Details

Test Failed with More than 10 Errors

When there are more than 10 schema validation errors, the Cypress log will show only the first 10 and, at the end of the list, an additional line indicating "...and N more errors.".

Test Failed Many Errors

Detailed Error View in the Console

In this case, clicking on the summary line for the schema validation error in the Cypress log will also display: the total number of errors, the full list of errors as provided by Ajv, and the user-friendly view of the schema mismatches, making it easy to understand where the errors occurred.

Error Details in Console

More Errors in the Console

When clicking on the "...and N more errors." line in the Cypress log, the browser console will show additional details for the errors grouped under that entry as provided by Ajv.

More Errors in Console

License

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

Changelog

[1.1.1]

  • Added details to documentation.

[1.1.0]

  • Added GitHub CI/CD workflow.

[1.0.0]

  • Initial release.

External references