From 1128a0549c1c696fdc91f9f1b47e7ad47bf19b7e Mon Sep 17 00:00:00 2001 From: Ed Salter Date: Tue, 23 Mar 2021 17:22:18 +0000 Subject: [PATCH] fix(Validation): Schema validator now accepts OpenAPI formats 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 #19 --- lib/schema-validator/index.js | 29 ++++++++++++++----- package-lock.json | 53 ++++++++++++++++++++++++++++++----- package.json | 3 +- 3 files changed, 70 insertions(+), 15 deletions(-) diff --git a/lib/schema-validator/index.js b/lib/schema-validator/index.js index 1573529..ad978bf 100644 --- a/lib/schema-validator/index.js +++ b/lib/schema-validator/index.js @@ -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; diff --git a/package-lock.json b/package-lock.json index e79ff92..f3bb416 100644 --- a/package-lock.json +++ b/package-lock.json @@ -468,16 +468,24 @@ } }, "ajv": { - "version": "6.10.2", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.10.2.tgz", - "integrity": "sha512-TXtUUEYHuaTEbLZWIKUr5pmBuhDLy+8KYtPYdcV8qC+pOZL+NKqYwvWSRrVXHn+ZmRRAu8vJTAznH7Oag6RVRw==", + "version": "6.12.6", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", + "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", "requires": { - "fast-deep-equal": "^2.0.1", + "fast-deep-equal": "^3.1.1", "fast-json-stable-stringify": "^2.0.0", "json-schema-traverse": "^0.4.1", "uri-js": "^4.2.2" } }, + "ajv-openapi": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ajv-openapi/-/ajv-openapi-2.0.0.tgz", + "integrity": "sha512-wHqERDU4g81yZN7JoJ5IgSU1Bggi6x8oUJ8Xet10im4V94Er5v2Dx7aSJGhOHeZkHHnLbbDmB4SJOqictQlTzw==", + "requires": { + "decimal.js": "^10.2.0" + } + }, "ansi-colors": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.1.tgz", @@ -1059,6 +1067,11 @@ "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=", "dev": true }, + "decimal.js": { + "version": "10.2.1", + "resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.2.1.tgz", + "integrity": "sha512-KaL7+6Fw6i5A2XSnsbhm/6B+NuEA7TZ4vqxnd5tXz9sbKtrN9Srj8ab4vKVdK8YAqZO9P1kg45Y6YLoduPf+kw==" + }, "deep-is": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.3.tgz", @@ -1247,6 +1260,18 @@ "text-table": "^0.2.0" }, "dependencies": { + "ajv": { + "version": "6.12.6", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", + "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", + "dev": true, + "requires": { + "fast-deep-equal": "^3.1.1", + "fast-json-stable-stringify": "^2.0.0", + "json-schema-traverse": "^0.4.1", + "uri-js": "^4.2.2" + } + }, "ansi-regex": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", @@ -1501,9 +1526,9 @@ "integrity": "sha512-Y9n/Ky/xZx/Bj8DePvXspUYRtHl/rGQytoIT5LaxmNwSe3wWyOeOXb3lT6Dpipq240PVpeFaGKzScz/5fvff2g==" }, "fast-deep-equal": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz", - "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=" + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" }, "fast-json-stable-stringify": { "version": "2.0.0", @@ -4338,6 +4363,20 @@ "lodash": "^4.17.14", "slice-ansi": "^2.1.0", "string-width": "^3.0.0" + }, + "dependencies": { + "ajv": { + "version": "6.12.6", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", + "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", + "dev": true, + "requires": { + "fast-deep-equal": "^3.1.1", + "fast-json-stable-stringify": "^2.0.0", + "json-schema-traverse": "^0.4.1", + "uri-js": "^4.2.2" + } + } } }, "test-exclude": { diff --git a/package.json b/package.json index 2012d8b..661308d 100644 --- a/package.json +++ b/package.json @@ -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",