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

lambda-openapi-validator

v0.0.4

Published

Input validation using OpenAPI 3.0 definitions and ajv

Downloads

14

Readme

lambda-openapi-validator

NPM Version NPM Downloads Known Vulnerabilities Apache 2.0 License

This package provides data validation within a Lambda function according to a Swagger/OpenAPI definition. It uses Ajv under the hood for validation.

Table of Contents

Installation

Install using the node package registry:

npm install --save lambda-openapi-validator

Then import the module in your code:

import OpenApiValidator from 'lambda-openapi-validator'

API

lambda-openapi-validator.init(openapiSchema, options)

Initialize using a swagger definition. The function executes synchronously and does not return anything.

  • openapiSchema: The OpenAPI definition.
  • options: Additional options (see below).

Options

Options currently supported:

  • formats - Array of formats that can be added to ajv configuration, each element in the array should include name and pattern.

    formats: [
      { name: 'double', pattern: /\d+\.(\d+)+/ },
      { name: 'int64', pattern: /^\d{1,19}$/ },
      { name: 'int32', pattern: /^\d{1,10}$/ },
    ]
  • keywords - Array of keywords that can be added to ajv configuration, each element in the array can be either an object or a function. If the element is an object, it must include name and definition. If the element is a function, it should accept ajv as its first argument and inside the function you need to call ajv.addKeyword to add your custom keyword

  • beautifyErrors- Boolean that indicates if to beautify the errors, in this case it will create a string from the Ajv error.

    • Examples:
      • query/limit should be <= 100 - query param
      • path/petId should NOT be shorter than 3 characters - path param not in format
      • body/[0].test.field1 should be string - Item in an array body
      • body/test should have required property 'field1' - nested field
      • body should have required property 'name' - Missing field in body

    You can see more examples in the tests.

  • ajvConfigBody - Object that will be passed as config to new Ajv instance which will be used for validating request body. Can be useful to e. g. enable type coercion (to automatically convert strings to numbers etc). See Ajv documentation for supported values.

  • ajvConfigParams - Object that will be passed as config to new Ajv instance which will be used for validating request body. See Ajv documentation for supported values.

Important Notes

Schema Objects

It is important to set the type property of any Schema Objects explicitly to object. Although it isn't required in the OpenAPI specification, it is necessary in order for Ajv to work correctly.

Known Issues with OpenAPI 3

  • Inheritance with a discriminator is supported only if the ancestor object is the discriminator.
  • The discriminator support in the inheritance chain stops when getting to a child without a discriminator (a leaf in the inheritance tree), meaning a child without a discriminator cannot point to another child with a discriminator.

Running Tests

The tests use mocha, istanbul and mochawesome. Run them using the node test script:

npm test