-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphaistos.js
632 lines (508 loc) · 20.4 KB
/
phaistos.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
/*
PHAISTOS (Front-end for J.A.P with integrated subset of Vulcan & Sensei from micro-MVC)
File name: phaistos.js (Version: 1.3)
Description: This file contains the PHAISTOS library.
Coded by George Delaportas (G0D)
Copyright (C) 2015 - 2025
Open Software License (OSL 3.0)
*/
// PHAISTOS
function phaistos()
{
// Vulcan
function vulcan()
{
function validation()
{
function alpha()
{
this.is_string = function(val)
{
return (typeof val === 'string');
};
}
function numerics()
{
var __self = this;
this.is_number = function(val)
{
if (self.validation.misc.is_array(val) && val.length === 0)
return false;
return (!isNaN(val - parseFloat(val)));
};
this.is_integer = function(val)
{
return (__self.is_number(val) && (val % 1 === 0) && !self.misc.contains('.', val.toString()));
};
}
function misc()
{
var __self = this;
this.is_undefined = function(val)
{
return (val === undefined);
};
this.is_nothing = function(val)
{
return (val === null || val === '');
};
this.is_bool = function(val)
{
return (typeof val === 'boolean');
};
this.is_array = function(val)
{
return (Array.isArray(val));
};
this.is_object = function(val)
{
return (typeof val === 'object');
};
this.is_invalid = function(val)
{
return (__self.is_undefined(val) || __self.is_nothing(val));
};
}
function utilities()
{
this.reg_exp = function(pattern, expression)
{
return pattern.test(String(expression)).toLowerCase();
};
}
this.alpha = new alpha();
this.numerics = new numerics();
this.misc = new misc();
this.utilities = new utilities();
}
function misc()
{
this.contains = function(subject, list)
{
return (list.indexOf(subject) !== -1);
};
}
function init()
{
self.validation = new validation();
self.misc = new misc();
}
var self = this;
// Intialize
init();
}
// Sensei
function sensei(title, message)
{
var __index = 0,
__stars = '',
utils = new vulcan();
if ((!utils.validation.misc.is_invalid(title) && !utils.validation.alpha.is_string(title)) ||
(!utils.validation.misc.is_invalid(message) && !utils.validation.alpha.is_string(message)))
return false;
for (__index = 0; __index < title.length - 2; __index++)
__stars += '*';
console.log('-------------------------- ' + title + ' --------------------------');
console.log(message);
console.log('-------------------------- ' + __stars + ' --------------------------');
console.log('');
return true;
}
// Scan for unknown keywords
function has_unknown_keywords(definition_model)
{
if (!utils.validation.misc.is_object(definition_model))
return false;
var __index = null,
__attribute = null,
__option = null,
__property = null;
for (__index in definition_model)
{
__attribute = definition_model[__index];
if (!utils.validation.misc.is_object(__attribute))
{
if (!utils.misc.contains(__index, def_keywords))
return true;
continue;
}
if ((utils.validation.misc.is_object(__attribute) && Object.getOwnPropertyNames(__attribute).length === 0) ||
(utils.validation.misc.is_array(__attribute) && __attribute.length === 0))
return true;
for (__option in __attribute)
{
if (utils.validation.misc.is_object(__attribute[__option]))
{
for (__property in __attribute[__option])
{
if (utils.validation.numerics.is_number(__option))
continue;
if (!utils.misc.contains(__property, def_keywords))
return true;
if (has_unknown_keywords(__attribute[__option][__property]))
return true;
}
}
else
{
if (!utils.misc.contains(__option, def_keywords))
return true;
if (has_unknown_keywords(__attribute[__option]))
return true;
}
}
}
return false;
}
// Define the configuration
this.define = function(definition_model)
{
if (!utils.validation.misc.is_object(definition_model))
{
sensei('PHAISTOS', 'Invalid definition model!');
return false;
}
if (definition_model.length === 0)
{
sensei('PHAISTOS', 'The definition model is null!');
return false;
}
if (has_unknown_keywords(definition_model))
{
sensei('PHAISTOS', 'The definition model contains unknown keywords!');
return false;
}
var __this_key = null,
__this_value = null;
is_model_defined = false;
if (definition_model.hasOwnProperty('ignore_keys_num') && !utils.validation.misc.is_bool(definition_model.ignore_keys_num))
{
sensei('PHAISTOS', 'Missing or invalid "ignore_keys_num" attribute!');
return false;
}
if (!definition_model.hasOwnProperty('arguments') || !utils.validation.misc.is_object(definition_model.arguments))
{
sensei('PHAISTOS', 'Missing or invalid "arguments" attribute!');
return false;
}
def_model_args = definition_model.arguments;
for (counter = 0; counter < def_model_args.length; counter++)
{
if (!utils.validation.misc.is_object(def_model_args[counter]))
{
sensei('PHAISTOS', 'Invalid JSON object in "arguments" attribute!');
return false;
}
if (!def_model_args[counter].hasOwnProperty('key') || !def_model_args[counter].hasOwnProperty('value'))
{
sensei('PHAISTOS', 'Missing "key" or "value" mandatory attributes!');
return false;
}
__this_key = def_model_args[counter].key;
__this_value = def_model_args[counter].value;
if (!utils.validation.misc.is_object(__this_key) || !utils.validation.misc.is_object(__this_value))
{
sensei('PHAISTOS', 'A "key" or "value" attribute does not point to a JSON object!');
return false;
}
if (!__this_key.hasOwnProperty('name') || !__this_key.hasOwnProperty('optional'))
{
sensei('PHAISTOS', 'Missing "name" or "optional" mandatory properties!');
return false;
}
if (!utils.validation.alpha.is_string(__this_key.name) || !utils.validation.misc.is_bool(__this_key.optional))
{
sensei('PHAISTOS', 'Invalid specification for "name" or "optional" property!');
return false;
}
if (!__this_value.hasOwnProperty('type'))
{
sensei('PHAISTOS', 'Missing "type" mandatory property!');
return false;
}
if (!utils.validation.alpha.is_string(__this_value.type) || !utils.misc.contains(__this_value.type, all_value_types))
{
sensei('PHAISTOS', 'Invalid specification for "type" property!');
return false;
}
if (__this_value.hasOwnProperty('choices'))
{
if (!utils.misc.contains(__this_value.type, types_with_choices))
{
sensei('PHAISTOS', 'This type does not support the "choices" option!');
return false;
}
if (!utils.validation.misc.is_array(__this_value.choices) || __this_value.choices.length < 1)
{
sensei('PHAISTOS', 'The "choices" option has to be an array with at least\none element!');
return false;
}
}
if (__this_value.hasOwnProperty('length'))
{
if (utils.misc.contains(__this_value.type, uncountable_value_types))
{
sensei('PHAISTOS', 'This type does not support the "length" option!');
return false;
}
if (!utils.validation.numerics.is_integer(__this_value.length) || __this_value.length < 1)
{
sensei('PHAISTOS', 'The "length" option has to be a positive integer!');
return false;
}
}
if (__this_value.hasOwnProperty('regex'))
{
if (utils.misc.contains(__this_value.type, uncountable_value_types) || __this_value.type === 'array')
{
sensei('PHAISTOS', 'This type does not support the "regex" option!');
return false;
}
if (!utils.validation.misc.is_object(__this_value.regex) || __this_value.regex === '')
{
sensei('PHAISTOS', 'Invalid "regex" option!');
return false;
}
}
}
is_model_defined = true;
json_def_model = definition_model;
return true;
};
// Validate configuration based on the definition model
this.validate = function(config)
{
if (!is_model_defined)
{
sensei('PHAISTOS', 'No definition model was specified!');
return false;
}
if (!utils.validation.misc.is_object(config))
{
sensei('PHAISTOS', 'Invalid JSON object!');
return false;
}
var __json_key = null,
__this_key = null,
__this_value = null,
__is_multiple_keys_array = false,
__keys_exist = 0,
__mandatory_keys_not_found = 0,
__model_keywords = [];
def_model_args = json_def_model.arguments;
if (utils.validation.misc.is_array(config))
__is_multiple_keys_array = true;
for (counter = 0; counter < def_model_args.length; counter++)
{
for (__json_key in def_model_args[counter])
{
if (!utils.validation.misc.is_undefined(def_model_args[counter][__json_key].name))
__model_keywords.push(def_model_args[counter][__json_key].name);
}
}
for (__json_key in config)
{
if (__is_multiple_keys_array)
__mandatory_keys_not_found = 0;
for (counter = 0; counter < def_model_args.length; counter++)
{
__this_key = def_model_args[counter].key;
if (__is_multiple_keys_array)
{
for (__this_value in config[__json_key])
{
if (!utils.misc.contains(__this_value, __model_keywords))
{
sensei('PHAISTOS', 'Unknown keyword: "' + __this_value + '" in the configuration model!');
return false;
}
}
}
else
{
if (!utils.misc.contains(__json_key, __model_keywords))
{
sensei('PHAISTOS', 'Unknown keyword: "' + __json_key + '" in the configuration model!');
return false;
}
}
if (__this_key.optional === false)
{
if (__is_multiple_keys_array)
{
if (!config[__json_key].hasOwnProperty(__this_key.name))
__mandatory_keys_not_found++;
}
else
{
if (!config.hasOwnProperty(__this_key.name))
__mandatory_keys_not_found++;
}
}
}
if (__is_multiple_keys_array && __mandatory_keys_not_found > 0)
break;
__keys_exist++;
}
if ((!json_def_model.hasOwnProperty('ignore_keys_num') || json_def_model.ignore_keys_num === false) &&
__mandatory_keys_not_found > 0)
{
sensei('PHAISTOS', 'Mandatory properties are missing!');
return false;
}
if (__keys_exist === 0)
{
sensei('PHAISTOS', 'The JSON object is null!');
return false;
}
for (counter = 0; counter < def_model_args.length; counter++)
{
__this_key = def_model_args[counter].key;
__this_value = def_model_args[counter].value;
if (__this_value.type !== '*')
{
if (__this_value.type === 'null')
{
if (config[__this_key.name] !== null)
{
sensei('PHAISTOS', 'Argument: "' + __this_key.name + '" accepts only "null" values!');
return false;
}
}
else if (__this_value.type === 'number')
{
if (__is_multiple_keys_array)
{
for (__json_key in config)
{
if (utils.validation.misc.is_undefined(config[__json_key][__this_key.name]))
continue;
if (utils.validation.misc.is_nothing(config[__json_key][__this_key.name].toString().trim()) ||
!utils.validation.numerics.is_number(Number(config[__json_key][__this_key.name])))
{
sensei('PHAISTOS', 'Argument: "' + __this_key.name + '" accepts only "numeric" values!');
return false;
}
}
}
else
{
if (utils.validation.misc.is_undefined(config[__this_key.name]))
continue;
if (utils.validation.misc.is_nothing(config[__this_key.name].toString().trim()) ||
!utils.validation.numerics.is_number(Number(config[__this_key.name])))
{
sensei('PHAISTOS', 'Argument: "' + __this_key.name + '" accepts only "numeric" values!');
return false;
}
}
}
else if (__this_value.type === 'array')
{
if (!utils.validation.misc.is_array(config[__this_key.name]))
{
sensei('PHAISTOS', 'Argument: "' + __this_key.name + '" accepts only "array" values!');
return false;
}
}
else
{
if (__is_multiple_keys_array)
{
for (__json_key in config)
{
if (utils.validation.misc.is_undefined(config[__json_key][__this_key.name]))
continue;
if (typeof config[__json_key][__this_key.name] !== __this_value.type)
{
sensei('PHAISTOS', 'Argument: "' + __this_key.name + '" has a type mismatch!');
return false;
}
}
}
else
{
if (utils.validation.misc.is_undefined(config[__this_key.name]))
continue;
if (typeof config[__this_key.name] !== __this_value.type)
{
sensei('PHAISTOS', 'Argument: "' + __this_key.name + '" has a type mismatch!');
return false;
}
}
}
}
if (__this_value.hasOwnProperty('choices'))
{
if (__is_multiple_keys_array)
{
for (__json_key in config)
{
if (utils.validation.misc.is_undefined(config[__json_key][__this_key.name]))
continue;
if (!utils.misc.contains(config[__json_key][__this_key.name], __this_value.choices))
{
sensei('PHAISTOS', 'Argument: "' + __this_key.name + '" does not contain any\ndefined choices!');
return false;
}
}
}
else
{
if (utils.validation.misc.is_undefined(config[__this_key.name]))
continue;
if (!utils.misc.contains(config[__this_key.name], __this_value.choices))
{
sensei('PHAISTOS', 'Argument: "' + __this_key.name + '" does not contain any\ndefined choices!');
return false;
}
}
}
if (__this_value.hasOwnProperty('length'))
{
if (__this_value.type === 'array')
{
if (config[__this_key.name].length > __this_value.length)
{
sensei('PHAISTOS', 'Argument: "' + __this_key.name + '" has exceeded the defined length!');
return false;
}
}
else
{
if (config[__this_key.name].toString().length > __this_value.length)
{
sensei('PHAISTOS', 'Argument: "' + __this_key.name + '" has exceeded the defined length!');
return false;
}
}
}
if (__this_value.hasOwnProperty('regex'))
{
if (!utils.validation.utilities.reg_exp(__this_value.regex, config[__this_key.name]))
{
sensei('PHAISTOS', 'Argument: "' + __this_key.name + '" has not matched the specified regex!');
return false;
}
}
}
return true;
};
// Define and validate at once
this.verify = function(definition_model, config)
{
if (self.define(definition_model))
return self.validate(config);
return false;
};
var self = this,
is_model_defined = false,
counter = 0,
json_def_model = null,
def_model_args = null,
def_keywords = ['ignore_keys_num', 'arguments', 'key', 'value', 'name', 'optional', 'type', 'choices', 'length', 'regex'],
all_value_types = ['number', 'string', 'boolean', 'array', 'object', 'function', 'null', '*'],
uncountable_value_types = ['boolean', 'object', 'function', 'null', '*'],
types_with_choices = ['number', 'string', 'array'],
utils = new vulcan();
}