-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
75 lines (57 loc) · 1.29 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict'
const Definition = require('./lib/definition.js')
module.exports = class Env extends Map {
constructor(input) {
super()
if (!Array.isArray(input)) {
throw new TypeError('input must be an array')
}
this.rules = new Map()
this.validators = input
this._loadRules()
}
static string(name) {
return new Definition('string', name)
}
static number(name) {
return new Definition('number', name)
}
static boolean(name) {
return new Definition('boolean', name)
}
static regex(name) {
return new Definition('regex', name)
}
static enum(name) {
return new Definition('enum', name)
}
static list(name) {
return new Definition('list', name)
}
_loadRules() {
for (const rule of this.validators) {
this._addRule(rule)
}
}
toJSON() {
const out = Object.create(null)
for (const obj of this.entries()) {
out[obj[0]] = obj[1]
}
return out
}
validateEnvVars() {
for (const rule of this.rules.values()) {
rule.validate()
}
}
_addRule(rule) {
if (this.has(rule._name)) {
throw new Error(`Rule with name "${rule._name}" already exists`)
}
rule._populate()
this.set(rule._name, rule._value)
this.rules.set(rule._name, rule)
return this
}
}