-
Notifications
You must be signed in to change notification settings - Fork 0
/
Type.js
147 lines (109 loc) · 2.92 KB
/
Type.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
137
138
139
140
141
142
143
144
145
146
147
"use strict";
const isObject = require("../utils/isObject");
const isArray = require("../utils/isArray");
const Constraint = require("../prototypes/Constraint");
const def = {
message: `This value should be of type '{{ type }}'.`,
};
const Type = function (opt, extra) {
Constraint.apply(this, arguments); // call super constructor.
this.setExtra(extra);
if (isArray(opt)) {
opt = {
type: opt,
};
}
if (typeof opt === "string") {
opt = {
type: opt,
};
}
opt = Object.assign({}, def, opt);
if (!isArray(opt.type)) {
opt.type = [opt.type];
}
for (let i = 0, l = opt.type.length; i < l; i += 1) {
if (typeof opt.type[i] === "string") {
opt.type[i] = opt.type[i].toLowerCase();
} else {
throw new Error(
`Type constraint: Each of types have to be string and one of: ` +
Type.prototype.allowedTypes.map((a) => `"${a}"`).join(", ")
);
}
if (Type.prototype.allowedTypes.indexOf(opt.type[i]) === -1) {
throw new Error(
`Type constraint: One of types is string but is not one of: ` +
Type.prototype.allowedTypes.map((a) => `"${a}"`).join(", ")
);
}
}
this.setOptions(opt);
};
Type.prototype = Object.create(Constraint.prototype);
Type.prototype.constructor = Type;
Type.prototype.INVALID_TYPE_ERROR = "INVALID_TYPE_ERROR";
Type.prototype.validate = function (value, context, path, extra) {
const opt = this.getOptions();
if (!Type.prototype.logic(value, opt.type)) {
context
.buildViolation(opt.message)
.atPath(path)
.setParameter("{{ type }}", opt.type.join(", "))
.setCode(Type.prototype.INVALID_TYPE_ERROR)
.setInvalidValue(value)
.addViolation();
if (extra && extra.stop) {
return Promise.reject("stop Type");
}
}
return Promise.resolve("resolve Type");
};
Type.prototype.logic = function (value, type) {
let valid = false;
let arr = isArray(value);
let obj = isObject(value);
let t;
for (let i = 0, l = type.length; i < l; i += 1) {
t = type[i];
if (t === "arr") {
t = "array";
}
if (t === "obj") {
t = "object";
}
if (t === "int") {
t = "integer";
}
if (t === "str") {
t = "string";
}
if (t === "bool") {
t = "boolean";
}
if (t === "null" && value === null) {
valid = true;
break;
}
if (t === "array" && value !== null && arr && !obj) {
valid = true;
break;
}
if (t === "object" && value !== null && !arr && obj) {
valid = true;
break;
}
if (t === "integer" && Number.isInteger(value)) {
valid = true;
break;
}
if (!obj && !arr && typeof value === t) {
valid = true;
break;
}
}
return valid;
};
Type.prototype.allowedTypes =
"undefined obj object boolean bool number str string symbol function integer int arr array null".split(" ");
module.exports = Type;