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

swagvali

v1.0.1

Published

A module to build validators for OpenApi (Swagger) Request parameters and Response objects

Downloads

9

Readme

swagvali

A module to build validators for Swagger(OpenApi) Request parameters and Response objects.

Tested on APIs.guru

Swagvali uses is-my-json-valid JSON schema validator as the default validation tool. However, Joi schema validator can be used instead of the json schema validator, by setting the option joischema.

Install

npm i swagvali

Usage

    const Swagvali = require('swagvali');
    const api = 'http://petstore.swagger.io/v2/swagger.json';
    Swagvali(api);

Promise response:

    Swagvali(api, {
        path: '/pet/findByStatus',
        operation: 'get'
    }).then(validators => {
        //Parameter validator --> validators.parameters.
        validators.parameters.forEach(validator => {
            //Parameter spec --> validator.spec.
            //Validator function --> validator.validate
        });
        //Response validator --> validators.responses.
        Object.keys(validators.responses).forEach(response => {
            //Response spec --> validators.responses[response].spec.
            //Validator function --> validators.responses[response].validate
        });
    }).catch(error => {
        //Fail on error.
        Assert.ifError(error);
    });

Callback style:

    Swagvali(api, {
        path: '/pet/findByStatus',
        operation: 'get'
    }, (error, validators) => {
        //Fail on error.
        Assert.ifError(error);
        //Parameter validator --> validators.parameters.
        validators.parameters.forEach(validator => {
            //Parameter spec --> validator.spec.
            //Validator function --> validator.validate
        });
        //Response validator --> validators.responses.
        Object.keys(validators.responses).forEach(response => {
            //Response spec --> validators.responses[response].spec.
            //Validator function --> validators.responses[response].validate
        });
    });

API

Swagvali(api, [options], [cb])

  • api - (Object) or (String) or (Promise) - (required) - api can be one of the following.

    • A relative or absolute path to the Swagger api document.
    • A URL of the Swagger api document.
    • The swagger api Object
    • A promise (or a thenable) that resolves to the swagger api Object
  • options - (Object) - (optional) - Additional options to create the mock generator.

    • validated - (Boolean) - (optional) - Set this property to true if the api is already validated against swagger schema and already dereferenced all the $ref. This is really useful to generate validators for parsed api specs. Default value for this is false and the api will be validated using swagger-parser validate.
    • path - (String) - (optional) - The path for which the validators need to be generated. For example /pet/findByStatus, /pet etc. If a path is not specified, validators will be generated for all the paths defined by the swagger api. If you are setting a path option, you should set an operation as well.
    • operation - (String) - (optional) - The operation for which the validators need to be generated. For example get, post etc. If operation is not specified, validators will be generated for all the operations defined by the swagger api. If you are setting an operation option, you should set a path as well.
    • parameters - (Boolean) - (optional) - Set to false if you don't need to build validators for parameters. Default values is true.
    • responses - (Boolean) - (optional) - Set to false if you don't need to build validators for responses. Default values is true.
    • joischema - (Boolean) - (optional) - Set to true if you want to use Joi schema based Validators. Swagvali uses enjoi - The json to joi schema converter - to build the validator functions, if joischema option is set to true.

    By default swagvali uses is-my-json-valid JSON schema validator (when joischema option is set as false).

  • callback - (Function) - (optional) - function (error, mock). If a callback is not provided a Promise will be returned.

Validators

The Swagvali api generates a validator object for each parameter or response definition. The validator object has,

  • spec - (Object) - The spec of the parameter or the response.

  • validate - (Function) - function (data) - The validate function that accepts the data to be validated. The validate function uses is-my-json-valid validator by default. If the joischema option was set to true, the validator function uses Joi schema validator.

  • joischema - (Object) - The Joi schema object, if the joischema option was set as true. Otherwise this will be undefined.

validate response

The validate function response has,

  • status - (Boolean) - The status of the validation.
  • errors - (Array) - The list of errors for the validation.
  • value - The validated value with any type coercions/conversions and other modifiers applied. (the input is left unchanged). value can be incomplete, if validation failed.