Skip to content

dhruv4kubra/cypress-ajv-schema-validator

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

cypress-ajv-schema-validator

A Cypress plugin for validating the response body against JSON Schema, Swagger, and OpenAPI documents using Ajv.

Ajv JSON Schema Validator was chosen as the core engine because of its versatility, powerful validation capabilities, and excellent documentation. For more information on Ajv, visit Ajv official website.

Installation

npm install 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): 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
  • 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.

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.

Usage and Examples

Example: Using cy.validateSchema Command

Plain JSON Schema

describe('API Schema Validation with Plain JSON', () => {
  it('should validate the user data using plain JSON schema', () => {
    const schema = {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "age": { "type": "number" }
      },
      "required": ["name", "age"]
    };

    cy.request('GET', 'https://awesome.api.com/users/1')
      .validateSchema(schema)
      .then(response => {
        // Further assertions
      });
  });
});

OpenAPI 3.0.1

describe('API Schema Validation with OpenAPI 3.0.1', () => {
  it('should validate the user data using OpenAPI 3.0.1 schema', () => {
    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')
      .validateSchema(schema, path)
      .then(response => {
        // Further assertions
      });
  });
});

Swagger 2.0

describe('API Schema Validation with Swagger 2.0', () => {
  it('should validate the user data using Swagger 2.0 schema', () => {
    const schema = {
      "swagger": "2.0",
      "paths": {
        "/users/{id}": {
          "get": {
            "responses": {
              "200": {
                "schema": { "$ref": "#/definitions/User" }
              }
            }
          }
        }
      },
      "definitions": {
        "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')
      .validateSchema(schema, path)
      .then(response => {
        // Further assertions
      });
  });
});

Example: Using validateSchema Function

OpenAPI 3.0.1

import validateSchema from 'cypress-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 = validateSchema(response.body, schema, path);
      
      expect(errors).to.have.length(0); // Assertion to ensure no validation errors
    });
  });
});

Validation Results

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

Test Passed

Test Passed

Test Failed

Overview of Errors

Test Failed Overview

In the Cypress log, the number of errors is summarized, and the schema validation errors are listed in AJV format. Missing fields in the validated data are shown with the symbol ๐Ÿ—‘๏ธ, and the rest of the errors with the symbol ๐Ÿ‘‰.

Detailed Error View

Test Failed Details

When clicking on the summary line, the number of errors, the full list of errors in AJV format, and a view of the validated data indicating the errors in a user-friendly format are shown.

Test Failed with More than 10 Errors

Overview with More Errors

Test Failed Many Errors

At the end of the list of individual errors, a line "...and N more errors." is shown.

Error Details in Console

Error Details in Console

When clicking the summary line in the Cypress log, the error details are shown in the console.

More Errors in Console

More Errors in Console

When clicking on the "...and N more errors." line in the Cypress log, additional error details are shown in the console.

License

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

Changelog

[1.0.0]

  • Initial release.

About

Lightweight JSON Schema validator for Cypress using AJV

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%