-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.js
73 lines (62 loc) · 1.16 KB
/
base.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/** Class for Request Validator */
class BaseValidator {
/**
* Initialize the validator
* @param {Object}
* @param {Array} policy_conf, Policies
*/
constructor(opt) {
this.conf = opt.conf
this.policyFactory = opt.policyFactory
this.policy_list = []
}
/**
* @attribute policy_conf
*/
get policy_conf() {
throw new Error('Unimplemented')
}
/**
* @attribute policies
*/
get policies() {
var policy_list = []
this.policy_conf.forEach((policy)=>{
policy_list.push(this.policyFactory.build(policy))
})
return policy_list
}
before() {
this.policy_list = this.policies
return this
}
after() {
return this.done()
}
/**
* Run checks all policies
* @return {[type]} [description]
*/
run() {
return this.next()
}
next() {
return this.policy_list.length?this.policy_list.shift().approve(this):this.after()
}
validate(obj) {
return new Promise((resolve, reject)=>{
this.resolve = resolve
this.reject = reject
this.before().run()
})
}
/**
* check if request needs authentication
* @param {request}
* @return {this}
*/
done() {
throw new Error('Unimplemented')
}
}
module.exports = BaseValidator;