This repository has been archived by the owner on May 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.js
136 lines (110 loc) · 2.78 KB
/
validate.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict'
const {
bgGreen: bgOK,
bgRed: bgKO,
bgYellow: bgWarning,
bgBlue: bgInfo
} = require('colorette')
class ValidationMsg {
constructor (level, message) {
this._level = level || 'ok'
this._message = message
this._children = []
}
get level () { return this._level }
get message () { return this._message }
get children () { return this._children }
set level (value) {
if (LEVEL[this._level] < LEVEL[value]) {
this._level = value
}
}
add (msg) {
this._children.push(msg)
this.level = msg.level
}
}
const LEVEL = {
ok: 0,
info: 1,
warning: 2,
error: 3
}
class Test {
constructor (test, message, level = 'error') {
this._message = message
this._level = level
this._test = test
}
perform (value) {
const test = this._test(value)
const [level, message] = test
? ['ok', `${value}`]
: [this._level, this._message]
return new ValidationMsg(level, message)
}
}
class Validator {
constructor (name) {
this._name = name
this._tests = []
}
get name () { return this._name }
get tests () { return this._tests }
add (test) { this._tests.push(test); return this }
perform (value) {
const result = new ValidationMsg('ok', `${this.name}: '${value}'`)
for (const test of this.tests) {
result.add(test.perform(value))
}
return result
}
}
const doValidate = (values, validators) => {
let isError = false
for (const prop in values) {
const validator = validators.find(
validator => validator.name === prop
)
if (validator) {
const validation = validator.perform(values[prop])
const level = validation.level
isError |= level === 'error'
console.log(_prettify(validation))
} else {
console.log(_prettify(new ValidationMsg('info', `${prop}: Not checked (no validator found)`)))
}
}
return isError
}
const _prettify = (errMsg, showDetails = true) => {
// "Header"
let result = ''
switch (errMsg.level) {
case 'ok': result += bgOK('OK'); break
case 'info': result += bgInfo('INFO'); break
case 'warning': result += bgWarning('WARNING'); break
case 'error': result += bgKO('ERROR'); break
default: /* Do nothing */
}
if (errMsg.message) { result += ` ${errMsg.message}` }
if (showDetails) {
result += errMsg.children.reduce((acc, child) => {
if (child.level !== 'ok') {
acc += `\n ${child.message}`
}
return acc
}, '')
}
return result
}
// ////////////////////////////////
// ////////////////////////////////
// Public API
// ////////////////////////////////
// ////////////////////////////////
module.exports = {
createTest: (test, message, level) => new Test(test, message, level),
createValidator: (name) => new Validator(name),
doValidate
}