-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidationManager.js
47 lines (44 loc) · 1.3 KB
/
validationManager.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
const validation = require('./validation')
const {
Macroable
} = require('@ostro/support/macro')
const kApp = Symbol('app')
class ValidationManager extends Macroable {
constructor(app) {
super()
Object.defineProperty(this, kApp, {
value: app,
})
}
make(inputs, schema, messages = {}) {
return Promise.resolve(new validation(inputs, schema, messages))
}
validate(inputs, schema, messages = {}) {
return this.make(inputs, schema, messages).then(async validation => {
if (await validation.fails()) {
return Promise.reject(validation.errors())
} else {
return Promise.resolve(true)
}
})
}
static extend(name, cb, message) {
validation.register(name, cb, message)
}
static extendAsync(name, cb, message) {
validation.registerAsync(name, cb, message)
}
extend(name, cb, message) {
validation.register(name, cb, message)
}
extendAsync(name, cb, message) {
validation.registerAsync(name, cb, message)
}
static __call(target, method, args) {
return validation[method](...args);
}
__call(target, method, args) {
return validation[method](...args);
}
}
module.exports = ValidationManager