Skip to content

Ajv plugin to add Open API v3 data type formats support

Notifications You must be signed in to change notification settings

touchifyapp/ajv-openapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ajv-openapi

License NPM version NPM download Build Status Test Coverage Dependency Status

Ajv plugin that adds Open API v3 data types (formats: int32, int64, float, double, byte) validation.

Installation

npm install ajv ajv-openapi

Usage

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:

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

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

About

Ajv plugin to add Open API v3 data type formats support

Resources

Stars

Watchers

Forks

Packages

No packages published