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

@scoir/rest-swagger-validator

v1.1.1

Published

validate request/response json against swagger definitions

Downloads

5

Readme

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');
})