@keboola/middy-event-validator
v1.0.0
Published
Event validation middleware for Middy
Downloads
2
Keywords
Readme
Event Validation Middleware for Middy
Joi powered event validation middleware for Middy.
The middleware integrates Joi validation library to validate AWS Lambda events triggered by API Gateway.
Failed validations throw error of http-errors
instance with status code 422.
Usage
import middy from 'middy';
import eventValidator from '@keboola/middy-event-validator';
export const handler = middy(() => {
// ...
});
handler
.use(eventValidator({
path: {
id: Joi.string().required()
},
body: {
size: Joi.number().integer()
},
pagination: true
}));
The configuration accepts following options:
headers
- validation of HTTP headersbody
- validation of HTTP body in JSON formatpath
- validation of path parameters (shortcut for API Gateway'spathParameters
)query
- validation of query string (shortcut for API Gateway'squeryStringParameters
)pagination: true
- shortcut for two query string parametersoffset
andlimit
validated as numbers.- Default and maximum for
limit
is 1000. It can be changed by a definition:pagination: { max: 10 }
- Default and maximum for
The module also offers a set of validation shortcuts using class Validation
:
import middy from 'middy';
import eventValidator, { Validation } from '@keboola/middy-event-validator';
handler
.use(eventValidator({
path: {
id: Validation.string('id').required()
}
}));
List of available shortcuts:
getJoi()
- returns Joi instance for custom validationforbidden(param)
- setting of the parameter is forbiddenboolean(param)
- boolean valuestring(param)
- string. All string shortcuts promote trimming of trailing white spacesstringMaxLength(param, length)
- string with maximal lengthinteger(param)
object(param)
enum(param, values)
array(param)
arrayOfStrings(param)
All shortcuts create optional parameters, for required parameters add .required()
.