2.0.1 • Published 2 years ago

swagger-route-validator v2.0.1

Weekly downloads
146
License
Apache-2.0
Repository
github
Last release
2 years ago

  • Extremely fast and >10x more efficient than sway, the next fastest alternative
  • Zero dependencies :star:
  • Battle-tested by Fortune 500 companies
  • Supports most OpenAPI 3.0 features like $ref, $not, $anyOf, $allOf, $oneOf, etc.
  • Supports most common data formats like emails, ips, uuids, dates, etc.

Usage

This is an example of an express middleware, which you could easily implement if you have your routes' swagger definitions.

import validate from 'swagger-route-validator';

// Pass in the swagger spec for that route and return an express middleware
export default (routeSpec) => function validateRequest(req, res, next) {
    // Check validation errors
    const errors = validate(routeSpec, req);
    if (errors.length > 0) return res.status(400).json(errors);

    next();
};

If you don't know in advance what route definition to use on an inbound request, you can follow this example

Running tests

npm run test

License

Apache 2.0 - Frederic Charette