Skip to content

Commit

Permalink
change validatorjs to fast-jsvalidator
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Mar 7, 2017
1 parent c3e8644 commit e4b1711
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 60 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
70 changes: 32 additions & 38 deletions benchmark/suites/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
-----------------------------------------------------------------------
*/
2 changes: 1 addition & 1 deletion benchmark/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions examples/math.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions examples/simple/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 18 additions & 8 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
29 changes: 18 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"

Expand Down

0 comments on commit e4b1711

Please sign in to comment.