-
Notifications
You must be signed in to change notification settings - Fork 1
/
is.js
231 lines (209 loc) · 7.81 KB
/
is.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
'use strict';
/**
* Global constant & API entry point for Fluent JSON validator.
*
* @namespace is
* @type {Object}
*/
const is = {
/**
* Function to return a new configurable validator. You don't need to call this directly! Use a factory method instead.
*
* @class is.__validationUnit
* @return {Object} The new, unconfigured validator.
*/
__validationUnit: function() {
this.__type = undefined;
this.__required = true;
this.__functionalValidators = [];
this.__validateType = (subject) => {
return false; // FIXME should this actually throw an exception?
}
/**
* Validate the subject against the set schema.
*
* @param {any} subject The subject to validate.
* @return {Boolean} Validation result: true if the subject matches the schema, false otherwise.
*/
this.validate = (subject) => {
if (typeof subject === "undefined" && this.__type !== "array" && this.__type !== "various") {
return !this.__required;
} else {
return this.__validateType(subject) &&
this.__functionalValidators.filter(functionalValidator => !functionalValidator(subject)).length == 0;
// ^ == there is no functional validator lambda which does not validate the subject.
};
};
/**
* Set the validator to optional, so it accepts missing values.
*
* @return {is.__validationUnit} The validator itself.
*/
this.optional = function() {
this.__required = false;
return this;
};
/**
* Add a new functional validation lambda to the validator.
*
* @param {lambda} lambda The functional validator, which accesses the subject and returns true or false.
* @return {is.__validationUnit} The validator itself.
*/
this.Which = function(lambda) {
this.__functionalValidators.push(lambda);
return this;
}
/**
* Set the validator to validate strings.
*
* @return {is.__validationUnit} The validator itself.
*/
this.String = function() {
this.__type = "string";
this.__validateType = (value) => (
typeof value === "string"
);
return this;
};
/**
* Set the validator to validate booleans.
*
* @return {is.__validationUnit} The validator itself.
*/
this.Boolean = function() {
this.__type = "boolean";
this.__validateType = (value) => (
typeof value === "boolean"
);
return this;
}
/**
* Set the validator to validate numbers.
*
* @return {is.__validationUnit} The validator itself.
*/
this.Number = function() {
this.__type = "number";
this.__validateType = (value) => (
typeof value === "number"
);
return this;
};
/**
* Set the validator to validate objects.
*
* @param {Object} schemaObj The schema of the object to validate, made up of other validators.
* @return {is.__validationUnit} The validator itself.
*/
this.Object = function(schemaObj) {
this.__type = "object";
this.__schemaObj = schemaObj || {};
this.__validateType = (value) => {
if (typeof value !== "object") {
return false;
};
for (const schemaKey in this.__schemaObj) {
// here we iterate over the keys of the schema
// and validate the corresponding properties of the value object.
if (!schemaObj[schemaKey].validate(value[schemaKey])) {
return false;
};
};
return true;
};
return this;
};
/**
* Set the validator to validate arrays.
*
* @param {is.__validationUnit} schema The validator which must validate every element of the array.
* @return {is.__validationUnit} The validator itself.
*/
this.ArrayOf = function(schema) {
this.__type = "array";
this.__schema = schema;
this.__validateType = (value) => {
if (Array.isArray(value)) {
return value.filter(e => !this.__schema.validate(e)).length == 0;
// ^ == there is no item in the array which is not validated by the schema
} else {
return (!this.__required && typeof value === "undefined");
};
};
return this;
};
/**
* Set the validator to validate a variable type.
*
* @param {is.__validationUnit} schemaArray An array of other validators.
* @return {is.__validationUnit} The validator itself.
*/
this.OneOf = function(schemaArray) {
this.__type = "various";
this.__schemaArray = schemaArray;
this.__validateType = (value) => {
if (!this.__required && typeof value === "undefined") {
return true;
} else {
return this.__schemaArray.filter(schema => schema.validate(value)).length > 0;
// ^ == there is at least one schema which validates the value
};
};
return this;
}
return this;
},
};
/**
* Create a new validator object that is optional.
*
* @return {is.__validationUnit} A new optional validator object.
*/
is.optional = () => (new is.__validationUnit().optional());
/**
* Create a new functional validator.
*
* @param {Function} functionalValidator The functional validator which checks the subject for functional requirements.
* @return {is.__validationUnit} The new functional validator.
*/
is.Which = (functionalValidator) => (new is.__validationUnit().Which(functionalValidator));
/**
* Create a new string validator.
*
* @return {is.__validationUnit} The new string validator.
*/
is.String = () => (new is.__validationUnit().String());
/**
* Create a new boolean validator.
*
* @return {is.__validationUnit} The new boolean validator.
*/
is.Boolean = () => (new is.__validationUnit().Boolean());
/**
* Create a new number validator.
*
* @return {is.__validationUnit} The new number validator.
*/
is.Number = () => (new is.__validationUnit().Number());
/**
* Create an object validator.
*
* @param {Object} schemaObj The schema object, made up of other validators.
* @return {is.__validationUnit} The new object validator.
*/
is.Object = (schemaObj) => (new is.__validationUnit().Object(schemaObj));
/**
* Create an array validator.
*
* @param {is.__validationUnit} schema Any validator that the array elements must validate against.
* @return {is.__validationUnit} The new array validator.
*/
is.ArrayOf = (schema) => (new is.__validationUnit().ArrayOf(schema));
/**
* Create a variable type validator.
*
* @param {is.__validationUnit[]} schemaArray Array of validator objects the subject must match against (at least one of them).
* @return {is.__validationUnit} The new variable type validator.
*/
is.OneOf = (schemaArray) => (new is.__validationUnit().OneOf(schemaArray));
export { is };