Giter Club home page Giter Club logo

cypress-jsonschema-validation's Introduction

Cypress JSON Schema Validation Plugin

Validate data payload against a given JSON Schema.

npm (scoped) npm (types) npm (licence)

Cypress Installation

yarn add @jc21/cypress-jsonschema-validation

Then in your cypress Plugins file:

const {JsonSchemaValidation} = require('@jc21/cypress-jsonschema-validation');

module.exports = (on, config) => {
    // ...
    on('task', JsonSchemaValidation(config));
    // ...
    return config;
};

Cypress Usage

describe('Basic API checks', () => {
    it('Should return a valid health payload', function () {
        cy.request('/healthz').then($response => {
            cy.task('validateJsonSchema', {
                data:           $response.body,
                verbose:        true,                     // optional, default: false
                schemaFile:     './testing/schema.json',  // path or full URL, see below
                // Or you can also define the schema inline:
                schema:         {
                    "type": "object",
                    "properties": {
                        "id": {
                            "type": "integer"
                        }
                    }
                },
            }).should('equal', null);
        });
    });
});

The Schema

You can provide the schemaFile or the schema. One of these options is required and using both will cause an error.

The schemaFile can either be a file on disk or a URL. When using a file on disk and due to the fact that this plugin runs on the Cypress Backend, the location of the file must be defined as either the full path on disk or relative path to the running of the cypress command.

Options

Option Description Optional Default
schemaFile The location of the schema file to use for validation when schema provided
schema Schema to use for validation. Cannot be used with schemaFile when schemaFile provided
data The payload to validate
verbose Console.log more info when validation fails true false

Compiling Source

yarn install
yarn build
yarn test

cypress-jsonschema-validation's People

Contributors

dependabot[bot] avatar jc21 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

cypress-jsonschema-validation's Issues

Unable to verify api response with Json schema

I am trying to use your plugin in cypress for validating api response but I am unable to figure out that why it is always returning should("equal",null).
I want to capture the response of the validator that whether it is validating the schema or not?
Do we need anything else in this to verify the schema?
Following is the sample code for your reference:
it('Should return a valid payload', function () {
cy.api({
url: '' + Cypress.env('baseUrlapi') + '/api/generalTest,
headers: {
"Content-Type": "application/json;charset=UTF-8", "Origin": "" + Cypress.config().baseUrl + ""
}
}).then($response => {
cy.task('validateJsonSchema', {
data: $response.body,
verbose: true, // optional, default: false
// // Or you can also define the schema inline:
schema: {
"type": "object",
"properties": {
"status": {
"type": "integer"
},
"message": {
"type": "string"
},
"body": {
"type": "object"
}
}
},
}).should('equal', null);
});
});

it always give null to equal and it is not validating the actual schema.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.