From b36433546850180e810e3a8ba923a9eea53b3123 Mon Sep 17 00:00:00 2001 From: Ivano Picco Date: Tue, 24 Aug 2021 18:24:06 +0200 Subject: [PATCH] Extra keywords + fixes --- CHANGELOG.md | 10 +++++++--- index.js | 14 ++++++++++---- package.json | 5 +++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4417f41..110d512 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,12 @@ # Change Log -## Current Version (v2.1.0) +## Current Version (v2.2.0) -- using ajv extended formats -- using [ajv ^8.x.x](https://github.com/epoberezkin/ajv) +- using [ajv-keywords](https://github.com/ajv-validator/ajv-keywords) +- ignore invalid schema (TODO: fallback to deafult moleculer validator) +## Version v2.1.0 + +- using [ajv-formats](https://github.com/ajv-validator/ajv-formats) +- using [ajv ^8.x.x](https://github.com/ajv-validator/ajv) ## Version v2.0.0 diff --git a/index.js b/index.js index f71f39f..2390771 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ const Ajv = require('ajv') const addFormats = require("ajv-formats") +const addKeywords = require("ajv-formats") const BaseValidator = require('moleculer/src/validators/base') const {ValidationError} = require('moleculer/src/errors') @@ -9,7 +10,8 @@ class AjvValidator extends BaseValidator { constructor (options) { super() this.validator = new Ajv(options) - addFormats(this.validator) + addFormats(this.validator) + addKeywords(this.validator) } compile (schema) { @@ -32,10 +34,14 @@ class AjvValidator extends BaseValidator { return function validatorMiddleware(handler, action) { // Wrap a param validator if (action.params && typeof action.params === "object") { - if (!action.params.openApi && !action.params.swagger) { return handler; } - const check = this.compile(action.params); + let checkFn; + try { + checkFn = this.compile(action.params); + } catch (error) { + return handler; + } return async function validateContextParams(ctx) { - let res = await check(ctx.params != null ? ctx.params : {}); + let res = await checkFn(ctx.params != null ? ctx.params : {}); if (res === true) return handler(ctx); else { diff --git a/package.json b/package.json index 9918730..fdf262d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "moleculer-json-schema-validator", - "version": "2.1.0", + "version": "2.2.0", "description": "validate moleculer params by json schema", "keywords": [ "moleculer", @@ -12,7 +12,8 @@ "license": "MIT", "dependencies": { "ajv": "^8.x.x", - "ajv-formats": "^2.1.1" + "ajv-formats": "^2.1.1", + "ajv-keywords": "^5.0.0" }, "peerDependencies": { "moleculer": "^0.14.x"