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

swagger-lint-api

v1.2.0

Published

Linter for a Swagger JSON API spec

Downloads

28

Readme

About

Linter for a Swagger JSON API spec

Install

npm install --save swagger-lint-api

Usage

The library exposes validators to be used with an OpenAPI / Swagger JSON-formatted schema:

  1. Require the library
  2. Instantiate a new validator based on a schema
  3. Invoke validator methods to validate the schema

Available validators to be used

| Validator Class | Validator functions | Description | | --------------- | ----------------------------------- | ----------------------------------------------------------------------------------------- | | Description | descriptionHasNoLineBreaks | assert no line breaks such as \n exist in the string | | Description | descriptionHasNoTabs | assert no tab control character exist in the string | | Description | descriptionEndsWithString(string) | assert the string ends with specific string | | Description | descriptionCompliesWithFunction(fn) | pass a custom function which expects a value to return true or false for custom assertion | | Paths | has2xxResponses | assert all paths have 2xx HTTP responses | | Paths | has4xxResponses | assert all paths have 4xx HTTP responses | | Paths | has5xxResponses | assert all paths have 5xx HTTP responses |

Example

Using a JSON schema file you want to validate:

const {DescriptionValidator} = require('swagger-lint-api')

// since it's just a JSON document we can require it into a variable
// and pass on to the constructor call
const mySwaggerSchema = require('./swagger-schema.json')
const validator = new DescriptionValidator(mySwaggerSchema)

// validate
const result = validator.descriptionHasNoLineBreaks()
// check result.valid being true or false

Inline JSON validation:

const {DescriptionValidator} = require('swagger-lint-api')

const someJSON = {description: 'this \n has \nline-breaks'}
const validator = new DescriptionValidator(someJSON)

// validate for line breaks
const result = validator.descriptionHasNoLineBreaks()
// result.valid will be false

Using it as a linting for a project:

  1. Create a test file to run during lint / CI tests
  2. Assert for expected structure in the Swagger JSON file

See example reference:

const {PathsValidator} = require('swagger-lint-api')
const assert = require('assert')

const mySwaggerExample = {
  swagger: '2.0',
  host: 'api.superheroes.io',
  basePath: '/v2',
  paths: {
    '/superheroes': {
      get: {
        summary: 'Finds superheroes',
        produces: ['application/json'],
        responses: {
          '200': {
            description: 'successful operation',
            schema: {
              type: 'array',
              items: {
                $ref: '#/definitions/SuperHeroes'
              }
            }
          }
        }
      }
    }
  }
}

const validator = new PathsValidator(mySwaggerExample)
const actual = validator.has4xxResponses()
const expected = {valid: true}

assert.deepStrictEqual(actual, expected)
// will throw an error and print it on console
// due to mySwaggerExample object missing a
// 4xx response type

Contributing

Please consult CONTIRBUTING for guidelines on contributing to this project.

Author

swagger-lint-api © Liran Tal, Released under the Apache-2.0 License.