Skip to content

Commit

Permalink
Extra keywords + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pibi committed Aug 24, 2021
1 parent c046886 commit b364335
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand Down

0 comments on commit b364335

Please sign in to comment.