Socket
Socket
Sign inDemoInstall

@scoir/rest-swagger-validator

Package Overview
Dependencies
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@scoir/rest-swagger-validator

validate request/response json against swagger definitions


Version published
Maintainers
2
Created
Source

rest swagger validator

rest-swagger-validator is a module for use with one or more swagger definitions in order to validate that a request and/or response are valid.

the need

Swagger api specs creation/modification was added as the first step in any agile story. As part of our integration tests, we wanted to validate that any recorded api calls were valid responses to record and any actual api calls were valid requests.

our solution

Usage of the module could look something like this:

// integration spec
const swagga = require('rest-swagger-validator');
const swaggerFilePath = 'my-swagger.json';
var swag;

// validate that a response is valid before recording so that we do not test against a state that should never occur based on the api specification
const recordGet = (url, responseStatus, responseBody) => {
    const validationResult = swag.validateResponse(url, 'GET', responseStatus, responseBody);
    
    // a more thorough expectation should be used
    expect(validationResult).toBeUndefined();
    
    // record an expected GET request
}

// validate that any calls made from the browser pass the schema laid out in the swagger api spec
const validateGet = (url) => {
    const lastGetCall = myCallHistory.getLastGetCall(url);
    const validationResult = swag.validateRequest(url, 'GET', {
        query: lastGetCall.queryParameters,
        body: lastGetCall.body,
    });
    
    // a more thorough expectation should be used
    expect(validationResult).toBeUndefined();
}

// before we start testing, create a swagger validator instance for a specific swagger file
beforeAll(async () => {
    swag = await swagga.createFor(swaggerFilePath);
})

it('...', async () => {
    await recordGet('/some/url', 200, {
        field1: 'val1',
        field2: 'val2',
    });
    
    // load the page
    // assert some things happened on the page
    
    await validateGet('/some/url');
})

FAQs

Package last updated on 06 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc