Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/brandon93s/micro-superstruct
A Superstruct wrapper for Micro to validate your request body and query parameters
https://github.com/brandon93s/micro-superstruct
micro superstruct validation
Last synced: 3 months ago
JSON representation
A Superstruct wrapper for Micro to validate your request body and query parameters
- Host: GitHub
- URL: https://github.com/brandon93s/micro-superstruct
- Owner: brandon93s
- License: mit
- Created: 2018-02-03T16:05:16.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-09-07T16:47:31.000Z (about 4 years ago)
- Last Synced: 2024-07-06T06:48:00.224Z (4 months ago)
- Topics: micro, superstruct, validation
- Language: JavaScript
- Size: 83 KB
- Stars: 16
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
- awesome-micro - micro-superstruct - Superstruct wrapper for Micro enabling validation of request body and query parameters. (Modules / Wrappers)
README
# micro-superstruct [![Build Status](https://travis-ci.org/brandon93s/micro-superstruct.svg?branch=master)](https://travis-ci.org/brandon93s/micro-superstruct)
> A [Superstruct](https://github.com/ianstormtaylor/superstruct) wrapper for [Micro](https://github.com/zeit/micro) to validate request body and query parameters.
## Install
```shell
npm install micro-superstruct
```## Usage
micro-superstruct exports a validate function that allows you to create API validators from any `Struct`:
```js
const {object, string, number} = require('superstruct')
const {json, send} = require('micro')
const validate = require('micro-superstruct')// define a Superstruct `Struct`
const Unicorn = object({
name: string(),
age: number(),
color: string()
})// create a validator
const validator = validate(Unicorn)// write your Micro API
const handler = async (req, res) => {
const body = await json(req)
send(res, 200, body)
}// export validated service
module.exports = validator(handler)
```Requests that fail validation will return a 400 error along with a Superstruct validation message:
![Error](demo.png)
## 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.
```js
// body validation
validate(object({}))// body and/or query validation
validate({
body: object({}),
query: object({})
})
```## Request Properties
micro-superstruct attaches validated `body` and/or `query` objects to the request object for use by the API handler:
```js
const validator = validate(Unicorn)const handler = async (req, res) => {
const {body, query} = req
send(res, 200, body)
}
```## License
MIT © [Brandon Smith](https://github.com/brandon93s)