micro-superstruct
A Superstruct wrapper for Micro to validate request body and query parameters.
Install
npm install micro-superstruct
Usage
micro-superstruct exports a validate function that allows you to create API validators from any Struct
:
const object string number = const json send = const validate = // define a Superstruct `Struct`const Unicorn = // create a validatorconst validator = // write your Micro APIconst handler = async { const body = await } // export validated servicemoduleexports =
Requests that fail validation will return a 400 error along with a Superstruct validation message:
API
validate(config)
Returns a validator function that can be used to validate a Micro handler.
config
Type: Struct | object
Passing a Struct
directly will configure request body validation using the provided validator.
Passing an object
allows for validation of both the request body and query string. Both are optional.
// body validation // body and/or query validation
Request Properties
micro-superstruct attaches validated body
and/or query
objects to the request object for use by the API handler:
const validator = const handler = async { const body query = req }
License
MIT © Brandon Smith