Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/smartrecruiters/openapi-schemas-validator

Schema validators for api documentation in OpenAPI Specification 3.x format.
https://github.com/smartrecruiters/openapi-schemas-validator

oas3 openapi openapi3 validator

Last synced: about 1 month ago
JSON representation

Schema validators for api documentation in OpenAPI Specification 3.x format.

Awesome Lists containing this project

README

        

# @smartrecruiters/openapi-schemas-validator

[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Node.js Version][node-version-image]][node-version-url]
[![Licence][license-image]][license-url]
[![Build][travis-image]][travis-url]

Create schema validators for api documentation in OpenAPI Specification 3.x format.

## tv4

This module uses [tv4](https://www.npmjs.com/package/tv4) validator, additionally
configured with [tv4-formats](https://www.npmjs.com/package/tv4-formats).

This module uses tv4 [`validateMultiple`](https://www.npmjs.com/package/tv4#usage-3-multiple-errors) function,
with [`checkRecursive`](https://github.com/geraintluff/tv4#cyclical-javascript-objects) param always passed as `true`.

You can configure tv4 validator with following options:
* customFormats - will be passed to [`tv4.addFormat`](https://www.npmjs.com/package/tv4#addformatformat-validationfunction) function
* banUnknownProperties - will be passed to validating function ([The banUnknownProperties flag](https://www.npmjs.com/package/tv4#the-banunknownproperties-flag))

There is exported an `ErrorCodes` object containing mappings between error code numbers and error names that are generated by [tv4](https://www.npmjs.com/package/tv4) validator.

## OpenAPI Specification 3.0 specific features

Currently @smartrecruiters/openapi-schemas-validator supports `nullable` field.

Please refer to https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#fixed-fields-20.

## License

[MIT](LICENSE)

[npm-image]: https://img.shields.io/npm/v/@smartrecruiters/openapi-schemas-validator.svg
[npm-url]: https://www.npmjs.com/package/@smartrecruiters/openapi-schemas-validator
[downloads-image]: https://img.shields.io/npm/dm/@smartrecruiters/openapi-schemas-validator.svg
[downloads-url]: https://www.npmjs.com/package/@smartrecruiters/openapi-schemas-validator
[node-version-image]: https://img.shields.io/node/v/@smartrecruiters/openapi-schemas-validator.svg
[node-version-url]: https://nodejs.org/en/download/
[license-url]: https://github.com/smartrecruiters/openapi-schemas-validator/blob/master/LICENSE
[license-image]: https://img.shields.io/npm/l/@smartrecruiters/openapi-schemas-validator.svg
[travis-url]: https://travis-ci.org/smartrecruiters/openapi-schemas-validator
[travis-image]: https://api.travis-ci.org/smartrecruiters/openapi-schemas-validator.svg?branch=master
## API

### Modules


@smartrecruiters/openapi-schemas-validator


### Classes


SchemaValidator


### @smartrecruiters/openapi-schemas-validator

### SchemaValidator
**Kind**: global class

* [SchemaValidator](#SchemaValidator)
* [new SchemaValidator(spec, [options])](#new_SchemaValidator_new)
* [.validate(entity, schema)](#SchemaValidator+validate) ⇒ Array.<Object>

#### new SchemaValidator(spec, [options])
Create schema validator for schemas defined in spec at `#/components/schemas`

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| spec | Object | | API specification in OpenAPI Specification 3.0 format |
| [options] | Object | {} | options |
| [options.customFormats] | Object | | custom format validators |
| [options.banUnknownProperties] | boolean | false | disallow extra properties in validated objects |

#### schemaValidator.validate(entity, schema) ⇒ Array.<Object>
Validate object against schema. Schema can be passed explicitly or reference a schema from spec.

**Kind**: instance method of [SchemaValidator](#SchemaValidator)
**Returns**: Array.<Object> - An array with errors when object is invalid, `undefined` otherwise

| Param | Type | Description |
| --- | --- | --- |
| entity | Object | object to validate |
| schema | Object \| string | object schema |