Skip to content

Commit

Permalink
fix(Validation): Schema validator now accepts OpenAPI formats
Browse files Browse the repository at this point in the history
Added ajv-openapi package to add support for the known formats that OpenAPI defines.
Unknown formats will no longer cause errors to be thrown and will instead write warnings to the console.

Fixes jormaechea#19
  • Loading branch information
EdwardSalter committed Mar 24, 2021
1 parent 3473313 commit 1128a05
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
29 changes: 22 additions & 7 deletions lib/schema-validator/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
'use strict';
"use strict";

const Ajv = require('ajv');
const Ajv = require("ajv");
const openApi = require("ajv-openapi");

const ajv = new Ajv({ unknownFormats: ['password'] });
// Configuration as defined in ajv-openapi example: https://www.npmjs.com/package/ajv-openapi#configuration-for-full-openapi-compatibility
const ajvOptions = {
schemaId: "auto",
format: "full",
coerceTypes: true,
unknownFormats: "ignore",
useDefaults: true
};

class SchemaValidator {
const openApiOptions = {
useDraft04: true
};

static validate(data, schema) {
return !ajv.validate(schema, data) ? ajv.errors : [];
}
const ajv = openApi(
new Ajv(ajvOptions),
openApiOptions
);

class SchemaValidator {
static validate(data, schema) {
return !ajv.validate(schema, data) ? ajv.errors : [];
}
}

module.exports = SchemaValidator;
53 changes: 46 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"sinon": "^9.2.4"
},
"dependencies": {
"ajv": "^6.10.2",
"ajv": "^6.12.6",
"ajv-openapi": "^2.0.0",
"body-parser": "^1.19.0",
"chokidar": "^3.5.1",
"colors": "^1.4.0",
Expand Down

0 comments on commit 1128a05

Please sign in to comment.