Skip to content

Latest commit

 

History

History
116 lines (94 loc) · 3.58 KB

README.md

File metadata and controls

116 lines (94 loc) · 3.58 KB

License Node Version NPM Version Build Status Test Coverage Downloads Dependency Status

Ajv-OAI

OpenAPI's JsonSchema validator, Powered by Ajv.

Support formats in dataTypeFormat, except binary format.

Installation

npm install ajv-oai --save

Usage

OpenAPI v3.1

const ajv = new AjvOAI({ oasVer: '3.1' });

OpenAPI v3.0 or v2.0

const ajv = new AjvOAI();

// OR
const ajv = new AjvOAI({oasVer: '2.0'});

Default Options

ajv-oai sets followings as ajv's default options, you can override this options as need.

{
  coerceTypes: false,
  useDefaults: true,
}

Tips

BigInt is not supported by ajv, so int64 validation will be invalid when value is too big or too small. You can get more info from Should 'integer' type validate bigint ?

Example

const ajv = new AjvOAI();

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