-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
24 lines (19 loc) · 611 Bytes
/
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
const Ajv = require('ajv')
const BaseValidator = require('moleculer/src/validator')
const {ValidationError} = require('moleculer/src/errors')
class AjvValidator extends BaseValidator {
constructor () {
super()
this.validator = new Ajv()
}
compile (schema) {
const validate = this.validator.compile(schema)
return (params) => this.validate(params, validate)
}
validate (params, validate) {
const isValid = validate(params)
if (isValid) return true
else throw new ValidationError('Parameters validation error!', null, validate.errors)
}
}
module.exports = AjvValidator