# express-openapi-coercion

> Express middleware to coerce request properties according to an openapi parameter list.

Latest version **0.5.4** (published 2018-06-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-openapi-coercion
pnpm add express-openapi-coercion
yarn add express-openapi-coercion
bun add express-openapi-coercion
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.4 |
| Published | 2018-06-23 |
| First published | 2016-01-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 17.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 920 |
| Author | Joseph Spencer |
| Maintainers | jsdevel, kogo |
| Keywords | openapi, coercion, jsonschema, express, middleware |

## Links

- npm: https://www.npmjs.com/package/express-openapi-coercion
- Repository: https://github.com/kogosoftwarellc/open-api/tree/master/packages/express-openapi-coercion
- Homepage: https://github.com/kogosoftwarellc/open-api/tree/master/packages/express-openapi-coercion#readme
- Issues: https://github.com/kogosoftwarellc/open-api/issues
- npm.io page: https://npm.io/package/express-openapi-coercion

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.5.4 (latest) — 2018-06-23
- 0.5.3 — 2018-06-19
- 0.5.2 — 2018-05-11
- 0.5.1 — 2018-05-05
- 0.5.0 — 2016-12-07
- 0.4.1 — 2016-03-09
- 0.4.0 — 2016-02-18
- 0.3.3 — 2016-01-05
- 0.3.2 — 2016-01-05
- 0.3.1 — 2016-01-04
- 0.3.0 — 2016-01-04

## README

# Note: This has been deprecated in favor of openapi-request-coercer

Moving forward you can use [openapi-request-coercer](https://github.com/kogosoftwarellc/open-api/tree/master/packages/openapi-request-coercer).  See [express-openapi](https://github.com/kogosoftwarellc/open-api/tree/master/packages/express-openapi) for an example.

# express-openapi-coercion [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coveralls Status][coveralls-image]][coveralls-url]
> Express middleware to coerce request properties according to an openapi parameter list.

## Highlights

* Effortlessly coerce header, path, query and formData request properties to defined types in
an openapi parameters list.
* Handles array types.
* Performant.
* Extensively tested.
* Small footprint.
* Currently supports openapi 2.0 (a.k.a. swagger 2.0) parameter lists.
* Supports _collectionFormat_ for _formData array_ parameters.

## Example

See `./test/data-driven/*.js` for more examples.

```javascript
var app = require('express')();
var coerce = require('express-openapi-coercion')({
  parameters: [
    {
      in: 'query',
      type: 'integer',
      name: 'foo',
      required: true
    }
  ]
});

app.get('/something', coerce, function(req, res) {
  // GET /something?foo=5
  console.log(req.query.foo); //=> 5
});
```

## Boolean parameters

By default a boolean parameter is coerced to:
* _false_ if the input value is "false"
* _true_ for any other input value

This behaviour is makes sense if you do not wish to validate the input fields.

If the opposite is the case, the behaviour can be changed by setting the "x-express-openapi-coercion-strict" to _true_ on the parameter in question as illustrated below.

```javascript
var app = require('express')();
var coerce = require('express-openapi-coercion')({
  parameters: [
    {
      in: 'query',
      type: 'boolean',
      name: 'foo',
      required: true,
      "x-express-openapi-coercion-strict": true
    }
  ]
});
```

Doing so will result in the boolean parameter being coerced to:
* _false_ if the input value is "false" (in any casing)
* _true_ if the input value is "true" (in any casing)
* _null_ for any other input value

## API

### coerce(args)
#### args.parameters

An array of openapi parameters.

## LICENSE
``````
The MIT License (MIT)

Copyright (c) 2016 Kogo Software LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
``````

[downloads-image]: http://img.shields.io/npm/dm/express-openapi-coercion.svg
[npm-url]: https://npmjs.org/package/express-openapi-coercion
[npm-image]: http://img.shields.io/npm/v/express-openapi-coercion.svg

[travis-url]: https://travis-ci.org/kogosoftwarellc/open-api
[travis-image]: http://img.shields.io/travis/kogosoftwarellc/open-api.svg

[coveralls-url]: https://coveralls.io/r/kogosoftwarellc/open-api
[coveralls-image]: http://img.shields.io/coveralls/kogosoftwarellc/open-api/master.svg

---
_Source: https://npm.io/package/express-openapi-coercion · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
