forked from zhaoyao91/moleculer-json-schema-validator
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
58 lines (49 loc) · 1.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const Ajv = require("ajv");
const addFormats = require("ajv-formats");
const addKeywords = require("ajv-keywords");
const { Validators, Errors: { ValidationError } } = require("moleculer");
class AjvValidator extends Validators.Base {
constructor (options = {}) {
super();
this.validator = new Ajv(options);
addKeywords(this.validator);
addFormats(this.validator);
this.fallbackValidator = new Validators.Fastest();
}
compile (schema) {
const validate = this.validator.compile(schema);
return (params) => this.validate(params, validate);
}
async validate (params, validate) {
const isValid = await validate(params);
if (!isValid) throw new ValidationError("Parameters validation error!", null, validate.errors);
}
/**
* Register validator as a middleware
*
* @memberof ParamValidator
*/
middleware() {
return function validatorMiddleware(handler, action) {
if (!action.jsonSchema || typeof action.jsonSchema !== "object") {
// no schema to validate for => just return back the handler directly
if (!action.params || typeof action.params !== "object") return handler;
// fallback to the fastest validator (moleculer's default validator)
const checkFn = this.fallbackValidator.compile(action.params);
return async function validateContextParams(ctx) {
const res = await checkFn(ctx.params);
if (res !== true)
throw new ValidationError("Parameters validation error!", null, res);
return handler(ctx);
};
}
// Wrap a param validator when the params are specified
const checkFn = this.compile(action.jsonSchema);
return async function validateContextParams(ctx) {
await checkFn(ctx.params != null ? ctx.params : {});
return handler(ctx);
};
}.bind(this);
}
}
module.exports = AjvValidator;