forked from eclipse-thingweb/playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.js
1311 lines (1200 loc) · 49.3 KB
/
shared.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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
* Document License (2015-05-13) which is available at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
*/
/**
* This file contains functions, which are required by the core package as
* well as by the assertions package
*/
// A special JSON validator that is used only to check whether the given object has duplicate keys.
// The standard library doesn't detect duplicate keys and overwrites the first one with the second one.
// TODO: replace with jsonlint ??
const jsonValidator = require("json-dup-key-validator");
// This is used to validate if the multi language JSON keys are valid according to the BCP47 spec
const bcp47pattern =
/^(?:(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)|(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang))$|^((?:[a-z]{2,3}(?:(?:-[a-z]{3}){1,3})?)|[a-z]{4}|[a-z]{5,8})(?:-([a-z]{4}))?(?:-([a-z]{2}|\d{3}))?((?:-(?:[\da-z]{5,8}|\d[\da-z]{3}))*)?((?:-[\da-wy-z](?:-[\da-z]{2,8})+)*)?(-x(?:-[\da-z]{1,8})+)?$|^(x(?:-[\da-z]{1,8})+)$/i; // eslint-disable-line max-len
const fetch = require("node-fetch");
const fs = require("fs");
const jsonDiff = require("json-diff");
module.exports = {
checkPropUniqueness,
checkSecurity,
checkMultiLangConsistency,
checkLinksRelTypeCount,
checkUriSecurity,
checkTmOptionalPointer,
checkLinkedAffordances,
checkLinkedStructure,
};
/**
* This function returns part of the object given in param with the value found when resolving the path. Similar to JSON Pointers.
* In case no path is found, the param defaultValue is echoed back
* Taken from
* https://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-and-arrays-by-string-path/6491621#6491621
* @param {object} object
* @param {string} path
* @param {any} defaultValue
* @return {object}
**/
const resolvePath = (object, path, defaultValue) =>
path
.split(/[\.\[\]\'\"]/)
.filter((p) => p)
.reduce((o, p) => (o ? o[p] : defaultValue), object);
// -------------------------------------------------- checkPropUniqueness
/**
* Checking whether in one interaction pattern there are duplicate names, e.g. two properties called temp
* However, if there are no properties then it is not-impl
*
* @param {string} tdString The Td under test as string
*/
function checkPropUniqueness(tdString) {
const results = [];
// jsonvalidator throws an error if there are duplicate names in the interaction level
try {
jsonValidator.parse(tdString, false);
const td = JSON.parse(tdString);
// no problem in interaction level
let tdInteractions = [];
// checking whether there are properties at all, if not uniqueness is not impl
if (td.hasOwnProperty("properties")) {
tdInteractions = tdInteractions.concat(Object.keys(td.properties));
// then we can add unique properties pass
results.push({
ID: "td-properties_uniqueness",
Status: "pass",
Comment: "",
});
} else {
// then we add unique properties as not impl
results.push({
ID: "td-properties_uniqueness",
Status: "not-impl",
Comment: "no properties",
});
}
// similar to just before, checking whether there are actions at all, if not uniqueness is not impl
if (td.hasOwnProperty("actions")) {
tdInteractions = tdInteractions.concat(Object.keys(td.actions));
results.push({
ID: "td-actions_uniqueness",
Status: "pass",
Comment: "",
});
} else {
// then we add unique actions as not impl
results.push({
ID: "td-actions_uniqueness",
Status: "not-impl",
Comment: "no actions",
});
}
// similar to just before, checking whether there are events at all, if not uniqueness is not impl
if (td.hasOwnProperty("events")) {
tdInteractions = tdInteractions.concat(Object.keys(td.events));
results.push({
ID: "td-events_uniqueness",
Status: "pass",
Comment: "",
});
} else {
// then we add unique events as not impl
results.push({
ID: "td-events_uniqueness",
Status: "not-impl",
Comment: "no events",
});
}
return results;
} catch (error) {
// there is a duplicate somewhere
// convert it into string to be able to process it
// error is of form = Error: Syntax error: duplicated keys "overheating" near ting": {
const errorString = error.toString();
// to get the name, we need to remove the quotes around it
const startQuote = errorString.indexOf('"');
// slice to remove the part before the quote
const restString = errorString.slice(startQuote + 1);
// find where the interaction name ends
const endQuote = restString.indexOf('"');
// finally get the interaction name
const interactionName = restString.slice(0, endQuote);
// trying to find where this interaction is and put results accordingly
const td = JSON.parse(tdString);
if (td.hasOwnProperty("properties")) {
const tdProperties = td.properties;
if (tdProperties.hasOwnProperty(interactionName)) {
// duplicate was at properties but that fails the td-unique identifiers as well
results.push({
ID: "td-properties_uniqueness",
Status: "fail",
Comment: "duplicate property names at " + td.title,
});
// since JSON.parse removes duplicates, we replace the duplicate name with duplicateName
tdString = tdString.replace(interactionName, "duplicateName");
} else {
// there is duplicate but not here, so pass
results.push({
ID: "td-properties_uniqueness",
Status: "pass",
Comment: "",
});
}
} else {
results.push({
ID: "td-properties_uniqueness",
Status: "not-impl",
Comment: "no properties",
});
}
if (td.hasOwnProperty("actions")) {
const tdActions = td.actions;
if (tdActions.hasOwnProperty(interactionName)) {
// duplicate was at actions but that fails the td-unique identifiers as well
results.push({
ID: "td-actions_uniqueness",
Status: "fail",
Comment: "duplicate action names at " + td.title,
});
// since JSON.parse removes duplicates, we replace the duplicate name with duplicateName
tdString = tdString.replace(interactionName, "duplicateName");
} else {
results.push({
ID: "td-actions_uniqueness",
Status: "pass",
Comment: "",
});
}
} else {
results.push({
ID: "td-actions_uniqueness",
Status: "not-impl",
Comment: "no actions",
});
}
if (td.hasOwnProperty("events")) {
const tdEvents = td.events;
if (tdEvents.hasOwnProperty(interactionName)) {
// duplicate was at events but that fails the td-unique identifiers as well
results.push({
ID: "td-events_uniqueness",
Status: "fail",
Comment: "duplicate event names at " + td.title,
});
// since JSON.parse removes duplicates, we replace the duplicate name with duplicateName
tdString = tdString.replace(interactionName, "duplicateName");
} else {
results.push({
ID: "td-events_uniqueness",
Status: "pass",
Comment: "",
});
}
} else {
results.push({
ID: "td-events_uniqueness",
Status: "not-impl",
Comment: "no events",
});
}
return results;
}
}
// -------------------------------------------------- checkSecurity
/**
* check if used Security definitions are properly defined previously
* @param {object} td The TD to do assertion tests
*/
function checkSecurity(td) {
const results = [];
if (td.hasOwnProperty("securityDefinitions")) {
const securityDefinitionsObject = td.securityDefinitions;
const securityDefinitions = Object.keys(securityDefinitionsObject);
const rootSecurity = td.security;
if (securityContains(securityDefinitions, rootSecurity)) {
// all good
} else {
results.push({
ID: "td-security-scheme-name",
Status: "fail",
Comment: "used a non defined security scheme in root level at " + td.title,
});
return results;
}
if (td.hasOwnProperty("properties")) {
// checking security in property level
tdProperties = Object.keys(td.properties);
for (let i = 0; i < tdProperties.length; i++) {
const curPropertyName = tdProperties[i];
const curProperty = td.properties[curPropertyName];
// checking security in forms level
const curForms = curProperty.forms;
for (let j = 0; j < curForms.length; j++) {
const curForm = curForms[j];
if (curForm.hasOwnProperty("security")) {
const curSecurity = curForm.security;
if (securityContains(securityDefinitions, curSecurity)) {
// all good
} else {
results.push({
ID: "td-security-scheme-name",
Status: "fail",
Comment: "used a non defined security scheme in a property form at " + td.title,
});
return results;
}
}
}
}
}
if (td.hasOwnProperty("actions")) {
// checking security in action level
tdActions = Object.keys(td.actions);
for (let i = 0; i < tdActions.length; i++) {
const curActionName = tdActions[i];
const curAction = td.actions[curActionName];
// checking security in forms level
const curForms = curAction.forms;
for (let j = 0; j < curForms.length; j++) {
const curForm = curForms[j];
if (curForm.hasOwnProperty("security")) {
const curSecurity = curForm.security;
if (securityContains(securityDefinitions, curSecurity)) {
// all good
} else {
results.push({
ID: "td-security-scheme-name",
Status: "fail",
Comment: "used a non defined security scheme in an action form at " + td.title,
});
return results;
}
}
}
}
}
if (td.hasOwnProperty("events")) {
// checking security in event level
tdEvents = Object.keys(td.events);
for (let i = 0; i < tdEvents.length; i++) {
const curEventName = tdEvents[i];
const curEvent = td.events[curEventName];
// checking security in forms level
const curForms = curEvent.forms;
for (let j = 0; j < curForms.length; j++) {
const curForm = curForms[j];
if (curForm.hasOwnProperty("security")) {
const curSecurity = curForm.security;
if (securityContains(securityDefinitions, curSecurity)) {
// all good
} else {
results.push({
ID: "td-security-scheme-name",
Status: "fail",
Comment: "used a non defined security scheme in an event form at " + td.title,
});
return results;
}
}
}
}
}
// no security used non defined scheme, passed test
results.push({
ID: "td-security-scheme-name",
Status: "pass",
});
return results;
}
return results;
}
/**
* subfunction of checkSecurity
* security anywhere could be a string or array. Convert string to array
*
* @param {*} parent
* @param {string|Array<string>} child
*/
function securityContains(parent, child) {
if (typeof child === "string") {
child = [child];
}
return child.every((elem) => parent.indexOf(elem) > -1);
}
// -------------------------------------------------- checkMultiLangConsistency
/**
* this checks whether all titles and descriptions have the same language fields
* so the object keys of a titles and of a descriptions should be the same already,
* then everywhere else they should also be the same
*
* first collect them all, and then compare them
*
* @param {object} td The TD to do assertion tests
*/
function checkMultiLangConsistency(td) {
const results = [];
const multiLang = []; // an array of arrays where each small array has the multilang keys
const isTdTitlesDescriptions = []; // an array of boolean values to check td-titles-descriptions assertion
// checking root
if (td.hasOwnProperty("titles")) {
const rootTitlesObject = td.titles;
const rootTitles = Object.keys(rootTitlesObject);
multiLang.push(rootTitles);
// checking for td-titles-descriptions
isTdTitlesDescriptions.push({ ["root_title"]: isStringObjectKeyValue(td.title, rootTitlesObject) });
}
if (td.hasOwnProperty("descriptions")) {
const rootDescriptionsObject = td.descriptions;
const rootDescriptions = Object.keys(rootDescriptionsObject);
multiLang.push(rootDescriptions);
// check whether description exists in descriptions
if (td.hasOwnProperty("description")) {
isTdTitlesDescriptions.push({
["root_description"]: isStringObjectKeyValue(td.description, rootDescriptionsObject),
});
}
}
// checking inside each interaction
if (td.hasOwnProperty("properties")) {
// checking security in property level
tdProperties = Object.keys(td.properties);
for (let i = 0; i < tdProperties.length; i++) {
const curPropertyName = tdProperties[i];
const curProperty = td.properties[curPropertyName];
if (curProperty.hasOwnProperty("titles")) {
const titlesKeys = Object.keys(curProperty.titles);
multiLang.push(titlesKeys);
// checking if title exists in titles
if (curProperty.hasOwnProperty("title")) {
isTdTitlesDescriptions.push({
["property_" + curPropertyName + "_title"]: isStringObjectKeyValue(
curProperty.title,
curProperty.titles
),
});
}
}
if (curProperty.hasOwnProperty("descriptions")) {
const descriptionsKeys = Object.keys(curProperty.descriptions);
multiLang.push(descriptionsKeys);
// checking if description exists in descriptions
if (curProperty.hasOwnProperty("description")) {
isTdTitlesDescriptions.push({
["property_" + curPropertyName + "_desc"]: isStringObjectKeyValue(
curProperty.description,
curProperty.descriptions
),
});
}
}
}
}
if (td.hasOwnProperty("actions")) {
// checking security in action level
tdActions = Object.keys(td.actions);
for (let i = 0; i < tdActions.length; i++) {
const curActionName = tdActions[i];
const curAction = td.actions[curActionName];
if (curAction.hasOwnProperty("titles")) {
const titlesKeys = Object.keys(curAction.titles);
multiLang.push(titlesKeys);
// checking if title exists in titles
if (curAction.hasOwnProperty("title")) {
isTdTitlesDescriptions.push({
["action_" + curActionName + "_title"]: isStringObjectKeyValue(
curAction.title,
curAction.titles
),
});
}
}
if (curAction.hasOwnProperty("descriptions")) {
const descriptionsKeys = Object.keys(curAction.descriptions);
multiLang.push(descriptionsKeys);
// checking if description exists in descriptions
if (curAction.hasOwnProperty("description")) {
isTdTitlesDescriptions.push({
["action_" + curActionName + "_desc"]: isStringObjectKeyValue(
curAction.description,
curAction.descriptions
),
});
}
}
}
}
if (td.hasOwnProperty("events")) {
// checking security in event level
tdEvents = Object.keys(td.events);
for (let i = 0; i < tdEvents.length; i++) {
const curEventName = tdEvents[i];
const curEvent = td.events[curEventName];
if (curEvent.hasOwnProperty("titles")) {
const titlesKeys = Object.keys(curEvent.titles);
multiLang.push(titlesKeys);
// checking if title exists in titles
if (curEvent.hasOwnProperty("title")) {
isTdTitlesDescriptions.push({
["event_" + curEventName + "_title"]: isStringObjectKeyValue(curEvent.title, curEvent.titles),
});
}
}
if (curEvent.hasOwnProperty("descriptions")) {
const descriptionsKeys = Object.keys(curEvent.descriptions);
multiLang.push(descriptionsKeys);
// checking if description exists in descriptions
if (curEvent.hasOwnProperty("description")) {
isTdTitlesDescriptions.push({
["event_" + curEventName + "_desc"]: isStringObjectKeyValue(
curEvent.description,
curEvent.descriptions
),
});
}
}
}
}
if (arrayArraysItemsEqual(multiLang)) {
results.push({
ID: "td-multi-languages-consistent",
Status: "pass",
});
} else {
results.push({
ID: "td-multi-languages-consistent",
Status: "fail",
Comment: "not all multilang objects have same language tags at " + td.title,
});
}
const flatArray = []; // this is multiLang but flat, so just a single array.
// This way we can have scan the whole thing at once and then find the element that is not bcp47
for (let index = 0; index < multiLang.length; index++) {
let arrayElement = multiLang[index];
arrayElement = JSON.parse(arrayElement);
for (let e = 0; e < arrayElement.length; e++) {
const stringElement = arrayElement[e];
flatArray.push(stringElement);
}
}
const isBCP47 = checkBCP47array(flatArray);
if (isBCP47 === "ok") {
results.push({
ID: "td-multilanguage-language-tag",
Status: "pass",
});
} else {
results.push({
ID: "td-multilanguage-language-tag",
Status: "fail",
Comment: isBCP47 + " is not a BCP47 tag at " + td.title,
});
}
// // checking td-context-default-language-direction-script assertion
// results.push({
// "ID": "td-context-default-language-direction-script",
// "Status": checkAzeri(flatArray)
// })
// checking td-titles-descriptions assertion
// if there are no multilang, then it is not impl
if (isTdTitlesDescriptions.length === 0) {
results.push({
ID: "td-titles-descriptions",
Status: "not-impl",
Comment: "no multilang objects in the td",
});
return results;
}
// if at some point there was a false result, it is a fail
for (let index = 0; index < isTdTitlesDescriptions.length; index++) {
const element = isTdTitlesDescriptions[index];
const elementName = Object.keys(element);
if (element[elementName]) {
// do nothing it is correct
} else {
results.push({
ID: "td-titles-descriptions",
Status: "fail",
Comment: elementName + " is not on the multilang object at the same level at " + td.title,
});
return results;
}
}
// there was no problem, so just put pass
results.push({
ID: "td-titles-descriptions",
Status: "pass",
});
// ? nothing after this, there is return above
return results;
}
/**
* subfunction of checkMultiLangConsistency
* checks if an array that contains only arrays as items is composed of same items
*
* @param {Array<object>} myArray The array to check
*/
function arrayArraysItemsEqual(myArray) {
if (myArray.length === 0) return true;
// first stringify each array item
for (let i = myArray.length; i--; ) {
myArray[i] = JSON.stringify(myArray[i]);
}
for (let i = myArray.length; i--; ) {
if (i === 0) {
return true;
}
if (myArray[i] !== myArray[i - 1]) {
return false;
}
}
}
/**
* subfunction of checkMultiLangConsistency
* checks whether the items of an array, which must be strings, are valid language tags
*
* @param {Array<string>} myArray The array, which items are to be checked
*/
function checkBCP47array(myArray) {
// return tag name if one is not valid during the check
for (let index = 0; index < myArray.length; index++) {
const element = myArray[index];
if (bcp47pattern.test(element)) {
// keep going
} else {
return element;
}
}
// return true if reached the end
return "ok";
}
/**
* subfunction of checkMultiLangConsistency
* checks whether a given string exist as the value of key in an object
*
* @param {string} searchedString
* @param {object} searchedObject
*/
function isStringObjectKeyValue(searchedString, searchedObject) {
const objKeys = Object.keys(searchedObject);
if (objKeys.length === 0) return false; // if the object is empty, then the string cannot exist here
for (let index = 0; index < objKeys.length; index++) {
const element = objKeys[index];
if (searchedObject[element] === searchedString) {
return true; // found where the string is in the object
} else {
// nothing keep going, maybe in another key
}
}
return false;
}
/**
* subfunction of checkMultiLangConsistency
* checks whether an azeri language tag also specifies the version (Latn or Arab).
* basically if the language is called "az", it is invalid, if it is az-Latn or az-Arab it is valid.
*
* @param {Array<string>} myMultiLangArray The language array to check
*/
function checkAzeri(myMultiLangArray) {
for (let index = 0; index < myMultiLangArray.length; index++) {
const element = myMultiLangArray[index];
if (element === "az") {
return "fail";
} else if (element === "az-Latn" || element === "az-Arab") {
return "pass";
}
}
// no azeri, so it is not implemented
return "not-impl";
}
// --------------------------------------------------
// -------------------------------------------------- checkLinksRelTypeCount
/**
* this checks whether rel:type appears only once in the links array
*
* @param {object} td The TD to do assertion tests
*/
function checkLinksRelTypeCount(td) {
const results = [];
if (td.hasOwnProperty("links")) {
// links exist, check if there is rel type
let typeCount = 0;
for (let i = 0; i < td.links.length; i++) {
const element = td.links[i];
if (element.hasOwnProperty("rel")) {
if (element.rel === "type") {
typeCount++;
}
}
}
if (typeCount === 0) {
results.push({
ID: "tm-rel-type-maximum",
Status: "not-impl",
Comment: "no rel:type in any link",
});
} else if (typeCount === 1) {
results.push({
ID: "tm-rel-type-maximum",
Status: "pass",
Comment: "",
});
} else {
results.push({
ID: "tm-rel-type-maximum",
Status: "fail",
Comment: "too many rel:type in links array at " + td.title,
});
}
} else {
results.push({
ID: "tm-rel-type-maximum",
Status: "not-impl",
Comment: "no links array in the td",
});
}
return results;
}
/**
* When you have apikey security with the key in uri, you put the name of the urivariable in the name field in
* securityDefinitions. Ideally, that name appears in href as a uriVariable. See uriSecurity example
* td-security-in-uri-variable: The URIs provided in interactions where a security scheme using uri as the value for
* in MUST be a URI template including the defined variable.
* Additionally, this also checks that the uriVariable used in the security does not conflict with ones for the TD
* td-security-uri-variables-distinct: The names of URI variables declared in a SecurityScheme MUST be distinct from
* all other URI variables declared in the TD.
* @param {object} td The TD to do assertion tests
*/
function checkUriSecurity(td) {
const results = [];
if (td.hasOwnProperty("securityDefinitions")) {
const securityDefinitionsObject = td.securityDefinitions;
const securityDefinitionsNames = Object.keys(securityDefinitionsObject);
const securityUriVariables = [];
for (let index = 0; index < securityDefinitionsNames.length; index++) {
const curSecurityDefinition = securityDefinitionsObject[securityDefinitionsNames[index]];
if (curSecurityDefinition.scheme === "apikey") {
if (curSecurityDefinition.hasOwnProperty("in")) {
if (curSecurityDefinition.in === "uri") {
if (curSecurityDefinition.hasOwnProperty("name")) {
securityUriVariables.push(curSecurityDefinition.name);
}
}
}
}
}
if (securityUriVariables.length === 0) {
// we could not find any
results.push({
ID: "td-security-in-uri-variable",
Status: "not-impl",
Comment: "no use of name in a uri apikey scheme",
});
results.push({
ID: "td-security-uri-variables-distinct",
Status: "not-impl",
Comment: "no use of name in a uri apikey scheme",
});
return results;
} else {
let uriVariablesResult = "not-impl";
let uriVariablesDistinctResult = "not-impl";
let rootUriVariables = [];
if (td.hasOwnProperty("uriVariables")) {
rootUriVariables = Object.keys(td.uriVariables);
}
if (td.hasOwnProperty("properties")) {
// checking security in property level
tdProperties = Object.keys(td.properties);
for (let i = 0; i < tdProperties.length; i++) {
const curPropertyName = tdProperties[i];
const curProperty = td.properties[curPropertyName];
// checking href with uriVariable in forms level
const curForms = curProperty.forms;
for (let j = 0; j < curForms.length; j++) {
const curForm = curForms[j];
if (curForm.hasOwnProperty("href")) {
const curHref = curForm.href;
// bottom thing is taken from https://stackoverflow.com/a/5582621/3806426
if (securityUriVariables.some((v) => curHref.includes(v))) {
// There's at least one
if (uriVariablesResult !== "fail") {
uriVariablesResult = "pass";
}
}
}
}
// part for the check of td-security-uri-variables-distinct
if (curProperty.hasOwnProperty("uriVariables")) {
curPropertyUriVariables = Object.keys(curProperty.uriVariables);
curPropertyUriVariables.push(...rootUriVariables);
if (curPropertyUriVariables.length > 0) {
// there are urivariables somewhere at least
// below is from https://stackoverflow.com/a/1885569/3806426
const filteredArray = curPropertyUriVariables.filter((value) =>
securityUriVariables.includes(value)
);
// console.log(curPropertyUriVariables,"\n",securityUriVariables,"\n",filteredArray)
if (filteredArray.length > 0) {
uriVariablesDistinctResult = "fail";
} else {
if (uriVariablesDistinctResult !== "fail") {
uriVariablesDistinctResult = "pass";
}
}
} // otherwise not-impl stays
} else {
// even if there are no urivariables in affordances, the security urivariables is distinct
// fixes https://github.com/thingweb/thingweb-playground/issues/422
if (uriVariablesDistinctResult !== "fail") {
uriVariablesDistinctResult = "pass";
}
}
}
}
if (td.hasOwnProperty("actions")) {
// checking security in property level
tdActions = Object.keys(td.actions);
for (let i = 0; i < tdActions.length; i++) {
const curActionName = tdActions[i];
const curAction = td.actions[curActionName];
// checking href with uriVariable in forms level
const curForms = curAction.forms;
for (let j = 0; j < curForms.length; j++) {
const curForm = curForms[j];
if (curForm.hasOwnProperty("href")) {
const curHref = curForm.href;
// bottom thing is taken from https://stackoverflow.com/a/5582621/3806426
if (securityUriVariables.some((v) => curHref.includes(v))) {
// There's at least one
if (uriVariablesResult !== "fail") {
uriVariablesResult = "pass";
}
}
}
}
// part for the check of td-security-uri-variables-distinct
if (curAction.hasOwnProperty("uriVariables")) {
curActionUriVariables = Object.keys(curAction.uriVariables);
curActionUriVariables.push(...rootUriVariables);
if (curActionUriVariables.length > 0) {
// there are urivariables somewhere at least
// below is from https://stackoverflow.com/a/1885569/3806426
const filteredArray = curActionUriVariables.filter((value) =>
securityUriVariables.includes(value)
);
// console.log(curActionUriVariables,"\n",securityUriVariables,"\n",filteredArray)
if (filteredArray.length > 0) {
uriVariablesDistinctResult = "fail";
} else {
if (uriVariablesDistinctResult !== "fail") {
uriVariablesDistinctResult = "pass";
}
}
} else {
// even if there are no urivariables in affordances, the security urivariables is distinct
// fixes https://github.com/thingweb/thingweb-playground/issues/422
if (uriVariablesDistinctResult !== "fail") {
uriVariablesDistinctResult = "pass";
}
}
}
}
}
if (td.hasOwnProperty("events")) {
// checking security in property level
tdEvents = Object.keys(td.events);
for (let i = 0; i < tdEvents.length; i++) {
const curEventName = tdEvents[i];
const curEvent = td.events[curEventName];
// checking href with uriVariable in forms level
const curForms = curEvent.forms;
for (let j = 0; j < curForms.length; j++) {
const curForm = curForms[j];
if (curForm.hasOwnProperty("href")) {
const curHref = curForm.href;
// bottom thing is taken from https://stackoverflow.com/a/5582621/3806426
if (securityUriVariables.some((v) => curHref.includes(v))) {
// There's at least one
if (uriVariablesResult !== "fail") {
uriVariablesResult = "pass";
}
}
}
}
// part for the check of td-security-uri-variables-distinct
if (curEvent.hasOwnProperty("uriVariables")) {
curEventUriVariables = Object.keys(curEvent.uriVariables);
curEventUriVariables.push(...rootUriVariables);
if (curEventUriVariables.length > 0) {
// there are urivariables somewhere at least
// below is from https://stackoverflow.com/a/1885569/3806426
const filteredArray = curEventUriVariables.filter((value) =>
securityUriVariables.includes(value)
);
// console.log(curEventUriVariables,"\n",securityUriVariables,"\n",filteredArray)
if (filteredArray.length > 0) {
uriVariablesDistinctResult = "fail";
} else {
if (uriVariablesDistinctResult !== "fail") {
uriVariablesDistinctResult = "pass";
}
}
} else {
// even if there are no urivariables in affordances, the security urivariables is distinct
// fixes https://github.com/thingweb/thingweb-playground/issues/422
if (uriVariablesDistinctResult !== "fail") {
uriVariablesDistinctResult = "pass";
}
}
}
}
}
results.push({
ID: "td-security-in-uri-variable",
Status: uriVariablesResult,
});
results.push({
ID: "td-security-uri-variables-distinct",
Status: uriVariablesDistinctResult,
});
return results;
}
// no security used non defined scheme, passed test
results.push({
ID: "td-security-scheme-name",
Status: "pass",
});
return results;
}
return results;
}
/**
* When tm:optional uses a pointer, it should point to an actual affordance and only to an affordance, as said by
* tm-tmOptional-resolver: The JSON Pointers of tm:optional MUST resolve to an entire interaction affordance Map definition.
* JSON Schema checks for the syntax but cannot know if the pointed affordance exists.
* This function checks that programmatically
* @param {object} td The TD to do assertion tests
*/
function checkTmOptionalPointer(td) {
const results = [];
if (td.hasOwnProperty("tm:optional")) {
td["tm:optional"].forEach((element) => {
// However, tm: optional values start with / so it should be removed first
element = element.substring(1);
element = element.replace("/", "."); // since the resolvePath uses . instead of /
const pathTarget = resolvePath(td, element, "noTarget");
if (pathTarget === "noTarget" || pathTarget === undefined) {
results.push({
ID: "tm-tmOptional-resolver",
Status: "fail",
Comment: "tm:optional does not resolve to an affordance at " + td.title,
});
} else {
results.push({
ID: "tm-tmOptional-resolver",
Status: "pass",
Comment: "",
});
}
});
} else {
results.push({
ID: "tm-tmOptional-resolver",
Status: "not-impl",
Comment: "no use of tm:optional",
});
}
return results;
}
// ---------- Advanced TM Validation ----------
async function fetchLinkedTm(td) {
if (!td.links) {
return {
success: false,
status: "not-impl",
comment: "td does not link to tm",
};