lambda-openapi-validator
v0.0.4
Published
Input validation using OpenAPI 3.0 definitions and ajv
Downloads
6
Maintainers
Readme
lambda-openapi-validator
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 toajv
configuration, each element in the array should includename
andpattern
.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 toajv
configuration, each element in the array can be either an object or a function. If the element is an object, it must includename
anddefinition
. If the element is a function, it should acceptajv
as its first argument and inside the function you need to callajv.addKeyword
to add your custom keywordbeautifyErrors
- 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 parampath/petId should NOT be shorter than 3 characters
- path param not in formatbody/[0].test.field1 should be string
- Item in an array bodybody/test should have required property 'field1'
- nested fieldbody should have required property 'name'
- Missing field in body
You can see more examples in the tests.
- Examples:
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