https://github.com/touchifyapp/ajv-openapi
Ajv plugin to add Open API v3 data type formats support
https://github.com/touchifyapp/ajv-openapi
Last synced: 3 months ago
JSON representation
Ajv plugin to add Open API v3 data type formats support
- Host: GitHub
- URL: https://github.com/touchifyapp/ajv-openapi
- Owner: touchifyapp
- Created: 2020-04-25T01:05:52.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T04:22:40.000Z (over 3 years ago)
- Last Synced: 2025-10-20T05:06:59.995Z (8 months ago)
- Language: TypeScript
- Size: 927 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ajv-openapi
[](http://opensource.org/licenses/MIT)
[](https://npmjs.org/package/ajv-openapi)
[](https://npmjs.org/package/ajv-openapi)
[](https://travis-ci.org/touchifyapp/ajv-openapi)
[](https://coveralls.io/github/touchifyapp/ajv-openapi)
[](https://david-dm.org/touchifyapp/ajv-openapi)
[Ajv](https://github.com/epoberezkin/ajv) plugin that adds Open API v3 [data types](http://swagger.io/specification/#dataTypeFormat) (formats: int32, int64, float, double, byte) validation.
## Installation
```bash
npm install ajv ajv-openapi
```
## Usage
```javascript
const Ajv = require("ajv");
const openApi = require("ajv-openapi");
const ajv = openApi(new Ajv());
```
## Configuration for full OpenAPI compatibility
By default, the plugin only adds missing formats to Ajv validator.
To configure Ajv to be fully Open API v3 compliant, you should configure the plugin like this:
```javascript
const Ajv = require("ajv");
const openApi = require("ajv-openapi");
const ajvOptions = {
schemaId: "auto",
format: "full",
coerceTypes: true,
unknownFormats: "ignore",
useDefaults: true,
nullable: true
};
const openApiOptions = {
useDraft04: true
};
const ajv = openApi(
new Ajv(ajvOptions),
openApiOptions
);
```
## Examples
```javascript
console.log(ajv.compile({ type: "integer", format: "int32" })(2147483648));
console.log(ajv.compile({ type: "integer", format: "int32" })(-2147483649));
console.log(ajv.compile({ type: "integer", format: "int32" })(1.23));
console.log(ajv.compile({ type: "integer", format: "int32" })(123));
> false
> false
> false
> true
console.log(ajv.compile({ type: "integer", format: "int64" })(Number.MAX_VALUE));
console.log(ajv.compile({ type: "integer", format: "int64" })(Number.MIN_VALUE));
console.log(ajv.compile({ type: "integer", format: "int64" })(1.23));
console.log(ajv.compile({ type: "integer", format: "int64" })(123));
> false
> false
> false
> true
console.log(ajv.compile({ type: "number", format: "float" })(Number.MAX_VALUE));
console.log(ajv.compile({ type: "number", format: "float" })(Number.MIN_VALUE));
console.log(ajv.compile({ type: "number", format: "float" })(1.23));
console.log(ajv.compile({ type: "number", format: "float" })(123));
> false
> false
> true
> true
console.log(ajv.compile({ type: "number", format: "double" })(Number.MAX_VALUE));
console.log(ajv.compile({ type: "number", format: "double" })(Number.MIN_VALUE));
console.log(ajv.compile({ type: "number", format: "double" })(1.23));
console.log(ajv.compile({ type: "number", format: "double" })(123));
> true
> true
> true
> true
console.log(ajv.compile({ type: "string", format: "byte" })("MTIz"));
console.log(ajv.compile({ type: "string", format: "byte" })("abc"));
console.log(ajv.compile({ type: "string", format: "byte" })(1));
console.log(ajv.compile({ type: "string", format: "byte" })("5L2g5aW95ZWK"));
> true
> false
> false
> true
```