forked from jwaliszko/ExpressiveAnnotations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpressive.annotations.validate.js
679 lines (649 loc) · 35.6 KB
/
expressive.annotations.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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/* expressive.annotations.validate.js - v2.6.3
* Client-side component of ExpresiveAnnotations - annotation-based conditional validation library.
* https://github.com/JaroslawWaliszko/ExpressiveAnnotations
*
* Copyright (c) 2014 Jaroslaw Waliszko
* Licensed MIT: http://opensource.org/licenses/MIT */
(function($, window) {
'use strict';
var
backup = window.ea, // map over the ea in case of overwrite
api = { // to be accesssed from outer scope
settings: {
debug: false, // output debug messages to the web console (should be disabled for release code)
dependencyTriggers: 'change keyup', // a string containing one or more DOM field event types (such as "change", "keyup" or custom event names) for which fields directly dependent on referenced DOM field are validated
apply: function(options) { // alternative way of settings setup, crucial to invoke e.g. for new set of dependency triggers to be re-bound
$.extend(api.settings, options);
$('form').each(function() {
$(this).find('input, select, textarea').off('.expressive.annotations'); // remove all event handlers in the '.expressive.annotations' namespace
validationHelper.bindFields(this, true);
});
}
},
addMethod: function(name, func) { // provide custom function to be accessible for expression
toolchain.addMethod(name, func); // parameters: name - method name
}, // func - method body
// e.g. if server-side uses following attribute: [AssertThat("IsBloodType(BloodType)")], where IsBloodType() is a custom method available at C# side,
// its client-side equivalet, mainly function of the same signature (name and the number of parameters), must be also provided, i.e.
// ea.addMethod('IsBloodType', function(group) {
// return /^(A|B|AB|0)[\+-]$/.test(group);
// });
addValueParser: function(name, func) { // provide custom deserialization methods for values of these DOM fields, which are accordingly decorated with ValueParser attribute at the server-side
typeHelper.addValueParser(name, func); // parameters: name - parser name
}, // func - parse logic
// e.g. for objects when stored in non-json format or dates when stored in non-standard format (not proper for Date.parse(dateString)),
// i.e. suppose DOM field date string is given in dd/mm/yyyy format:
// ea.addValueParser('dateparser', function(value, field) { // parameters: value - raw data string extracted by default from DOM element,
// // field - DOM element name for which parser was invoked
// var arr = value.split('/'); return new Date(arr[2], arr[1] - 1, arr[0]).getTime(); // return milliseconds since January 1, 1970, 00:00:00 UTC
// });
// multiple parsers can be registered at once when, separated by whitespace, are provided to name parameter, i.e. ea.addValueParser('p1 p2', ...
// finally, if value parser is registered under the name of some type, e.g. datetime, int32, etc., all DOM fields of such a type are going to be deserialized using such a parser
noConflict: function() {
if (window.ea === this) {
window.ea = backup;
}
return this;
}
},
logger = {
dump: function(message) {
if (api.settings.debug && console && typeof console.log === 'function') { // flush in debug mode only
console.log(message);
}
},
warn: function(message) {
if (console && typeof console.warn === 'function') {
console.warn(message);
}
},
fail: function(message) {
if (console && typeof console.error === 'function') {
console.error(message);
}
}
},
toolchain = {
methods: {},
addMethod: function(name, func) { // add multiple function signatures to methods object (methods overloading, based only on numbers of arguments)
var old = this.methods[name];
this.methods[name] = function() {
if (func.length === arguments.length) {
return func.apply(this, arguments);
}
if (typeof old === 'function') {
return old.apply(this, arguments);
}
};
},
registerMethods: function(model) {
var name, body;
this.initialize();
for (name in this.methods) {
if (this.methods.hasOwnProperty(name)) {
body = this.methods[name];
model[name] = body;
}
}
},
initialize: function() {
this.addMethod('Now', function() { // return milliseconds
return Date.now(); // now() is faster than new Date().getTime()
});
this.addMethod('Today', function() { // return milliseconds
return new Date(new Date().setHours(0, 0, 0, 0)).getTime();
});
this.addMethod('Date', function(year, month, day) { // months are 1-based, return milliseconds
return new Date(year, month - 1, day).getTime();
});
this.addMethod('Date', function(year, month, day, hour, minute, second) { // months are 1-based, return milliseconds
return new Date(year, month - 1, day, hour, minute, second).getTime();
});
this.addMethod('TimeSpan', function(days, hours, minutes, seconds) { // return milliseconds
return seconds * 1e3 + minutes * 6e4 + hours * 36e5 + days * 864e5;
});
this.addMethod('Length', function(str) {
return str !== null && str !== undefined ? str.length : 0;
});
this.addMethod('Trim', function(str) {
return str !== null && str !== undefined ? $.trim(str) : null;
});
this.addMethod('Concat', function(strA, strB) {
return [strA, strB].join('');
});
this.addMethod('Concat', function(strA, strB, strC) {
return [strA, strB, strC].join('');
});
this.addMethod('CompareOrdinal', function(strA, strB) {
if (strA === strB) {
return 0;
}
if (strA !== null && strB === null) {
return 1;
}
if (strA === null && strB !== null) {
return -1;
}
return strA > strB ? 1 : -1;
});
this.addMethod('CompareOrdinalIgnoreCase', function(strA, strB) {
strA = (strA !== null && strA !== undefined) ? strA.toLowerCase() : null;
strB = (strB !== null && strB !== undefined) ? strB.toLowerCase() : null;
return this.CompareOrdinal(strA, strB);
});
this.addMethod('StartsWith', function(str, prefix) {
return str !== null && str !== undefined && prefix !== null && prefix !== undefined && str.slice(0, prefix.length) === prefix;
});
this.addMethod('StartsWithIgnoreCase', function(str, prefix) {
str = (str !== null && str !== undefined) ? str.toLowerCase() : null;
prefix = (prefix !== null && prefix !== undefined) ? prefix.toLowerCase() : null;
return this.StartsWith(str, prefix);
});
this.addMethod('EndsWith', function(str, suffix) {
return str !== null && str !== undefined && suffix !== null && suffix !== undefined && str.slice(-suffix.length) === suffix;
});
this.addMethod('EndsWithIgnoreCase', function(str, suffix) {
str = (str !== null && str !== undefined) ? str.toLowerCase() : null;
suffix = (suffix !== null && suffix !== undefined) ? suffix.toLowerCase() : null;
return this.EndsWith(str, suffix);
});
this.addMethod('Contains', function(str, substr) {
return str !== null && str !== undefined && substr !== null && substr !== undefined && str.indexOf(substr) > -1;
});
this.addMethod('ContainsIgnoreCase', function(str, substr) {
str = (str !== null && str !== undefined) ? str.toLowerCase() : null;
substr = (substr !== null && substr !== undefined) ? substr.toLowerCase() : null;
return this.Contains(str, substr);
});
this.addMethod('IsNullOrWhiteSpace', function(str) {
return str === null || !/\S/.test(str);
});
this.addMethod('IsDigitChain', function(str) {
return /^\d+$/.test(str);
});
this.addMethod('IsNumber', function(str) {
return /^[\+-]?\d*\.?\d+(?:[eE][\+-]?\d+)?$/.test(str);
});
this.addMethod('IsEmail', function(str) {
// taken from HTML5 specification: http://www.w3.org/TR/html5/forms.html#e-mail-state-(type=email)
return /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(str);
});
this.addMethod('IsUrl', function(str) {
// contributed by Diego Perini: https://gist.github.com/dperini/729294 (https://mathiasbynens.be/demo/url-regex)
return /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/i.test(str);
});
this.addMethod('IsRegexMatch', function(str, regex) {
return str !== null && str !== undefined && regex !== null && regex !== undefined && new RegExp(regex).test(str);
});
this.addMethod('Guid', function(str) {
var guid = typeHelper.guid.tryParse(str);
if (guid.error) {
throw guid.msg;
}
return guid;
});
}
},
typeHelper = {
parsers: {},
addValueParser: function(name, func) {
$.each(name.split(/\s+/), function(idx, parser) {
if (/\S/.test(parser)) {
typeHelper.parsers[parser] = func;
}
});
},
array: {
contains: function(arr, item) {
var i = arr.length;
while (i--) {
if (arr[i] === item) {
return true;
}
}
return false;
}
},
object: {
keys: function(obj) {
var key, arr = [];
for (key in obj) {
if (obj.hasOwnProperty(key)) {
arr.push(key);
}
}
return arr;
},
tryParse: function(value) {
try {
return $.parseJSON(value);
} catch (ex) {
return { error: true, msg: 'Given value was not recognized as a valid JSON. ' + ex };
}
}
},
string: {
format: function(text, params) {
function makeParam(value) {
value = typeHelper.isObject(value) ? JSON.stringify(value, null, 4): value;
value = typeHelper.isString(value) ? value.replace(/\$/g, '$$$$'): value; // escape $ sign for string.replace()
return value;
}
function applyParam(text, param, idx) {
return text.replace(new RegExp('\\{' + idx + '\\}', 'gm'), param);
}
var i;
if (params instanceof Array) {
for (i = 0; i < params.length; i++) {
text = applyParam(text, makeParam(params[i]), i);
}
return text;
}
for (i = 0; i < arguments.length - 1; i++) {
text = applyParam(text, makeParam(arguments[i + 1]), i);
}
return text;
},
tryParse: function(value) {
if (typeHelper.isString(value)) {
return value;
}
if (value !== undefined && value !== null) {
return value.toString();
}
return { error: true, msg: 'Given value was not recognized as a valid string.' };
}
},
bool: {
tryParse: function(value) {
if (typeHelper.isBool(value)) {
return value;
}
if (typeHelper.isString(value)) {
value = $.trim(value).toLowerCase();
if (value === 'true' || value === 'false') {
return value === 'true';
}
}
return { error: true, msg: 'Given value was not recognized as a valid boolean.' };
}
},
number: {
tryParse: function(value) {
function isNumber(n) {
return typeHelper.isNumeric(parseFloat(n)) && isFinite(n);
}
if (isNumber(value)) {
return parseFloat(value);
}
return { error: true, msg: 'Given value was not recognized as a valid float.' };
}
},
timespan: {
tryParse: function(value) {
if (typeHelper.isTimeSpan(value)) {
var DAY = 2, HOUR = 3, MINUTE = 4, SECOND = 5, MILLISECOND = 6;
var match = /(\-)?(?:(\d*)\.)?(\d+)\:(\d+)(?:\:(\d+)\.?(\d{3})?)?/.exec(value);
var sign = (match[1] === '-') ? -1 : 1;
var d = {
days: typeHelper.number.tryParse(match[DAY] || 0) * sign,
hours: typeHelper.number.tryParse(match[HOUR] || 0) * sign,
minutes: typeHelper.number.tryParse(match[MINUTE] || 0) * sign,
seconds: typeHelper.number.tryParse(match[SECOND] || 0) * sign,
milliseconds: typeHelper.number.tryParse(match[MILLISECOND] || 0) * sign
};
var millisec = d.milliseconds +
d.seconds * 1e3 + // 1000
d.minutes * 6e4 + // 1000 * 60
d.hours * 36e5 + // 1000 * 60 * 60
d.days * 864e5; // 1000 * 60 * 60 * 24
return millisec;
}
return { error: true, msg: 'Given value was not recognized as a valid .NET style timespan string.' };
}
},
date: {
tryParse: function(value) {
if (typeHelper.isDate(value)) {
return value.getTime(); // return the time value in milliseconds
}
if (typeHelper.isString(value)) {
var millisec = Date.parse(value); // default parsing of string representing an RFC 2822 or ISO 8601 date
if (typeHelper.isNumeric(millisec)) {
return millisec;
}
}
return { error: true, msg: 'Given value was not recognized as a valid RFC 2822 or ISO 8601 date.' };
}
},
guid: {
tryParse: function(value) {
if (typeHelper.isGuid(value)) {
return value.toUpperCase();
}
return { error: true, msg: 'Given value was not recognized as a valid guid - guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).' };
}
},
isTimeSpan: function(value) {
return /(\-)?(?:(\d*)\.)?(\d+)\:(\d+)(?:\:(\d+)\.?(\d{3})?)?/.test(value); // regex for recognition of .NET style timespan string, taken from moment.js v2.9.0
},
isNumeric: function(value) {
return typeof value === 'number' && !isNaN(value);
},
isDate: function(value) {
return value instanceof Date;
},
isObject: function(value) {
return typeof value === 'object' || value instanceof Object;
},
isString: function(value) {
return typeof value === 'string' || value instanceof String;
},
isBool: function(value) {
return typeof value === 'boolean' || value instanceof Boolean;
},
isGuid: function(value) {
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value); // basic check
},
tryParse: function(value, type, field, parser) {
var parseFunc;
if (parser !== null && parser !== undefined) {
parseFunc = typeHelper.findValueParser(field, parser); // pointed by attribute custom field-specific parser lookup - highest parsing priority
if (!parseFunc.error) {
return parseFunc(value, field);
}
logger.warn(parseFunc.msg);
}
parseFunc = typeHelper.findValueParser(field, type); // custom type-specific parser lookup - secondary parsing priority
if (!parseFunc.error) {
logger.warn(typeHelper.string.format('Overriden {0} type parsing runs for {1} field. All fields of {0} type are going to be parsed using your value parser. If such a behaviour is unintentional, change the name of your value parser to one, which does not indicate at {0} (or any other) type name.', type, field));
return parseFunc(value, field);
}
return typeHelper.tryAutoParse(value, type); // built-in type-aware parser lookup - lowest parsing priority
},
tryAutoParse: function(value, type) {
switch (type) {
case 'timespan':
return typeHelper.timespan.tryParse(value);
case 'datetime':
return typeHelper.date.tryParse(value);
case 'numeric':
return typeHelper.number.tryParse(value);
case 'string':
return typeHelper.string.tryParse(value);
case 'bool':
return typeHelper.bool.tryParse(value);
case 'guid':
return typeHelper.guid.tryParse(value);
default:
return typeHelper.object.tryParse(value);
}
},
findValueParser: function(field, parser) {
var parseFunc = typeHelper.parsers[parser]; // custom parsing lookup
if (typeof parseFunc === 'function') {
return parseFunc;
}
return { error: true, msg: typeHelper.string.format('Custom value parser {0} not found. Consider its registration with ea.addValueParser(), or remove redundant ValueParser attribute from {1} model field.', parser, field) };
}
},
modelHelper = {
getPrefix: function(value) {
return value.substr(0, value.lastIndexOf('.') + 1);
},
extractValue: function(form, name, prefix, type, parser) {
function getValue(element) {
var elementType = element.attr('type');
switch (elementType) {
case 'checkbox':
if (element.length > 2) {
logger.warn(typeHelper.string.format('DOM field {0} is ambiguous (unless custom value parser is provided).', element.attr('name')));
}
return element.is(':checked');
case 'radio':
return element.filter(':checked').val();
default:
if (element.length > 1) {
logger.warn(typeHelper.string.format('DOM field {0} is ambiguous (unless custom value parser is provided).', element.attr('name')));
}
return element.val();
}
}
var field, fieldName, rawValue, parsedValue;
fieldName = prefix + name;
field = $(form).find(typeHelper.string.format(':input[name="{0}"]', fieldName));
if (field.length === 0) {
throw typeHelper.string.format('DOM field {0} not found.', fieldName);
}
rawValue = getValue(field);
if (rawValue === null || rawValue === undefined || rawValue === '') { // field value not set
return null;
}
parsedValue = typeHelper.tryParse(rawValue, type, fieldName, parser); // convert field value to required type
if (parsedValue !== null && parsedValue !== undefined && parsedValue.error) {
throw typeHelper.string.format('DOM field {0} value conversion to {1} failed. {2}', fieldName, type, parsedValue.msg);
}
return parsedValue;
},
deserializeObject: function(form, fieldsMap, constsMap, parsersMap, prefix) {
function buildField(fieldName, fieldValue, object) {
var props, parent, i, match, arridx;
props = fieldName.split('.');
parent = object;
for (i = 0; i < props.length - 1; i++) {
fieldName = props[i];
match = /^([a-z_0-9]+)\[([0-9]+)\]$/i.exec(fieldName); // check for array element access
if (match) {
fieldName = match[1];
arridx = match[2];
if (!parent.hasOwnProperty(fieldName)) {
parent[fieldName] = {};
}
parent[fieldName][arridx] = {};
parent = parent[fieldName][arridx];
continue;
}
if (!parent.hasOwnProperty(fieldName)) {
parent[fieldName] = {};
}
parent = parent[fieldName];
}
fieldName = props[props.length - 1];
parent[fieldName] = fieldValue;
}
var model = {}, name, type, value, parser;
for (name in fieldsMap) {
if (fieldsMap.hasOwnProperty(name)) {
type = fieldsMap[name];
parser = parsersMap[name];
value = this.extractValue(form, name, prefix, type, parser);
buildField(name, value, model);
}
}
for (name in constsMap) {
if (constsMap.hasOwnProperty(name)) {
value = constsMap[name];
buildField(name, value, model);
}
}
return model;
},
adjustGivenValue: function(value, element, params) {
value = element.type === 'checkbox' ? element.checked : value; // special treatment for checkbox, because when unchecked, false value should be retrieved instead of undefined
var field = element.name.replace(params.prefix, '');
var parser = params.parsersMap[field];
if (parser !== null && parser !== undefined) {
var parseFunc = typeHelper.findValueParser(element.name, parser); // pointed by attribute custom field-specific parser lookup - highest parsing priority
if (!parseFunc.error) {
return parseFunc(value, element.name);
}
logger.warn(parseFunc.msg);
}
return value;
},
ctxEval: function(exp, ctx) { // evaluates expression in the scope of context object
return (new Function('expression', 'context', 'with(context){return eval(expression)}'))(exp, ctx); // function constructor used on purpose (a hack), for 'with' statement not to collide with strict mode, which
// is applied to entire module scope (BTW 'use strict'; pragma intentionally not put to function constructor)
}
},
validationHelper = {
referencesMap: [],
collectReferences: function(fields, refField, prefix) {
var i, name;
for (i = 0; i < fields.length; i++) {
name = prefix + fields[i];
if (name !== refField) {
this.referencesMap[name] = this.referencesMap[name] || [];
if (!typeHelper.array.contains(this.referencesMap[name], refField)) {
this.referencesMap[name].push(refField);
}
}
}
},
validateReferences: function(name, form) {
var i, field, referencedFields;
referencedFields = this.referencesMap[name];
if (referencedFields !== undefined && referencedFields !== null) {
logger.dump(typeHelper.string.format('Validation triggered for following {0} dependencies: {1}.', name, referencedFields.join(', ')));
i = referencedFields.length;
while (i--) {
field = $(form).find(typeHelper.string.format(':input[data-val][name="{0}"]', referencedFields[i]));
if (field.length !== 0) {
field.valid();
}
}
} else {
logger.dump(typeHelper.string.format('No dependencies of {0} field detected.', name));
}
},
bindFields: function(form, force) { // attach validation handlers to dependency triggers (events) for some form elements
if (api.settings.dependencyTriggers !== null && api.settings.dependencyTriggers !== undefined && api.settings.dependencyTriggers !== '') {
var namespacedEvents = [];
$.each(api.settings.dependencyTriggers.split(/\s+/), function(idx, event) {
if (/\S/.test(event)) {
namespacedEvents.push(typeHelper.string.format('{0}.expressive.annotations', event));
}
});
// attach handlers to all inputs that do not have 'ea-triggers-bound' class (unless force is true)
$(form).find('input, select, textarea').not(function(idx, element) {
var bound = $(element).hasClass('ea-triggers-bound');
$(element).addClass('ea-triggers-bound');
return !force && bound;
}).on(namespacedEvents.join(' '), function(event) {
var field = $(this).attr('name');
logger.dump(typeHelper.string.format('Dependency validation trigger - {0} event, handled.', event.type));
validationHelper.validateReferences(field, form); // validate referenced fields only
});
}
}
},
buildAdapter = function(adapter, options) {
var rules = {
prefix: modelHelper.getPrefix(options.element.name),
form: options.form
};
for (var key in options.params) {
if (options.params.hasOwnProperty(key)) {
rules[key] = options.params[key] !== undefined ? $.parseJSON(options.params[key]) : {};
}
}
if (options.message) {
options.messages[adapter] = function(params, element) {
var matches, message, remaining, arg, leftBraces, rightBraces, length, left, right, param;
message = remaining = options.message;
while ((matches = /({+)[a-zA-Z_]+(?:(?:\.[a-zA-Z_])?[a-zA-Z0-9_]*)*(}+)/g.exec(remaining)) !== null) { // search for custom string format specifiers, e.g. {field} {field.field}, for field values to be extracted
arg = matches[0]; // braces can be escaped by double-braces, i.e. to output a { use {{ and to output a } use }}
leftBraces = matches[1];
rightBraces = matches[2];
if (leftBraces.length !== rightBraces.length)
throw 'Input string was not in a correct format.';
length = leftBraces.length;
// flatten each pair of braces (curly brackets) into single brace
left = new Array(parseInt(length / 2) + 1).join('{');
right = new Array(parseInt(length / 2) + 1).join('}');
param = arg.substr(length, arg.length - 2 * length);
if (length % 2 !== 0) // substitute param with respective value
param = modelHelper.extractValue(params.form, param, params.prefix, 'string', null);
message = message.replace(arg, typeHelper.string.format("{0}{1}{2}", left, param, right));
remaining = remaining.replace(arg, '');
}
return message;
};
}
validationHelper.bindFields(options.form);
validationHelper.collectReferences(typeHelper.object.keys(rules.fieldsMap), options.element.name, rules.prefix);
options.rules[adapter] = rules;
},
computeAssertThat = function(value, element, params) {
value = modelHelper.adjustGivenValue(value, element, params); // preprocess given value (here basically we are concerned about determining if such a value is null or not, to determine if the attribute
// logic should be invoked or not - full type-detection parsing is not required at this stage, but we may have to extract such a value using
// value parser, e.g. for an array which values are distracted among multiple fields)
if (!(value === undefined || value === null || value === '')) { // check if the field value is set (continue if so, otherwise skip condition verification)
var model = modelHelper.deserializeObject(params.form, params.fieldsMap, params.constsMap, params.parsersMap, params.prefix);
toolchain.registerMethods(model);
logger.dump(typeHelper.string.format('AssertThat expression of {0} field:\n{1}\nwill be executed within following context (methods hidden):\n{2}', element.name, params.expression, model));
if (!modelHelper.ctxEval(params.expression, model)) { // check if the assertion condition is not satisfied
return false; // assertion not satisfied => notify
}
}
return true;
},
computeRequiredIf = function(value, element, params) {
value = modelHelper.adjustGivenValue(value, element, params);
if(value === undefined || value === null || value === '' // check if the field value is not set (undefined, null or empty string treated at client as null at server)
|| (!/\S/.test(value) && !params.allowEmpty)) {
var model = modelHelper.deserializeObject(params.form, params.fieldsMap, params.constsMap, params.parsersMap, params.prefix);
toolchain.registerMethods(model);
logger.dump(typeHelper.string.format('RequiredIf expression of {0} field:\n{1}\nwill be executed within following context (methods hidden):\n{2}', element.name, params.expression, model));
if (modelHelper.ctxEval(params.expression, model)) { // check if the requirement condition is satisfied
return false; // requirement confirmed => notify
}
}
return true;
},
annotations = ' abcdefghijklmnopqrstuvwxyz'; // suffixes for attributes annotating single field multiple times
$.each(annotations.split(''), function() { // it would be ideal to have exactly as many handlers as there are unique annotations, but the number of annotations isn't known untill DOM is ready
var adapter = typeHelper.string.format('assertthat{0}', $.trim(this));
$.validator.unobtrusive.adapters.add(adapter, ['expression', 'fieldsMap', 'constsMap', 'parsersMap'], function(options) {
buildAdapter(adapter, options);
});
});
$.each(annotations.split(''), function() {
var adapter = typeHelper.string.format('requiredif{0}', $.trim(this));
$.validator.unobtrusive.adapters.add(adapter, ['expression', 'fieldsMap', 'constsMap', 'parsersMap', 'allowEmpty'], function(options) {
buildAdapter(adapter, options);
});
});
$.each(annotations.split(''), function() {
var method = typeHelper.string.format('assertthat{0}', $.trim(this));
$.validator.addMethod(method, function(value, element, params) {
try {
return computeAssertThat(value, element, params);
} catch (ex) {
logger.fail(ex);
}
}, '');
});
$.each(annotations.split(''), function() {
var method = typeHelper.string.format('requiredif{0}', $.trim(this));
$.validator.addMethod(method, function(value, element, params) {
try {
return computeRequiredIf(value, element, params);
} catch (ex) {
logger.fail(ex);
}
}, '');
});
// for testing only (section to be removed in release code)
api.___6BE7863DC1DB4AFAA61BB53FF97FE169 = {
logger: logger,
toolchain: toolchain,
typeHelper: typeHelper,
modelHelper: modelHelper,
validationHelper: validationHelper,
computeRequiredIf: computeRequiredIf,
computeAssertThat: computeAssertThat
};
// --------------------------------------------------------
window.ea = api; // expose some tiny api to the ea global object
}(jQuery, window));