From e4b171114fb187a605d1cd82ce86262176758e71 Mon Sep 17 00:00:00 2001 From: Icebob Date: Tue, 7 Mar 2017 22:02:15 +0100 Subject: [PATCH] change validatorjs to fast-jsvalidator --- .vscode/launch.json | 7 ++++ benchmark/suites/call.js | 70 ++++++++++++++++++--------------------- benchmark/user.service.js | 2 +- examples/math.service.js | 4 +-- examples/simple/index.js | 2 ++ package.json | 1 + src/validator.js | 26 ++++++++++----- yarn.lock | 29 ++++++++++------ 8 files changed, 81 insertions(+), 60 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 5ff249b8e..9f0c4f24d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,6 +11,13 @@ "program": "${workspaceRoot}\\examples\\caching\\index.js", "cwd": "${workspaceRoot}\\examples\\caching" }, + { + "type": "node", + "request": "launch", + "name": "Launch demo:simple", + "program": "${workspaceRoot}\\examples\\simple\\index.js", + "cwd": "${workspaceRoot}\\examples\\simple" + }, { "type": "node", "request": "launch", diff --git a/benchmark/suites/call.js b/benchmark/suites/call.js index f47d99e95..74fb3b800 100644 --- a/benchmark/suites/call.js +++ b/benchmark/suites/call.js @@ -200,59 +200,53 @@ Platform info: Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz × 8 Suite: Call methods -√ Direct service call x 598,287 ops/sec ±0.72% (83 runs sampled) -√ action.handler x 857,459 ops/sec ±0.42% (86 runs sampled) -√ ctx.invoke x 563,193 ops/sec ±0.82% (87 runs sampled) -√ broker.call (normal) x 530,562 ops/sec ±1.15% (83 runs sampled) -√ broker.call (with params) x 533,886 ops/sec ±0.67% (87 runs sampled) - - Direct service call -30.23% (598,287 ops/sec) - action.handler 0.00% (857,459 ops/sec) - ctx.invoke -34.32% (563,193 ops/sec) - broker.call (normal) -38.12% (530,562 ops/sec) - broker.call (with params) -37.74% (533,886 ops/sec) +√ broker.call (normal) x 818,115 ops/sec ±0.38% (87 runs sampled) +√ broker.call (with params) x 790,488 ops/sec ±1.35% (86 runs sampled) + + broker.call (normal) 0.00% (818,115 ops/sec) + broker.call (with params) -3.38% (790,488 ops/sec) ----------------------------------------------------------------------- Suite: Call with middlewares -√ Call without middlewares x 531,402 ops/sec ±0.91% (88 runs sampled) -√ Call with 1 middleware x 531,329 ops/sec ±2.01% (87 runs sampled) -√ Call with 5 middlewares x 537,754 ops/sec ±0.98% (90 runs sampled) +√ Call without middlewares x 762,770 ops/sec ±1.03% (87 runs sampled) +√ Call with 1 middleware x 769,515 ops/sec ±0.73% (88 runs sampled) +√ Call with 5 middlewares x 769,607 ops/sec ±0.96% (87 runs sampled) - Call without middlewares -1.18% (531,402 ops/sec) - Call with 1 middleware -1.19% (531,329 ops/sec) - Call with 5 middlewares 0.00% (537,754 ops/sec) + Call without middlewares -0.88% (762,770 ops/sec) + Call with 1 middleware 0.00% (769,515 ops/sec) + Call with 5 middlewares 0.01% (769,607 ops/sec) ----------------------------------------------------------------------- Suite: Call with cachers -√ No cacher x 441,422 ops/sec ±1.39% (87 runs sampled) -√ Built-in cacher x 105,621 ops/sec ±0.99% (83 runs sampled) -√ Built-in cacher (keys filter) x 202,384 ops/sec ±0.78% (88 runs sampled) +√ No cacher x 598,163 ops/sec ±0.86% (85 runs sampled) +√ Built-in cacher x 64,271 ops/sec ±0.99% (84 runs sampled) +√ Built-in cacher (keys filter) x 88,719 ops/sec ±0.93% (86 runs sampled) - No cacher 0.00% (441,422 ops/sec) - Built-in cacher -76.07% (105,621 ops/sec) - Built-in cacher (keys filter) -54.15% (202,384 ops/sec) + No cacher 0.00% (598,163 ops/sec) + Built-in cacher -89.26% (64,271 ops/sec) + Built-in cacher (keys filter) -85.17% (88,719 ops/sec) ----------------------------------------------------------------------- Suite: Call with param validator -√ No validator x 435,510 ops/sec ±1.47% (87 runs sampled) -√ With validator passes x 93,460 ops/sec ±0.96% (87 runs sampled) -√ With validator fail x 18,570 ops/sec ±1.35% (85 runs sampled) +√ No validator x 588,463 ops/sec ±1.11% (84 runs sampled) +√ With validator passes x 541,903 ops/sec ±1.41% (84 runs sampled) +√ With validator fail x 25,648 ops/sec ±1.62% (85 runs sampled) - No validator 0.00% (435,510 ops/sec) - With validator passes -78.54% (93,460 ops/sec) - With validator fail -95.74% (18,570 ops/sec) + No validator 0.00% (588,463 ops/sec) + With validator passes -7.91% (541,903 ops/sec) + With validator fail -95.64% (25,648 ops/sec) ----------------------------------------------------------------------- Suite: Call with statistics & metrics -√ No statistics x 524,523 ops/sec ±0.90% (81 runs sampled) -√ With metrics x 280,352 ops/sec ±1.41% (86 runs sampled) -√ With statistics x 319,233 ops/sec ±1.95% (81 runs sampled) -√ With metrics & statistics x 192,269 ops/sec ±3.09% (61 runs sampled) - - No statistics 0.00% (524,523 ops/sec) - With metrics -46.55% (280,352 ops/sec) - With statistics -39.14% (319,233 ops/sec) - With metrics & statistics -63.34% (192,269 ops/sec) +√ No statistics x 735,371 ops/sec ±1.16% (88 runs sampled) +√ With metrics x 180,635 ops/sec ±1.49% (82 runs sampled) +√ With statistics x 493,453 ops/sec ±1.06% (86 runs sampled) +√ With metrics & statistics x 178,360 ops/sec ±0.96% (85 runs sampled) + + No statistics 0.00% (735,371 ops/sec) + With metrics -75.44% (180,635 ops/sec) + With statistics -32.90% (493,453 ops/sec) + With metrics & statistics -75.75% (178,360 ops/sec) ----------------------------------------------------------------------- */ \ No newline at end of file diff --git a/benchmark/user.service.js b/benchmark/user.service.js index e5551355f..36d827d03 100644 --- a/benchmark/user.service.js +++ b/benchmark/user.service.js @@ -45,7 +45,7 @@ module.exports = function(broker) { validate: { cache: false, params: { - id: "required|integer|min:1" + id: { type: "number", integer: true, min: 1 } }, handler(ctx) { return users.find(user => user.id == ctx.params.id); diff --git a/examples/math.service.js b/examples/math.service.js index a9079ecd4..f9cee1ad7 100644 --- a/examples/math.service.js +++ b/examples/math.service.js @@ -11,8 +11,8 @@ module.exports = { mult: { params: { - a: "required|numeric", - b: "required|numeric" + a: "number", + b: "number" }, handler(ctx) { return Number(ctx.params.a) * Number(ctx.params.b); diff --git a/examples/simple/index.js b/examples/simple/index.js index d5c71335a..b1df8f323 100644 --- a/examples/simple/index.js +++ b/examples/simple/index.js @@ -25,4 +25,6 @@ broker.call("math.add", { a: 5, b: 3 }).then(res => broker.logger.log(" 5 + 3 = }) .catch(err => { broker.logger.error(`Error occured when '${err.ctx.action.name}' action called! Message:`, err.message, " Params:", err.ctx.params); + if (err.data) + broker.logger.error("Data:", err.data); }); \ No newline at end of file diff --git a/package.json b/package.json index 171df69be..dcaf28cd0 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "conventional-changelog-cli": "1.2.0", "es6-error": "4.0.2", "eventemitter2": "3.0.1", + "fast-jsvalidator": "icebob/fast-jsvalidator", "glob": "7.1.1", "ioredis": "2.5.0", "lodash": "4.17.4", diff --git a/src/validator.js b/src/validator.js index b15f01d9e..8ec551baa 100644 --- a/src/validator.js +++ b/src/validator.js @@ -7,12 +7,13 @@ "use strict"; const Promise = require("bluebird"); -const Validator = require("validatorjs"); +const Validator = require("fast-jsvalidator"); const { ValidationError } = require("./errors"); class ParamValidator { constructor() { + this.validator = new Validator(); } init(broker) { @@ -22,11 +23,14 @@ class ParamValidator { } } + compile(schema) { + return this.validator.compile(schema); + } + validate(schema, params) { - let validation = new Validator(params, schema); - const res = validation.passes(); - if (!res) - throw new ValidationError("Parameters validation error!", validation.errors.all()); + const res = this.validator.validate(params, schema); + if (res !== true) + throw new ValidationError("Parameters validation error!", res); return true; } @@ -37,11 +41,17 @@ class ParamValidator { * @memberOf ParamValidator */ middleware() { - let validate = Promise.method(this.validate); return function validatorMiddleware(handler, action) { // Wrap a param validator - if (action.params && typeof action.params == "object") { - return ctx => validate(action.params, ctx.params).then(() => handler(ctx)); + if (action.params && typeof action.params === "object") { + const check = this.compile(action.params); + return ctx => { + const res = check(ctx.params); + if (res === true) + return handler(ctx); + else + return Promise.reject(new ValidationError("Parameters validation error!", res)); + }; } return handler; }.bind(this); diff --git a/yarn.lock b/yarn.lock index d0e383d8c..1f12680e6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -422,11 +422,11 @@ bluebird@3.4.1: version "3.4.1" resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.4.1.tgz#b731ddf48e2dd3bedac2e75e1215a11bcb91fa07" -bluebird@3.4.7, bluebird@^3.3.4, bluebird@^3.4.6: +bluebird@3.4.7: version "3.4.7" resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.4.7.tgz#f72d760be09b7f76d08ed8fae98b289a8d05fab3" -bluebird@3.5.0: +bluebird@3.5.0, bluebird@^3.3.4, bluebird@^3.4.6: version "3.5.0" resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.0.tgz#791420d7f551eea2897453a8a77653f96606d67c" @@ -1019,21 +1019,21 @@ dateformat@^1.0.11, dateformat@^1.0.12: get-stdin "^4.0.1" meow "^3.3.0" -debug@2, debug@2.3.3, debug@^2.1.1: - version "2.3.3" - resolved "https://registry.yarnpkg.com/debug/-/debug-2.3.3.tgz#40c453e67e6e13c901ddec317af8986cda9eff8c" +debug@2, debug@2.6.1: + version "2.6.1" + resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.1.tgz#79855090ba2c4e3115cc7d8769491d58f0491351" dependencies: ms "0.7.2" -debug@2.2.0, debug@^2.2.0: +debug@2.2.0, debug@^2.1.1, debug@^2.2.0: version "2.2.0" resolved "https://registry.yarnpkg.com/debug/-/debug-2.2.0.tgz#f87057e995b1a1f6ae6a4960664137bc56f039da" dependencies: ms "0.7.1" -debug@2.6.1: - version "2.6.1" - resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.1.tgz#79855090ba2c4e3115cc7d8769491d58f0491351" +debug@2.3.3: + version "2.3.3" + resolved "https://registry.yarnpkg.com/debug/-/debug-2.3.3.tgz#40c453e67e6e13c901ddec317af8986cda9eff8c" dependencies: ms "0.7.2" @@ -1497,6 +1497,13 @@ fakerator: version "0.3.0" resolved "https://registry.yarnpkg.com/fakerator/-/fakerator-0.3.0.tgz#4635d9c345d367ecf69a6892b525d5b7fda7a080" +fast-jsvalidator@icebob/fast-jsvalidator: + version "0.1.0" + resolved "https://codeload.github.com/icebob/fast-jsvalidator/tar.gz/a1aed8d5ea7f60b24ccfcd17aebf935fc5c9a44f" + dependencies: + glob "^7.1.1" + lodash "^4.17.4" + fast-levenshtein@~2.0.4: version "2.0.5" resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.5.tgz#bd33145744519ab1c36c3ee9f31f08e9079b67f2" @@ -1723,7 +1730,7 @@ github@2.4.0: https-proxy-agent "^1.0.0" mime "^1.2.11" -glob, glob@^7.0.0, glob@^7.0.3, glob@^7.0.5: +glob, glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.1.1: version "7.1.1" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.1.tgz#805211df04faaf1c63a3600306cdf5ade50b2ec8" dependencies: @@ -2752,7 +2759,7 @@ lodash@3.10.1: version "3.10.1" resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6" -lodash@4.17.4, lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.3, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0: +lodash@4.17.4, lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0: version "4.17.4" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"