forked from Jemt/Fit.UI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidation.js
389 lines (329 loc) · 16.4 KB
/
Validation.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/// <container name="Fit.Validation">
/// Validation logic
/// </container>
Fit.Validation = {};
Fit._internal.Validation = {};
Fit._internal.Validation.DebugMode = true; // TODO: When ready for production: Remove or set False!
Fit._internal.Validation.Clone = null;
// ==========================================================
// Type checking
// ==========================================================
/// <function container="Fit.Validation" name="ExpectNumber" access="public" static="true">
/// <description> Throws error if passed object is not a number </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectNumber = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if (typeof(val) !== "number" || isNaN(val) === true)
Fit.Validation.ThrowError("Value '" + val + "' is not a valid number");
}
/// <function container="Fit.Validation" name="ExpectInteger" access="public" static="true">
/// <description> Throws error if passed object is not an integer </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectInteger = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if (typeof(val) !== "number" || val % 1 !== 0)
Fit.Validation.ThrowError("Value '" + val + "' is not a valid integer");
}
/// <function container="Fit.Validation" name="ExpectString" access="public" static="true">
/// <description> Throws error if passed object is not a string </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectString = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if (typeof(val) !== "string")
Fit.Validation.ThrowError("Value '" + val + "' is not a valid string");
}
/// <function container="Fit.Validation" name="ExpectStringValue" access="public" static="true">
/// <description> Same as Fit.Validation.ExpectString(..), but string must contain an actual value if set (not be empty) </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectStringValue = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
Fit.Validation.ExpectString(val);
if (val === "")
Fit.Validation.ThrowError("String cannot be empty");
}
/// <function container="Fit.Validation" name="ExpectBoolean" access="public" static="true">
/// <description> Throws error if passed object is not a boolean </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectBoolean = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if (typeof(val) !== "boolean")
Fit.Validation.ThrowError("Value '" + val + "' is not a valid boolean");
}
/// <function container="Fit.Validation" name="ExpectDate" access="public" static="true">
/// <description> Throws error if passed object is not an instance of Date </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectDate = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if ((val instanceof Date) === false)
Fit.Validation.ThrowError("Value '" + val + "' is not an instance of Date");
}
/// <function container="Fit.Validation" name="ExpectArray" access="public" static="true">
/// <description> Throws error if passed object is not an instance of Array </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectArray = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if ((val instanceof Array) === false)
Fit.Validation.ThrowError("Value '" + val + "' is not an instance of Array");
}
/// <function container="Fit.Validation" name="ExpectTypeArray" access="public" static="true">
/// <description>
/// Throws error if passed object is not an instance of Array
/// contaning only objects/values of type given by validation callback.
/// Example: Fit.Validation.ExpectTypeArray(arr, Fit.Validation.ExpectString)
/// </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="typeValCallback" type="function"> Value validation callback </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectTypeArray = function(val, typeValCallback, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if ((val instanceof Array) === false)
Fit.Validation.ThrowError("Value '" + val + "' is not an instance of Array");
// Validate types within array
Fit.Validation.ExpectFunction(typeValCallback); // Make sure callback is valid
Fit.Array.ForEach(val, function(v)
{
typeValCallback(v);
});
}
/// <function container="Fit.Validation" name="ExpectInstanceArray" access="public" static="true">
/// <description>
/// Throws error if passed object is not an instance of Array
/// contaning only instances of specified type. Example:
/// Fit.Validation.ExpectInstanceArray(arr, Fit.Controls.TreeView.Node)
/// </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="instanceType" type="object"> Instance type (constructor, e.g. Fit.Http.Request) </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectInstanceArray = function(val, instanceType, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if ((val instanceof Array) === false)
Fit.Validation.ThrowError("Value '" + val + "' is not an instance of Array");
// Validate types within array
Fit.Array.ForEach(val, function(v)
{
Fit.Validation.ExpectInstance(v, instanceType);
});
}
/// <function container="Fit.Validation" name="ExpectCollection" access="public" static="true">
/// <description> Throws error if passed object is not a collection that can be iterated </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectCollection = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if ((val instanceof NodeList) === false && (window.StaticNodeList && (val instanceof StaticNodeList) === false) && (val instanceof HTMLCollection) === false && (val instanceof Array) === false)
Fit.Validation.ThrowError("Value '" + val + "' is not a valid collection");
}
/// <function container="Fit.Validation" name="ExpectRegExp" access="public" static="true">
/// <description> Throws error if passed object is not an instance of RegExp </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectRegExp = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if (val instanceof RegExp === false)
Fit.Validation.ThrowError("Value '" + val + "' is not an instance of RegExp");
}
/// <function container="Fit.Validation" name="ExpectDomElement" access="public" static="true">
/// <description> Throws error if passed object is not an instance of Element </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectDomElement = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if ((val instanceof Element) === false)
Fit.Validation.ThrowError("Value '" + val + "' is not a DOMElement");
}
/// <function container="Fit.Validation" name="ExpectContentNode" access="public" static="true">
/// <description> Throws error if passed object is not an instance of Element or Text </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectContentNode = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if ((val instanceof Element) === false && (val instanceof Text) === false)
Fit.Validation.ThrowError("Value '" + val + "' is not a Content Node (Element or Text)");
}
/// <function container="Fit.Validation" name="ExpectWindow" access="public" static="true">
/// <description> Throws error if passed object is not an instance of Window </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectWindow = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if ((val instanceof Window) === false)
Fit.Validation.ThrowError("Value '" + val + "' is not an instance of Window");
}
/// <function container="Fit.Validation" name="ExpectFunction" access="public" static="true">
/// <description> Throws error if passed object is not a valid function </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectFunction = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if (typeof(val) !== "function")
Fit.Validation.ThrowError("Value '" + val + "' is not a valid function");
}
/// <function container="Fit.Validation" name="ExpectEventTarget" access="public" static="true">
/// <description> Throws error if passed object is not an instance of EventTarget </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectEventTarget = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if (typeof(EventTarget) !== "undefined" && (val instanceof EventTarget) === false)
Fit.Validation.ThrowError("Value '" + val + "' is not an instance of EventTarget");
else if (!val.removeEventListener && !val.attachEvent) // IE8
Fit.Validation.ThrowError("Value '" + val + "' is not an instance of EventTarget");
}
/// <function container="Fit.Validation" name="ExpectEvent" access="public" static="true">
/// <description> Throws error if passed object is not an instance of Event </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectEvent = function(val, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
// IE9 and above: window.event is now of type MSEventObj (legacy),
// while an actual Event instance is passed to handlers as specified by W3C.
if ((val instanceof Event) === false && (val instanceof MSEventObj) === false)
Fit.Validation.ThrowError("Value '" + val + "' is not an instance of Event");
}
/// <function container="Fit.Validation" name="ExpectInstance" access="public" static="true">
/// <description> Throws error if passed object is not an instance of specified type </description>
/// <param name="val" type="object"> Object to validate </param>
/// <param name="instanceType" type="object"> Instance type (constructor, e.g. Fit.Http.Request) </param>
/// <param name="allowNotSet" type="boolean" default="false"> Set True to allow object to be Null or Undefined </param>
/// </function>
Fit.Validation.ExpectInstance = function(val, instanceType, allowNotSet)
{
if (allowNotSet === true && (val === undefined || val === null))
return;
if ((val instanceof instanceType) === true || Fit.Core.Extends(val, instanceType) === true)
return;
Fit.Validation.ThrowError("Unsupported object type passed");
}
/// <function container="Fit.Validation" name="ExpectIsSet" access="public" static="true">
/// <description> Throws error if passed object is either Null or Undefined </description>
/// <param name="val" type="object"> Object to validate </param>
/// </function>
Fit.Validation.ExpectIsSet = function(val)
{
if (Fit.Validation.IsSet(val) === false)
Fit.Validation.ThrowError("Value not set");
}
// ==========================================================
// Misc.
// ==========================================================
/// <function container="Fit.Validation" name="IsSet" access="public" static="true">
/// <description> Returns True if specified object is set (not Null or Undefined), otherwise False </description>
/// <param name="val" type="object"> Object to validate </param>
/// </function>
Fit.Validation.IsSet = function(obj)
{
return (obj !== null && obj !== undefined);
}
/// <function container="Fit.Validation" name="ThrowError" access="public" static="true">
/// <description> Throw error and provide stack trace to browser console </description>
/// <param name="msg" type="object"> Object to validate </param>
/// </function>
Fit.Validation.ThrowError = function(msg)
{
if (Fit._internal.Validation.DebugMode === true)
{
var stackTrace = Fit.Validation.GetStackTrace();
alert("ThrowError: " + msg + ((stackTrace !== "") ? "\n\n" : "") + stackTrace);
}
if (window.console && console.trace)
console.trace();
throw new Error(msg); // Never change this behaviour - we should always re-throw the error to terminate execution
}
Fit.Validation.GetStackTrace = function()
{
if (window.Error === undefined || Error.captureStackTrace === undefined)
return "";
var obj = {};
Error.captureStackTrace(obj, Fit.Validation.GetStackTrace);
return obj.stack;
}
// ==========================================================
// DebugMode (enable/disable type checking)
// ==========================================================
/// <function container="Fit.Validation" name="Enabled" access="public" static="true">
/// <description> Set or get flag indicating whether type checking should take place (DebugMode) </description>
/// <param name="val" type="boolean"> True enables DebugMode, False disables DebugMode </param>
/// </function>
Fit.Validation.Enabled = function(val)
{
if (Fit.Validation.IsSet(val) === true)
{
if (val === true)
{
Fit._internal.Validation.DebugMode = true;
if (Fit._internal.Validation.Clone !== null) // Null if Debug Mode is already enabled
Fit.Validation = Fit.Core.Clone(Fit._internal.Validation.Clone);
}
else
{
Fit._internal.Validation.DebugMode = false;
if (Fit._internal.Validation.Clone === null)
Fit._internal.Validation.Clone = Fit.Core.Clone(Fit.Validation);
// Replace all type checking functions when Debug Mode is disabled
for (var f in Fit.Validation)
if (f.indexOf("Expect") === 0)
Fit.Validation[f] = function() {}
}
}
return (Fit._internal.Validation.DebugMode === true);
}
if (Fit._internal.Validation.DebugMode !== true)
Fit.Validation.Enabled(false);