@lionweb/validation 0.6.13-beta.5 → 0.6.13-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/validators/LionWebChunkDefinitions.d.ts +2 -2
- package/dist/validators/LionWebChunkDefinitions.d.ts.map +1 -1
- package/dist/validators/LionWebChunkDefinitions.js +33 -20
- package/dist/validators/LionWebChunkDefinitions.js.map +1 -1
- package/dist/validators/LionWebLanguageReferenceValidator.d.ts.map +1 -1
- package/dist/validators/LionWebLanguageReferenceValidator.js.map +1 -1
- package/dist/validators/generic/SyntaxValidator.d.ts +8 -8
- package/dist/validators/generic/SyntaxValidator.d.ts.map +1 -1
- package/dist/validators/generic/SyntaxValidator.js +60 -57
- package/dist/validators/generic/SyntaxValidator.js.map +1 -1
- package/dist/validators/generic/ValidationTypes.d.ts +24 -0
- package/dist/validators/generic/ValidationTypes.d.ts.map +1 -1
- package/dist/validators/generic/ValidationTypes.js +20 -1
- package/dist/validators/generic/ValidationTypes.js.map +1 -1
- package/dist/validators/generic/index.d.ts +0 -1
- package/dist/validators/generic/index.d.ts.map +1 -1
- package/dist/validators/generic/index.js +0 -1
- package/dist/validators/generic/index.js.map +1 -1
- package/dist/validators/index.d.ts +0 -1
- package/dist/validators/index.d.ts.map +1 -1
- package/dist/validators/index.js +0 -1
- package/dist/validators/index.js.map +1 -1
- package/package.json +3 -3
- package/src/validators/LionWebChunkDefinitions.ts +34 -21
- package/src/validators/LionWebLanguageReferenceValidator.ts +1 -0
- package/src/validators/generic/SyntaxValidator.ts +97 -90
- package/src/validators/generic/ValidationTypes.ts +40 -2
- package/src/validators/generic/index.ts +0 -1
- package/src/validators/index.ts +0 -1
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TypeDefinition } from "./generic/ValidationTypes.js";
|
|
2
2
|
/**
|
|
3
3
|
* The structure below defines the structure of a LionWeb Chunk by defining all the properties.
|
|
4
4
|
* It can
|
|
5
5
|
* - be fed to the SyntaxValidator to validate an object sat runtime.
|
|
6
6
|
* - used to generate all the types for a LionWebChunk.
|
|
7
7
|
*/
|
|
8
|
-
export declare const expectedTypes: Map<string,
|
|
8
|
+
export declare const expectedTypes: Map<string, TypeDefinition>;
|
|
9
9
|
//# sourceMappingURL=LionWebChunkDefinitions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LionWebChunkDefinitions.d.ts","sourceRoot":"","sources":["../../src/validators/LionWebChunkDefinitions.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"LionWebChunkDefinitions.d.ts","sourceRoot":"","sources":["../../src/validators/LionWebChunkDefinitions.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0C,cAAc,EAAE,MAAM,8BAA8B,CAAA;AAGrG;;;;;GAKG;AACH,eAAO,MAAM,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAoGpD,CAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MAY_BE_NULL, PropertyDef } from "./generic/ValidationTypes.js";
|
|
1
|
+
import { MAY_BE_NULL, PrimitiveDef, PropertyDef } from "./generic/ValidationTypes.js";
|
|
2
2
|
import { validateId, validateKey, validateSerializationFormatVersion, validateVersion } from "./ValidationFunctions.js";
|
|
3
3
|
/**
|
|
4
4
|
* The structure below defines the structure of a LionWeb Chunk by defining all the properties.
|
|
@@ -10,9 +10,9 @@ export const expectedTypes = new Map([
|
|
|
10
10
|
[
|
|
11
11
|
"LionWebMetaPointer",
|
|
12
12
|
[
|
|
13
|
-
PropertyDef({ property: "key", expectedType: "
|
|
14
|
-
PropertyDef({ property: "version", expectedType: "
|
|
15
|
-
PropertyDef({ property: "language", expectedType: "
|
|
13
|
+
PropertyDef({ property: "key", expectedType: "LionWebKey" }),
|
|
14
|
+
PropertyDef({ property: "version", expectedType: "LionWebVersion" }),
|
|
15
|
+
PropertyDef({ property: "language", expectedType: "LionWebKey" })
|
|
16
16
|
]
|
|
17
17
|
],
|
|
18
18
|
[
|
|
@@ -20,13 +20,13 @@ export const expectedTypes = new Map([
|
|
|
20
20
|
[
|
|
21
21
|
PropertyDef({ property: "kind", expectedType: "string" }),
|
|
22
22
|
PropertyDef({ property: "message", expectedType: "string" }),
|
|
23
|
-
PropertyDef({ property: "data", expectedType: "object", mayBeNull: true })
|
|
23
|
+
PropertyDef({ property: "data", expectedType: "object", mayBeNull: true, isOptional: true })
|
|
24
24
|
]
|
|
25
25
|
],
|
|
26
26
|
[
|
|
27
27
|
"LionWebChunk",
|
|
28
28
|
[
|
|
29
|
-
PropertyDef({ property: "serializationFormatVersion", expectedType: "
|
|
29
|
+
PropertyDef({ property: "serializationFormatVersion", expectedType: "LionWebSerializationFormatVersion" }),
|
|
30
30
|
PropertyDef({ property: "languages", expectedType: "LionWebUsedLanguage", isList: true }),
|
|
31
31
|
PropertyDef({ property: "nodes", expectedType: "LionWebNode", isList: true })
|
|
32
32
|
]
|
|
@@ -34,20 +34,20 @@ export const expectedTypes = new Map([
|
|
|
34
34
|
[
|
|
35
35
|
"LionWebUsedLanguage",
|
|
36
36
|
[
|
|
37
|
-
PropertyDef({ property: "key", expectedType: "
|
|
38
|
-
PropertyDef({ property: "version", expectedType: "
|
|
37
|
+
PropertyDef({ property: "key", expectedType: "LionWebKey" }),
|
|
38
|
+
PropertyDef({ property: "version", expectedType: "LionWebVersion" })
|
|
39
39
|
]
|
|
40
40
|
],
|
|
41
41
|
[
|
|
42
42
|
"LionWebNode",
|
|
43
43
|
[
|
|
44
|
-
PropertyDef({ property: "id", expectedType: "
|
|
44
|
+
PropertyDef({ property: "id", expectedType: "LionWebId" }),
|
|
45
45
|
PropertyDef({ property: "classifier", expectedType: "LionWebMetaPointer" }),
|
|
46
46
|
PropertyDef({ property: "properties", expectedType: "LionWebProperty", isList: true }),
|
|
47
47
|
PropertyDef({ property: "containments", expectedType: "LionWebContainment", isList: true }),
|
|
48
48
|
PropertyDef({ property: "references", expectedType: "LionWebReference", isList: true }),
|
|
49
|
-
PropertyDef({ property: "annotations", expectedType: "
|
|
50
|
-
PropertyDef({ property: "parent", expectedType: "
|
|
49
|
+
PropertyDef({ property: "annotations", expectedType: "LionWebId", isList: true }),
|
|
50
|
+
PropertyDef({ property: "parent", expectedType: "LionWebId", mayBeNull: MAY_BE_NULL }),
|
|
51
51
|
]
|
|
52
52
|
],
|
|
53
53
|
[
|
|
@@ -61,7 +61,7 @@ export const expectedTypes = new Map([
|
|
|
61
61
|
"LionWebContainment",
|
|
62
62
|
[
|
|
63
63
|
PropertyDef({ property: "containment", expectedType: "LionWebMetaPointer" }),
|
|
64
|
-
PropertyDef({ property: "children", expectedType: "
|
|
64
|
+
PropertyDef({ property: "children", expectedType: "LionWebId", isList: true }),
|
|
65
65
|
]
|
|
66
66
|
],
|
|
67
67
|
[
|
|
@@ -75,23 +75,36 @@ export const expectedTypes = new Map([
|
|
|
75
75
|
"LionWebReferenceTarget",
|
|
76
76
|
[
|
|
77
77
|
PropertyDef({ property: "resolveInfo", expectedType: "string", mayBeNull: MAY_BE_NULL }),
|
|
78
|
-
PropertyDef({ property: "reference", expectedType: "
|
|
78
|
+
PropertyDef({ property: "reference", expectedType: "LionWebId", mayBeNull: MAY_BE_NULL }),
|
|
79
79
|
]
|
|
80
80
|
],
|
|
81
81
|
/**
|
|
82
|
-
* Elements without properties are assumed to be JSON/JS primitive values, and tested using `typeof
|
|
82
|
+
* Elements without properties are assumed to be JSON/JS primitive values, and tested using `typeof`
|
|
83
|
+
* and the (optional) validate function.
|
|
83
84
|
*/
|
|
84
85
|
[
|
|
85
|
-
"
|
|
86
|
-
|
|
86
|
+
"LionWebId",
|
|
87
|
+
PrimitiveDef({ primitiveType: "string", validate: validateId }),
|
|
88
|
+
],
|
|
89
|
+
[
|
|
90
|
+
"LionWebKey",
|
|
91
|
+
PrimitiveDef({ primitiveType: "string", validate: validateKey }),
|
|
87
92
|
],
|
|
88
93
|
[
|
|
89
|
-
"
|
|
90
|
-
|
|
94
|
+
"LionWebVersion",
|
|
95
|
+
PrimitiveDef({ primitiveType: "string", validate: validateVersion }),
|
|
91
96
|
],
|
|
92
97
|
[
|
|
93
|
-
"
|
|
94
|
-
|
|
98
|
+
"LionWebSerializationFormatVersion",
|
|
99
|
+
PrimitiveDef({ primitiveType: "string", validate: validateSerializationFormatVersion }),
|
|
95
100
|
],
|
|
101
|
+
[
|
|
102
|
+
"string",
|
|
103
|
+
PrimitiveDef({ primitiveType: "string" }),
|
|
104
|
+
],
|
|
105
|
+
[
|
|
106
|
+
"object",
|
|
107
|
+
PrimitiveDef({ primitiveType: "object" }),
|
|
108
|
+
]
|
|
96
109
|
]);
|
|
97
110
|
//# sourceMappingURL=LionWebChunkDefinitions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LionWebChunkDefinitions.js","sourceRoot":"","sources":["../../src/validators/LionWebChunkDefinitions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,WAAW,
|
|
1
|
+
{"version":3,"file":"LionWebChunkDefinitions.js","sourceRoot":"","sources":["../../src/validators/LionWebChunkDefinitions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,WAAW,EAAkB,MAAM,8BAA8B,CAAA;AACrG,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,kCAAkC,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAEvH;;;;;GAKG;AACH,MAAM,CAAC,MAAM,aAAa,GAAgC,IAAI,GAAG,CAAyB;IACtF;QACI,oBAAoB;QACpB;YACI,WAAW,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,CAAC;YAC5D,WAAW,CAAC,EAAE,QAAQ,EAAE,SAAS,EAAE,YAAY,EAAE,gBAAgB,EAAE,CAAC;YACpE,WAAW,CAAC,EAAE,QAAQ,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,EAAE,CAAC;S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AAW,EAAE,CAAC;SAC5F;KACJ;IACD;;;OAGG;IACH;QACI,WAAW;QACX,YAAY,CAAC,EAAE,aAAa,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,CAAC;KAClE;IACD;QACI,YAAY;QACZ,YAAY,CAAC,EAAE,aAAa,EAAE,QAAQ,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC;KACnE;IACD;QACI,gBAAgB;QAChB,YAAY,CAAC,EAAE,aAAa,EAAE,QAAQ,EAAE,QAAQ,EAAE,eAAe,EAAE,CAAC;KACvE;IACD;QACI,mCAAmC;QACnC,YAAY,CAAC,EAAE,aAAa,EAAE,QAAQ,EAAE,QAAQ,EAAE,kCAAkC,EAAE,CAAC;KAC1F;IACD;QACI,QAAQ;QACR,YAAY,CAAC,EAAE,aAAa,EAAE,QAAQ,EAAE,CAAC;KAC5C;IACD;QACI,QAAQ;QACR,YAAY,CAAC,EAAE,aAAa,EAAE,QAAQ,EAAE,CAAC;KAC5C;CACJ,CAAC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LionWebLanguageReferenceValidator.d.ts","sourceRoot":"","sources":["../../src/validators/LionWebLanguageReferenceValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8C,eAAe,EAAE,mBAAmB,EAAwB,MAAM,eAAe,CAAA;AACtI,OAAO,EACH,WAAW,EAGX,uBAAuB,EAI1B,MAAM,qBAAqB,CAAA;AAmB5B,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;
|
|
1
|
+
{"version":3,"file":"LionWebLanguageReferenceValidator.d.ts","sourceRoot":"","sources":["../../src/validators/LionWebLanguageReferenceValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8C,eAAe,EAAE,mBAAmB,EAAwB,MAAM,eAAe,CAAA;AACtI,OAAO,EACH,WAAW,EAGX,uBAAuB,EAI1B,MAAM,qBAAqB,CAAA;AAmB5B,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAIhE;;GAEG;AACH,qBAAa,iCAAiC;IAGM,OAAO,CAAC,QAAQ;IAFhE,gBAAgB,EAAE,gBAAgB,CAAA;gBAEtB,gBAAgB,EAAE,gBAAgB,EAAU,QAAQ,EAAE,gBAAgB;IASlF,QAAQ,CAAC,GAAG,EAAE,uBAAuB,GAAG,IAAI;IAoB5C,OAAO,CAAC,mBAAmB;IAmB3B,OAAO,CAAC,iBAAiB;IAwBzB;;;OAGG;IACH,gBAAgB,CAAC,IAAI,EAAE,eAAe,EAAE,WAAW,EAAE,eAAe,GAAG,SAAS,EAAE,IAAI,EAAE,mBAAmB,EAAE,OAAO,EAAE,WAAW,GAAG,IAAI;CAqF3I"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LionWebLanguageReferenceValidator.js","sourceRoot":"","sources":["../../src/validators/LionWebLanguageReferenceValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAsF,MAAM,eAAe,CAAA;AACtI,OAAO,EACH,WAAW,EACX,8BAA8B,EAC9B,oBAAoB,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,mBAAmB,EAElF,OAAO,EACP,YAAY,EACZ,SAAS,EACZ,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EACH,+CAA+C,EAC/C,2CAA2C,EAC3C,4CAA4C,EAC5C,4BAA4B,EAC5B,6CAA6C,EAC7C,6BAA6B,EAChC,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EACH,8CAA8C,EAC9C,4CAA4C,EAC5C,iCAAiC,EACjC,8BAA8B,EAC9B,+BAA+B,EAClC,MAAM,6BAA6B,CAAA;AAMpC,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;
|
|
1
|
+
{"version":3,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A;gCAChB,CAAC;gCACD,OAAO,IAAI,CAAC,KAAK,KAAK,UAAU,CAAC,KAAK,CAAA;4BAC1C,CAAC,CAAC,CAAA;wBACN,CAAC;wBACD,IAAI,CAAC,KAAK,EAAE,CAAC;4BACT,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,IAAI,4BAA4B,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAA;wBAClI,CAAC;oBACL,CAAC;gBACL,CAAC;YACL,CAAC;iBAAM,CAAC;gBACJ,8BAA8B;YAClC,CAAC;QACL,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { JsonContext } from "@lionweb/json-utils";
|
|
2
2
|
import { ValidationResult } from "./ValidationResult.js";
|
|
3
|
-
import {
|
|
3
|
+
import { ObjectDefinition, PrimitiveDefinition, TypeDefinition, UnknownObjectType } from "./ValidationTypes.js";
|
|
4
4
|
/**
|
|
5
5
|
* Syntax Validator can check whether objects are structurally conforming to the
|
|
6
|
-
* definitions given in `
|
|
6
|
+
* definitions given in `typeDefinitions`.
|
|
7
7
|
*/
|
|
8
8
|
export declare class SyntaxValidator {
|
|
9
9
|
validationResult: ValidationResult;
|
|
10
|
-
|
|
11
|
-
constructor(validationResult: ValidationResult, expectedTypes: Map<string,
|
|
10
|
+
typeDefinitions: Map<string, TypeDefinition>;
|
|
11
|
+
constructor(validationResult: ValidationResult, expectedTypes: Map<string, TypeDefinition>);
|
|
12
12
|
/**
|
|
13
13
|
* Check whether `obj` is a JSON object that conforms to the definition of `expectedType`.
|
|
14
14
|
* All errors found will be pushed into the `errors` array, if its length is not 0, the check has failed.
|
|
@@ -19,19 +19,19 @@ export declare class SyntaxValidator {
|
|
|
19
19
|
/**
|
|
20
20
|
* Validate whether `object` is structured conform the properties in `propertyDef`
|
|
21
21
|
* @param originalProperty The property of which `object` it the value
|
|
22
|
-
* @param
|
|
22
|
+
* @param typeDef The property definitions that are being validated
|
|
23
23
|
* @param object The object being validated
|
|
24
24
|
* @param jsonContext The location in the JSON
|
|
25
25
|
* @private
|
|
26
26
|
*/
|
|
27
|
-
|
|
28
|
-
validatePrimitiveValue(propDef:
|
|
27
|
+
validateObjectProperties(originalProperty: string, typeDef: ObjectDefinition, object: UnknownObjectType, jsonContext: JsonContext): void;
|
|
28
|
+
validatePrimitiveValue(propertyName: string, propDef: PrimitiveDefinition, object: unknown, jsonContext: JsonContext): boolean;
|
|
29
29
|
/**
|
|
30
30
|
* Check whether there are extra properties that should not be there.
|
|
31
31
|
* @param obj Object to be validated
|
|
32
32
|
* @param properties The names of the expected properties
|
|
33
33
|
* @param context Location in JSON
|
|
34
34
|
*/
|
|
35
|
-
checkStrayProperties(obj: UnknownObjectType,
|
|
35
|
+
checkStrayProperties(obj: UnknownObjectType, def: ObjectDefinition, context: JsonContext): void;
|
|
36
36
|
}
|
|
37
37
|
//# sourceMappingURL=SyntaxValidator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SyntaxValidator.d.ts","sourceRoot":"","sources":["../../../src/validators/generic/SyntaxValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AAQjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,
|
|
1
|
+
{"version":3,"file":"SyntaxValidator.d.ts","sourceRoot":"","sources":["../../../src/validators/generic/SyntaxValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AAQjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAGH,gBAAgB,EAChB,mBAAmB,EACnB,cAAc,EACd,iBAAiB,EACpB,MAAM,sBAAsB,CAAA;AAE7B;;;GAGG;AACH,qBAAa,eAAe;IACxB,gBAAgB,EAAE,gBAAgB,CAAA;IAClC,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;gBAEhC,gBAAgB,EAAE,gBAAgB,EAAE,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC;IAK1F;;;;;OAKG;IACH,QAAQ,CAAC,GAAG,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM;IAY3C;;;;;;;OAOG;IACH,wBAAwB,CAAC,gBAAgB,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,EAAE,iBAAiB,EAAE,WAAW,EAAE,WAAW;IAmFjI,sBAAsB,CAAC,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,WAAW,GAAG,OAAO;IAc9H;;;;;OAKG;IACH,oBAAoB,CAAC,GAAG,EAAE,iBAAiB,EAAE,GAAG,EAAE,gBAAgB,EAAE,OAAO,EAAE,WAAW;CAS3F"}
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { JsonContext } from "@lionweb/json-utils";
|
|
2
2
|
import { Syntax_ArrayContainsNull_Issue, Syntax_PropertyMissingIssue, Syntax_PropertyNullIssue, Syntax_PropertyTypeIssue, Syntax_PropertyUnknownIssue } from "../../issues/index.js";
|
|
3
|
+
import { isObjectDefinition, isPrimitiveDefinition } from "./ValidationTypes.js";
|
|
3
4
|
/**
|
|
4
5
|
* Syntax Validator can check whether objects are structurally conforming to the
|
|
5
|
-
* definitions given in `
|
|
6
|
+
* definitions given in `typeDefinitions`.
|
|
6
7
|
*/
|
|
7
8
|
export class SyntaxValidator {
|
|
8
9
|
constructor(validationResult, expectedTypes) {
|
|
9
10
|
this.validationResult = validationResult;
|
|
10
|
-
this.
|
|
11
|
+
this.typeDefinitions = expectedTypes;
|
|
11
12
|
}
|
|
12
13
|
/**
|
|
13
14
|
* Check whether `obj` is a JSON object that conforms to the definition of `expectedType`.
|
|
@@ -16,119 +17,125 @@ export class SyntaxValidator {
|
|
|
16
17
|
* @param expectedType The expected type of the object.
|
|
17
18
|
*/
|
|
18
19
|
validate(obj, expectedType) {
|
|
19
|
-
if (typeof obj !== "object") {
|
|
20
|
-
throw new Error(`SyntaxValidator.validate: 'obj' is not an object, expected a '${expectedType}'`);
|
|
21
|
-
}
|
|
22
20
|
const object = obj;
|
|
23
|
-
const
|
|
24
|
-
if (
|
|
21
|
+
const typeDef = this.typeDefinitions.get(expectedType);
|
|
22
|
+
if (typeDef === undefined) {
|
|
25
23
|
throw new Error(`SyntaxValidator.validate: cannot find definition for ${expectedType}`);
|
|
26
24
|
}
|
|
27
|
-
else {
|
|
28
|
-
this.validateObjectProperties(expectedType,
|
|
25
|
+
else if (isObjectDefinition(typeDef)) {
|
|
26
|
+
this.validateObjectProperties(expectedType, typeDef, object, new JsonContext(null, ["$"]));
|
|
27
|
+
}
|
|
28
|
+
else if (isPrimitiveDefinition(typeDef)) {
|
|
29
|
+
this.validatePrimitiveValue("$", typeDef, object, new JsonContext(null, ["$"]));
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
32
|
/**
|
|
32
33
|
* Validate whether `object` is structured conform the properties in `propertyDef`
|
|
33
34
|
* @param originalProperty The property of which `object` it the value
|
|
34
|
-
* @param
|
|
35
|
+
* @param typeDef The property definitions that are being validated
|
|
35
36
|
* @param object The object being validated
|
|
36
37
|
* @param jsonContext The location in the JSON
|
|
37
38
|
* @private
|
|
38
39
|
*/
|
|
39
|
-
validateObjectProperties(originalProperty,
|
|
40
|
-
if (
|
|
40
|
+
validateObjectProperties(originalProperty, typeDef, object, jsonContext) {
|
|
41
|
+
if (typeDef === null || typeDef === undefined) {
|
|
41
42
|
return;
|
|
42
43
|
}
|
|
43
44
|
if ((typeof object) !== "object") {
|
|
44
45
|
this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, originalProperty, "object", typeof object));
|
|
45
46
|
return;
|
|
46
47
|
}
|
|
47
|
-
for (const
|
|
48
|
-
const
|
|
49
|
-
const validator =
|
|
50
|
-
const propertyValue = object[
|
|
48
|
+
for (const propertyDef of typeDef) {
|
|
49
|
+
const expectedTypeDef = this.typeDefinitions.get(propertyDef.expectedType);
|
|
50
|
+
const validator = propertyDef.validate;
|
|
51
|
+
const propertyValue = object[propertyDef.property];
|
|
51
52
|
if (propertyValue === undefined) {
|
|
52
|
-
|
|
53
|
+
if (!propertyDef.isOptional) {
|
|
54
|
+
this.validationResult.issue(new Syntax_PropertyMissingIssue(jsonContext, propertyDef.property));
|
|
55
|
+
}
|
|
53
56
|
continue;
|
|
54
57
|
}
|
|
55
|
-
if (!
|
|
56
|
-
this.validationResult.issue(new Syntax_PropertyNullIssue(jsonContext,
|
|
58
|
+
if (!propertyDef.mayBeNull && propertyValue === null) {
|
|
59
|
+
this.validationResult.issue(new Syntax_PropertyNullIssue(jsonContext, propertyDef.property));
|
|
57
60
|
continue;
|
|
58
61
|
}
|
|
59
|
-
if (
|
|
62
|
+
if (propertyDef.mayBeNull && propertyValue === null) {
|
|
60
63
|
// Ok, stop checking, continue with next property def
|
|
61
64
|
continue;
|
|
62
65
|
}
|
|
63
|
-
if (
|
|
66
|
+
if (propertyDef.isList) {
|
|
64
67
|
// Check whether value is an array
|
|
65
68
|
if (!Array.isArray(propertyValue)) {
|
|
66
|
-
const newContext = jsonContext.concat(
|
|
67
|
-
this.validationResult.issue(new Syntax_PropertyTypeIssue(newContext,
|
|
69
|
+
const newContext = jsonContext.concat(propertyDef.property);
|
|
70
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(newContext, propertyDef.property, "array", typeof propertyValue));
|
|
68
71
|
return;
|
|
69
72
|
}
|
|
70
73
|
// If an array, validate every item in the array
|
|
71
74
|
propertyValue.forEach((item, index) => {
|
|
72
|
-
const newContext = jsonContext.concat(
|
|
75
|
+
const newContext = jsonContext.concat(propertyDef.property, index);
|
|
73
76
|
if (item === null) {
|
|
74
|
-
this.validationResult.issue(new Syntax_ArrayContainsNull_Issue(newContext,
|
|
77
|
+
this.validationResult.issue(new Syntax_ArrayContainsNull_Issue(newContext, propertyDef.property, index));
|
|
75
78
|
}
|
|
76
79
|
else {
|
|
77
|
-
if (
|
|
78
|
-
if (
|
|
80
|
+
if (expectedTypeDef !== undefined) {
|
|
81
|
+
if (isPrimitiveDefinition(expectedTypeDef)) {
|
|
79
82
|
// propertyValue should be a primitive as it has no property definitions
|
|
80
|
-
if (this.validatePrimitiveValue(
|
|
81
|
-
validator.apply(null, [item, this.validationResult, newContext,
|
|
83
|
+
if (this.validatePrimitiveValue(propertyDef.property, expectedTypeDef, item, jsonContext)) {
|
|
84
|
+
validator.apply(null, [item, this.validationResult, newContext, propertyDef]);
|
|
82
85
|
}
|
|
83
86
|
}
|
|
84
87
|
else {
|
|
85
88
|
// propertyValue should be an object, validate its properties
|
|
86
|
-
this.validateObjectProperties(
|
|
87
|
-
validator.apply(null, [item, this.validationResult, newContext,
|
|
89
|
+
this.validateObjectProperties(propertyDef.property, expectedTypeDef, item, newContext);
|
|
90
|
+
validator.apply(null, [item, this.validationResult, newContext, propertyDef]);
|
|
88
91
|
}
|
|
89
92
|
}
|
|
90
93
|
else {
|
|
91
|
-
throw new Error(`Expected type '${
|
|
94
|
+
throw new Error(`Expected type '${propertyDef.expectedType} has neither property defs, nor a validator.`);
|
|
92
95
|
}
|
|
93
96
|
}
|
|
94
97
|
});
|
|
95
98
|
}
|
|
96
99
|
else {
|
|
97
|
-
const newContext = jsonContext.concat(
|
|
100
|
+
const newContext = jsonContext.concat(propertyDef.property);
|
|
98
101
|
if (Array.isArray(propertyValue)) {
|
|
99
|
-
this.validationResult.issue(new Syntax_PropertyTypeIssue(newContext,
|
|
102
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(newContext, propertyDef.property, propertyDef.expectedType, "array"));
|
|
100
103
|
return;
|
|
101
104
|
}
|
|
102
105
|
// Single valued property, validate it
|
|
103
|
-
if (
|
|
104
|
-
if (
|
|
106
|
+
if (expectedTypeDef !== undefined) {
|
|
107
|
+
if (isPrimitiveDefinition(expectedTypeDef)) {
|
|
105
108
|
// propertyValue should be a primitive as it has no property definitions
|
|
106
|
-
if (this.validatePrimitiveValue(
|
|
107
|
-
validator.apply(null, [propertyValue, this.validationResult, newContext,
|
|
109
|
+
if (this.validatePrimitiveValue(propertyDef.property, expectedTypeDef, propertyValue, jsonContext)) {
|
|
110
|
+
validator.apply(null, [propertyValue, this.validationResult, newContext, propertyDef]);
|
|
108
111
|
}
|
|
109
112
|
}
|
|
110
|
-
else {
|
|
113
|
+
else if (isObjectDefinition(expectedTypeDef)) {
|
|
111
114
|
// propertyValue should be an object, validate its properties
|
|
112
|
-
this.validateObjectProperties(
|
|
113
|
-
validator.apply(null, [propertyValue, this.validationResult, newContext,
|
|
115
|
+
this.validateObjectProperties(propertyDef.property, expectedTypeDef, propertyValue, newContext);
|
|
116
|
+
validator.apply(null, [propertyValue, this.validationResult, newContext, propertyDef]);
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
throw new Error("EXPECTING ObjectDefinition or PrimitiveDefinition, but got something else");
|
|
114
120
|
}
|
|
115
121
|
}
|
|
116
122
|
else {
|
|
117
|
-
throw new Error(`Expected single type '${
|
|
123
|
+
throw new Error(`Expected single type '${propertyDef.expectedType}' for '${propertyDef.property}' at ${newContext.toString()} has neither property defs, nor a validator.`);
|
|
118
124
|
}
|
|
119
125
|
}
|
|
120
126
|
}
|
|
121
|
-
this.checkStrayProperties(object,
|
|
127
|
+
this.checkStrayProperties(object, typeDef, jsonContext);
|
|
122
128
|
}
|
|
123
|
-
validatePrimitiveValue(propDef, object, jsonContext) {
|
|
124
|
-
if (!propDef.mayBeNull && (object === null || object === undefined)) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
if (typeof object !== propDef.
|
|
129
|
-
this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext,
|
|
129
|
+
validatePrimitiveValue(propertyName, propDef, object, jsonContext) {
|
|
130
|
+
// if (!propDef.mayBeNull && (object === null || object === undefined)) {
|
|
131
|
+
// this.validationResult.issue(new Syntax_PropertyNullIssue(jsonContext, propDef.property))
|
|
132
|
+
// return false
|
|
133
|
+
// }
|
|
134
|
+
if (typeof object !== propDef.primitiveType) {
|
|
135
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, propertyName, propDef.primitiveType, typeof object));
|
|
130
136
|
return false;
|
|
131
137
|
}
|
|
138
|
+
propDef.validate(object, this.validationResult, jsonContext);
|
|
132
139
|
return true;
|
|
133
140
|
}
|
|
134
141
|
/**
|
|
@@ -137,18 +144,14 @@ export class SyntaxValidator {
|
|
|
137
144
|
* @param properties The names of the expected properties
|
|
138
145
|
* @param context Location in JSON
|
|
139
146
|
*/
|
|
140
|
-
checkStrayProperties(obj,
|
|
147
|
+
checkStrayProperties(obj, def, context) {
|
|
141
148
|
const own = Object.getOwnPropertyNames(obj);
|
|
149
|
+
const defined = def.map(pdef => pdef.property);
|
|
142
150
|
own.forEach((ownProp) => {
|
|
143
|
-
if (!
|
|
151
|
+
if (!defined.includes(ownProp)) {
|
|
144
152
|
this.validationResult.issue(new Syntax_PropertyUnknownIssue(context, ownProp));
|
|
145
153
|
}
|
|
146
154
|
});
|
|
147
|
-
properties.forEach((prop) => {
|
|
148
|
-
if (!own.includes(prop)) {
|
|
149
|
-
this.validationResult.issue(new Syntax_PropertyMissingIssue(context, prop));
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
155
|
}
|
|
153
156
|
}
|
|
154
157
|
//# sourceMappingURL=SyntaxValidator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SyntaxValidator.js","sourceRoot":"","sources":["../../../src/validators/generic/SyntaxValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACjD,OAAO,EACH,8BAA8B,EAC9B,2BAA2B,EAC3B,wBAAwB,EACxB,wBAAwB,EACxB,2BA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C,CAAC,OAAO,EAAE,EAAE;YACpB,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC7B,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,IAAI,2BAA2B,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAA;YAClF,CAAC;QACL,CAAC,CAAC,CAAA;IACN,CAAC;CACJ"}
|
|
@@ -28,6 +28,10 @@ export type PropertyDefinition = {
|
|
|
28
28
|
* IS this a list property?
|
|
29
29
|
*/
|
|
30
30
|
isList?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Is this property optional?
|
|
33
|
+
*/
|
|
34
|
+
isOptional?: boolean;
|
|
31
35
|
/**
|
|
32
36
|
* Additional validation function
|
|
33
37
|
*/
|
|
@@ -41,4 +45,24 @@ export declare const MAY_BE_NULL = true;
|
|
|
41
45
|
* @constructor
|
|
42
46
|
*/
|
|
43
47
|
export declare function PropertyDef(propDef: PropertyDefinition): PropertyDefinition;
|
|
48
|
+
export type PrimitiveDefinition = {
|
|
49
|
+
/**
|
|
50
|
+
* The expected type of the property value
|
|
51
|
+
*/
|
|
52
|
+
primitiveType: string;
|
|
53
|
+
/**
|
|
54
|
+
* Additional validation function
|
|
55
|
+
*/
|
|
56
|
+
validate?: ValidatorFunction;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Easy way to create a PropertyDefinition typed object with default values.
|
|
60
|
+
* @param propDef
|
|
61
|
+
* @constructor
|
|
62
|
+
*/
|
|
63
|
+
export declare function PrimitiveDef(propDef: PrimitiveDefinition): PrimitiveDefinition;
|
|
64
|
+
export type ObjectDefinition = PropertyDefinition[];
|
|
65
|
+
export type TypeDefinition = ObjectDefinition | PrimitiveDefinition;
|
|
66
|
+
export declare function isObjectDefinition(def: TypeDefinition): def is ObjectDefinition;
|
|
67
|
+
export declare function isPrimitiveDefinition(def: TypeDefinition): def is PrimitiveDefinition;
|
|
44
68
|
//# sourceMappingURL=ValidationTypes.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ValidationTypes.d.ts","sourceRoot":"","sources":["../../../src/validators/generic/ValidationTypes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAExD,MAAM,MAAM,iBAAiB,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAA;AAE1D;;;;;;;GAOG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC7B;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;OAEG;IACH,YAAY,EAAE,MAAM,CAAA;IACpB;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"ValidationTypes.d.ts","sourceRoot":"","sources":["../../../src/validators/generic/ValidationTypes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAExD,MAAM,MAAM,iBAAiB,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAA;AAE1D;;;;;;;GAOG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC7B;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;OAEG;IACH,YAAY,EAAE,MAAM,CAAA;IACpB;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;OAEG;IACH,QAAQ,CAAC,EAAE,iBAAiB,CAAA;CAC/B,CAAA;AAED,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,MAAM,EAAE,gBAAgB,EAAE,GAAG,EAAE,WAAW,EAAE,IAAI,CAAC,EAAE,kBAAkB,KAAK,IAAI,CAAA;AAa1H,eAAO,MAAM,WAAW,OAAO,CAAA;AAE/B;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,kBAAkB,CAU3E;AAED,MAAM,MAAM,mBAAmB,GAAG;IAC9B;;OAEG;IACH,aAAa,EAAE,MAAM,CAAA;IACrB;;OAEG;IACH,QAAQ,CAAC,EAAE,iBAAiB,CAAA;CAC/B,CAAA;AACD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,mBAAmB,CAM9E;AACD,MAAM,MAAM,gBAAgB,GAAG,kBAAkB,EAAE,CAAA;AACnD,MAAM,MAAM,cAAc,GAAG,gBAAgB,GAAG,mBAAmB,CAAA;AAEnE,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,cAAc,GAAG,GAAG,IAAI,gBAAgB,CAE/E;AAED,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,cAAc,GAAG,GAAG,IAAI,mBAAmB,CAErF"}
|
|
@@ -15,13 +15,32 @@ export const MAY_BE_NULL = true;
|
|
|
15
15
|
* @constructor
|
|
16
16
|
*/
|
|
17
17
|
export function PropertyDef(propDef) {
|
|
18
|
-
const { property, expectedType, mayBeNull = false, isList = false, validate = emptyValidation } = propDef;
|
|
18
|
+
const { property, expectedType, mayBeNull = false, isList = false, isOptional = false, validate = emptyValidation } = propDef;
|
|
19
19
|
return {
|
|
20
20
|
property: property,
|
|
21
21
|
expectedType: expectedType,
|
|
22
22
|
isList: isList,
|
|
23
23
|
mayBeNull: mayBeNull,
|
|
24
|
+
isOptional: isOptional,
|
|
24
25
|
validate: validate
|
|
25
26
|
};
|
|
26
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Easy way to create a PropertyDefinition typed object with default values.
|
|
30
|
+
* @param propDef
|
|
31
|
+
* @constructor
|
|
32
|
+
*/
|
|
33
|
+
export function PrimitiveDef(propDef) {
|
|
34
|
+
const { primitiveType, validate = emptyValidation } = propDef;
|
|
35
|
+
return {
|
|
36
|
+
primitiveType: primitiveType,
|
|
37
|
+
validate: validate
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export function isObjectDefinition(def) {
|
|
41
|
+
return Array.isArray(def);
|
|
42
|
+
}
|
|
43
|
+
export function isPrimitiveDefinition(def) {
|
|
44
|
+
return def?.primitiveType !== undefined;
|
|
45
|
+
}
|
|
27
46
|
//# sourceMappingURL=ValidationTypes.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ValidationTypes.js","sourceRoot":"","sources":["../../../src/validators/generic/ValidationTypes.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ValidationTypes.js","sourceRoot":"","sources":["../../../src/validators/generic/ValidationTypes.ts"],"names":[],"mappings":"AA0CA;;;;;;GAMG;AACH,6DAA6D;AAC7D,SAAS,eAAe,CAAI,MAAS,EAAE,MAAwB,EAAE,GAAgB,EAAE,IAAyB,IAAS,CAAC;AAEtH,uCAAuC;AACvC,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,CAAA;AAE/B;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,OAA2B;IACnD,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,SAAS,GAAG,KAAK,EAAE,MAAM,GAAG,KAAK,EAAE,UAAU,GAAG,KAAK,EAAE,QAAQ,GAAG,eAAe,EAAE,GAAG,OAAO,CAAA;IAC7H,OAAO;QACH,QAAQ,EAAE,QAAQ;QAClB,YAAY,EAAE,YAAY;QAC1B,MAAM,EAAE,MAAM;QACd,SAAS,EAAE,SAAS;QACpB,UAAU,EAAE,UAAU;QACtB,QAAQ,EAAE,QAAQ;KACrB,CAAA;AACL,CAAC;AAYD;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,OAA4B;IACrD,MAAM,EAAE,aAAa,EAAE,QAAQ,GAAG,eAAe,EAAE,GAAG,OAAO,CAAA;IAC7D,OAAO;QACH,aAAa,EAAE,aAAa;QAC5B,QAAQ,EAAE,QAAQ;KACrB,CAAA;AACL,CAAC;AAID,MAAM,UAAU,kBAAkB,CAAC,GAAmB;IAClD,OAAO,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;AAC7B,CAAC;AAED,MAAM,UAAU,qBAAqB,CAAC,GAAmB;IACrD,OAAQ,GAA2B,EAAE,aAAa,KAAK,SAAS,CAAA;AACpE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/validators/generic/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA;AACpC,cAAc,uBAAuB,CAAA;AACrC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/validators/generic/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA;AACpC,cAAc,uBAAuB,CAAA;AACrC,cAAc,sBAAsB,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/validators/generic/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA;AACpC,cAAc,uBAAuB,CAAA;AACrC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/validators/generic/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA;AACpC,cAAc,uBAAuB,CAAA;AACrC,cAAc,sBAAsB,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/validators/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/validators/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,6BAA6B,CAAA;AAC3C,cAAc,gCAAgC,CAAA;AAC9C,cAAc,+BAA+B,CAAA;AAC7C,cAAc,wCAAwC,CAAA;AACtD,cAAc,uBAAuB,CAAA;AACrC,cAAc,0BAA0B,CAAA;AACxC,cAAc,8BAA8B,CAAA"}
|
package/dist/validators/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/validators/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/validators/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,6BAA6B,CAAA;AAC3C,cAAc,gCAAgC,CAAA;AAC9C,cAAc,+BAA+B,CAAA;AAC7C,cAAc,wCAAwC,CAAA;AACtD,cAAc,uBAAuB,CAAA;AACrC,cAAc,0BAA0B,CAAA;AACxC,cAAc,8BAA8B,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lionweb/validation",
|
|
3
|
-
"version": "0.6.13-beta.
|
|
3
|
+
"version": "0.6.13-beta.6",
|
|
4
4
|
"license": "Apache 2.0",
|
|
5
5
|
"description": "LionWeb Serialization validation",
|
|
6
6
|
"author": "jos.warmer@openmodeling.nl",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"copyBuiltins": "node -e \"const fs = require('fs'); fs.copyFileSync('./src/languages/LionCore-builtins.json', './dist/languages/LionCore-builtins.json');\"",
|
|
21
21
|
"lint": "eslint src",
|
|
22
22
|
"test-here": "cd ../test && find dist/validation -type d | sed '1!G;h;$!d' | xargs npx mocha",
|
|
23
|
-
"release": "npm publish",
|
|
24
|
-
"release-beta": "npm publish --tag beta",
|
|
23
|
+
"release": "npm publish --registry https://registry.npmjs.org",
|
|
24
|
+
"release-beta": "npm publish --registry https://registry.npmjs.org --tag beta",
|
|
25
25
|
"testValid": "node dist/runners/RunCheckFolder.js ../test/testset/valid",
|
|
26
26
|
"testInvalid": "node dist/runners/RunCheckFolder.js ../test/testset/invalid",
|
|
27
27
|
"testInvalidWithLanguage": "node dist/runners/RunCheckFolderWithLanguage.js ../test/testset/withLanguage/invalid/ ../test/testset/withLanguage/myLang.language.json",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MAY_BE_NULL, PropertyDef,
|
|
1
|
+
import { MAY_BE_NULL, PrimitiveDef, PropertyDef, TypeDefinition } from "./generic/ValidationTypes.js"
|
|
2
2
|
import { validateId, validateKey, validateSerializationFormatVersion, validateVersion } from "./ValidationFunctions.js"
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -7,13 +7,13 @@ import { validateId, validateKey, validateSerializationFormatVersion, validateVe
|
|
|
7
7
|
* - be fed to the SyntaxValidator to validate an object sat runtime.
|
|
8
8
|
* - used to generate all the types for a LionWebChunk.
|
|
9
9
|
*/
|
|
10
|
-
export const expectedTypes: Map<string,
|
|
10
|
+
export const expectedTypes: Map<string, TypeDefinition> = new Map<string, TypeDefinition>([
|
|
11
11
|
[
|
|
12
12
|
"LionWebMetaPointer",
|
|
13
13
|
[
|
|
14
|
-
PropertyDef({ property: "key", expectedType: "
|
|
15
|
-
PropertyDef({ property: "version", expectedType: "
|
|
16
|
-
PropertyDef({ property: "language", expectedType: "
|
|
14
|
+
PropertyDef({ property: "key", expectedType: "LionWebKey" }),
|
|
15
|
+
PropertyDef({ property: "version", expectedType: "LionWebVersion" }),
|
|
16
|
+
PropertyDef({ property: "language", expectedType: "LionWebKey" })
|
|
17
17
|
]
|
|
18
18
|
],
|
|
19
19
|
[
|
|
@@ -21,13 +21,13 @@ export const expectedTypes: Map<string, PropertyDefinition[]> = new Map([
|
|
|
21
21
|
[
|
|
22
22
|
PropertyDef({ property: "kind", expectedType: "string" }),
|
|
23
23
|
PropertyDef({ property: "message", expectedType: "string" }),
|
|
24
|
-
PropertyDef({ property: "data", expectedType: "object", mayBeNull: true })
|
|
24
|
+
PropertyDef({ property: "data", expectedType: "object", mayBeNull: true, isOptional: true })
|
|
25
25
|
]
|
|
26
26
|
],
|
|
27
27
|
[
|
|
28
28
|
"LionWebChunk",
|
|
29
29
|
[
|
|
30
|
-
PropertyDef({ property: "serializationFormatVersion", expectedType: "
|
|
30
|
+
PropertyDef({ property: "serializationFormatVersion", expectedType: "LionWebSerializationFormatVersion" }),
|
|
31
31
|
PropertyDef({ property: "languages", expectedType: "LionWebUsedLanguage", isList: true }),
|
|
32
32
|
PropertyDef({ property: "nodes", expectedType: "LionWebNode", isList: true })
|
|
33
33
|
]
|
|
@@ -35,20 +35,20 @@ export const expectedTypes: Map<string, PropertyDefinition[]> = new Map([
|
|
|
35
35
|
[
|
|
36
36
|
"LionWebUsedLanguage",
|
|
37
37
|
[
|
|
38
|
-
PropertyDef({ property: "key", expectedType: "
|
|
39
|
-
PropertyDef({ property: "version", expectedType: "
|
|
38
|
+
PropertyDef({ property: "key", expectedType: "LionWebKey" }),
|
|
39
|
+
PropertyDef({ property: "version", expectedType: "LionWebVersion" })
|
|
40
40
|
]
|
|
41
41
|
],
|
|
42
42
|
[
|
|
43
43
|
"LionWebNode",
|
|
44
44
|
[
|
|
45
|
-
PropertyDef({ property: "id", expectedType: "
|
|
45
|
+
PropertyDef({ property: "id", expectedType: "LionWebId" }),
|
|
46
46
|
PropertyDef({ property: "classifier", expectedType: "LionWebMetaPointer" }),
|
|
47
47
|
PropertyDef({ property: "properties", expectedType: "LionWebProperty", isList: true }),
|
|
48
48
|
PropertyDef({ property: "containments", expectedType: "LionWebContainment", isList: true }),
|
|
49
49
|
PropertyDef({ property: "references", expectedType: "LionWebReference", isList: true }),
|
|
50
|
-
PropertyDef({ property: "annotations", expectedType: "
|
|
51
|
-
PropertyDef({ property: "parent", expectedType: "
|
|
50
|
+
PropertyDef({ property: "annotations", expectedType: "LionWebId", isList: true }),
|
|
51
|
+
PropertyDef({ property: "parent", expectedType: "LionWebId", mayBeNull: MAY_BE_NULL }),
|
|
52
52
|
]
|
|
53
53
|
],
|
|
54
54
|
[
|
|
@@ -62,7 +62,7 @@ export const expectedTypes: Map<string, PropertyDefinition[]> = new Map([
|
|
|
62
62
|
"LionWebContainment",
|
|
63
63
|
[
|
|
64
64
|
PropertyDef({ property: "containment", expectedType: "LionWebMetaPointer" }),
|
|
65
|
-
PropertyDef({ property: "children", expectedType: "
|
|
65
|
+
PropertyDef({ property: "children", expectedType: "LionWebId", isList: true }),
|
|
66
66
|
]
|
|
67
67
|
],
|
|
68
68
|
[
|
|
@@ -76,24 +76,37 @@ export const expectedTypes: Map<string, PropertyDefinition[]> = new Map([
|
|
|
76
76
|
"LionWebReferenceTarget",
|
|
77
77
|
[
|
|
78
78
|
PropertyDef({ property: "resolveInfo", expectedType: "string", mayBeNull: MAY_BE_NULL }),
|
|
79
|
-
PropertyDef({ property: "reference", expectedType: "
|
|
79
|
+
PropertyDef({ property: "reference", expectedType: "LionWebId", mayBeNull: MAY_BE_NULL }),
|
|
80
80
|
]
|
|
81
81
|
],
|
|
82
82
|
/**
|
|
83
|
-
* Elements without properties are assumed to be JSON/JS primitive values, and tested using `typeof
|
|
83
|
+
* Elements without properties are assumed to be JSON/JS primitive values, and tested using `typeof`
|
|
84
|
+
* and the (optional) validate function.
|
|
84
85
|
*/
|
|
85
86
|
[
|
|
86
|
-
"
|
|
87
|
-
|
|
87
|
+
"LionWebId",
|
|
88
|
+
PrimitiveDef({ primitiveType: "string", validate: validateId }),
|
|
89
|
+
],
|
|
90
|
+
[
|
|
91
|
+
"LionWebKey",
|
|
92
|
+
PrimitiveDef({ primitiveType: "string", validate: validateKey }),
|
|
88
93
|
],
|
|
89
94
|
[
|
|
90
|
-
"
|
|
91
|
-
|
|
95
|
+
"LionWebVersion",
|
|
96
|
+
PrimitiveDef({ primitiveType: "string", validate: validateVersion }),
|
|
92
97
|
],
|
|
93
98
|
[
|
|
94
|
-
"
|
|
95
|
-
|
|
99
|
+
"LionWebSerializationFormatVersion",
|
|
100
|
+
PrimitiveDef({ primitiveType: "string", validate: validateSerializationFormatVersion }),
|
|
96
101
|
],
|
|
102
|
+
[
|
|
103
|
+
"string",
|
|
104
|
+
PrimitiveDef({ primitiveType: "string" }),
|
|
105
|
+
],
|
|
106
|
+
[
|
|
107
|
+
"object",
|
|
108
|
+
PrimitiveDef({ primitiveType: "object" }),
|
|
109
|
+
]
|
|
97
110
|
])
|
|
98
111
|
|
|
99
112
|
|
|
@@ -30,6 +30,7 @@ import { LanguageRegistry } from "../languages/index.js"
|
|
|
30
30
|
import { ValidationResult } from "./generic/ValidationResult.js"
|
|
31
31
|
import { validateBoolean, validateInteger, validateJSON } from "./ValidationFunctions.js"
|
|
32
32
|
|
|
33
|
+
|
|
33
34
|
/**
|
|
34
35
|
* Check against the language definition
|
|
35
36
|
*/
|
|
@@ -7,19 +7,26 @@ import {
|
|
|
7
7
|
Syntax_PropertyUnknownIssue
|
|
8
8
|
} from "../../issues/index.js"
|
|
9
9
|
import { ValidationResult } from "./ValidationResult.js"
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
isObjectDefinition,
|
|
12
|
+
isPrimitiveDefinition,
|
|
13
|
+
ObjectDefinition,
|
|
14
|
+
PrimitiveDefinition,
|
|
15
|
+
TypeDefinition,
|
|
16
|
+
UnknownObjectType
|
|
17
|
+
} from "./ValidationTypes.js"
|
|
11
18
|
|
|
12
19
|
/**
|
|
13
20
|
* Syntax Validator can check whether objects are structurally conforming to the
|
|
14
|
-
* definitions given in `
|
|
21
|
+
* definitions given in `typeDefinitions`.
|
|
15
22
|
*/
|
|
16
23
|
export class SyntaxValidator {
|
|
17
24
|
validationResult: ValidationResult
|
|
18
|
-
|
|
25
|
+
typeDefinitions: Map<string, TypeDefinition>
|
|
19
26
|
|
|
20
|
-
constructor(validationResult: ValidationResult, expectedTypes: Map<string,
|
|
27
|
+
constructor(validationResult: ValidationResult, expectedTypes: Map<string, TypeDefinition>) {
|
|
21
28
|
this.validationResult = validationResult
|
|
22
|
-
this.
|
|
29
|
+
this.typeDefinitions = expectedTypes
|
|
23
30
|
}
|
|
24
31
|
|
|
25
32
|
/**
|
|
@@ -29,115 +36,119 @@ export class SyntaxValidator {
|
|
|
29
36
|
* @param expectedType The expected type of the object.
|
|
30
37
|
*/
|
|
31
38
|
validate(obj: unknown, expectedType: string) {
|
|
32
|
-
if (typeof obj !== "object") {
|
|
33
|
-
throw new Error(`SyntaxValidator.validate: 'obj' is not an object, expected a '${expectedType}'`)
|
|
34
|
-
}
|
|
35
39
|
const object = obj as UnknownObjectType
|
|
36
|
-
const
|
|
37
|
-
if (
|
|
40
|
+
const typeDef = this.typeDefinitions.get(expectedType)
|
|
41
|
+
if (typeDef === undefined) {
|
|
38
42
|
throw new Error(`SyntaxValidator.validate: cannot find definition for ${expectedType}`)
|
|
39
|
-
} else {
|
|
40
|
-
this.validateObjectProperties(expectedType,
|
|
43
|
+
} else if (isObjectDefinition(typeDef)){
|
|
44
|
+
this.validateObjectProperties(expectedType, typeDef, object, new JsonContext(null, ["$"]))
|
|
45
|
+
} else if( isPrimitiveDefinition(typeDef)) {
|
|
46
|
+
this.validatePrimitiveValue("$", typeDef, object, new JsonContext(null, ["$"]))
|
|
41
47
|
}
|
|
42
48
|
}
|
|
43
49
|
|
|
44
50
|
/**
|
|
45
51
|
* Validate whether `object` is structured conform the properties in `propertyDef`
|
|
46
52
|
* @param originalProperty The property of which `object` it the value
|
|
47
|
-
* @param
|
|
53
|
+
* @param typeDef The property definitions that are being validated
|
|
48
54
|
* @param object The object being validated
|
|
49
55
|
* @param jsonContext The location in the JSON
|
|
50
56
|
* @private
|
|
51
57
|
*/
|
|
52
|
-
|
|
53
|
-
if (
|
|
54
|
-
return
|
|
55
|
-
}
|
|
56
|
-
if ((typeof object) !== "object") {
|
|
57
|
-
this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, originalProperty, "object", typeof object))
|
|
58
|
+
validateObjectProperties(originalProperty: string, typeDef: ObjectDefinition, object: UnknownObjectType, jsonContext: JsonContext) {
|
|
59
|
+
if (typeDef === null || typeDef === undefined) {
|
|
58
60
|
return
|
|
59
61
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const propertyValue = object[pdef.property]
|
|
64
|
-
if (propertyValue === undefined) {
|
|
65
|
-
this.validationResult.issue(new Syntax_PropertyMissingIssue(jsonContext, pdef.property + `{ ${typeof object}}{${originalProperty}}`))
|
|
66
|
-
continue
|
|
67
|
-
}
|
|
68
|
-
if (!pdef.mayBeNull && propertyValue === null) {
|
|
69
|
-
this.validationResult.issue(new Syntax_PropertyNullIssue(jsonContext, pdef.property))
|
|
70
|
-
continue
|
|
71
|
-
}
|
|
72
|
-
if (pdef.mayBeNull && propertyValue === null) {
|
|
73
|
-
// Ok, stop checking, continue with next property def
|
|
74
|
-
continue
|
|
62
|
+
if ((typeof object) !== "object") {
|
|
63
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, originalProperty, "object", typeof object))
|
|
64
|
+
return
|
|
75
65
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
66
|
+
for (const propertyDef of typeDef) {
|
|
67
|
+
const expectedTypeDef = this.typeDefinitions.get(propertyDef.expectedType)
|
|
68
|
+
const validator = propertyDef.validate!
|
|
69
|
+
const propertyValue = object[propertyDef.property]
|
|
70
|
+
if (propertyValue === undefined) {
|
|
71
|
+
if (!propertyDef.isOptional) {
|
|
72
|
+
this.validationResult.issue(new Syntax_PropertyMissingIssue(jsonContext, propertyDef.property))
|
|
73
|
+
}
|
|
74
|
+
continue
|
|
82
75
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
76
|
+
if (!propertyDef.mayBeNull && propertyValue === null) {
|
|
77
|
+
this.validationResult.issue(new Syntax_PropertyNullIssue(jsonContext, propertyDef.property))
|
|
78
|
+
continue
|
|
79
|
+
}
|
|
80
|
+
if (propertyDef.mayBeNull && propertyValue === null) {
|
|
81
|
+
// Ok, stop checking, continue with next property def
|
|
82
|
+
continue
|
|
83
|
+
}
|
|
84
|
+
if (propertyDef.isList) {
|
|
85
|
+
// Check whether value is an array
|
|
86
|
+
if (!Array.isArray(propertyValue)) {
|
|
87
|
+
const newContext = jsonContext.concat(propertyDef.property)
|
|
88
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(newContext, propertyDef.property, "array", typeof propertyValue))
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
// If an array, validate every item in the array
|
|
92
|
+
(propertyValue as UnknownObjectType[]).forEach((item, index) => {
|
|
93
|
+
const newContext = jsonContext.concat(propertyDef.property, index)
|
|
94
|
+
if (item === null) {
|
|
95
|
+
this.validationResult.issue(new Syntax_ArrayContainsNull_Issue(newContext, propertyDef.property, index))
|
|
96
|
+
} else {
|
|
97
|
+
if (expectedTypeDef !== undefined) {
|
|
98
|
+
if (isPrimitiveDefinition(expectedTypeDef)) {
|
|
99
|
+
// propertyValue should be a primitive as it has no property definitions
|
|
100
|
+
if (this.validatePrimitiveValue(propertyDef.property, expectedTypeDef, item, jsonContext)) {
|
|
101
|
+
validator.apply(null, [item, this.validationResult, newContext, propertyDef])
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
// propertyValue should be an object, validate its properties
|
|
105
|
+
this.validateObjectProperties(propertyDef.property, expectedTypeDef, item as UnknownObjectType, newContext)
|
|
106
|
+
validator.apply(null, [item, this.validationResult, newContext, propertyDef])
|
|
94
107
|
}
|
|
95
108
|
} else {
|
|
96
|
-
|
|
97
|
-
this.validateObjectProperties(pdef.property, expectedPropertyDefs, item as UnknownObjectType, newContext)
|
|
98
|
-
validator.apply(null, [item, this.validationResult, newContext, pdef])
|
|
109
|
+
throw new Error(`Expected type '${propertyDef.expectedType} has neither property defs, nor a validator.`)
|
|
99
110
|
}
|
|
100
|
-
} else {
|
|
101
|
-
throw new Error(`Expected type '${pdef.expectedType} has neither property defs, nor a validator.`)
|
|
102
111
|
}
|
|
112
|
+
})
|
|
113
|
+
} else {
|
|
114
|
+
const newContext = jsonContext.concat(propertyDef.property)
|
|
115
|
+
if (Array.isArray(propertyValue)) {
|
|
116
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(newContext, propertyDef.property, propertyDef.expectedType, "array"))
|
|
117
|
+
return
|
|
103
118
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
119
|
+
// Single valued property, validate it
|
|
120
|
+
if (expectedTypeDef !== undefined) {
|
|
121
|
+
if (isPrimitiveDefinition(expectedTypeDef)) {
|
|
122
|
+
// propertyValue should be a primitive as it has no property definitions
|
|
123
|
+
if (this.validatePrimitiveValue(propertyDef.property, expectedTypeDef, propertyValue, jsonContext)) {
|
|
124
|
+
validator.apply(null, [propertyValue, this.validationResult, newContext, propertyDef])
|
|
125
|
+
}
|
|
126
|
+
} else if (isObjectDefinition(expectedTypeDef)) {
|
|
127
|
+
// propertyValue should be an object, validate its properties
|
|
128
|
+
this.validateObjectProperties(propertyDef.property, expectedTypeDef, propertyValue as UnknownObjectType, newContext)
|
|
129
|
+
validator.apply(null, [propertyValue, this.validationResult, newContext, propertyDef])
|
|
130
|
+
} else {
|
|
131
|
+
throw new Error("EXPECTING ObjectDefinition or PrimitiveDefinition, but got something else")
|
|
117
132
|
}
|
|
118
133
|
} else {
|
|
119
|
-
|
|
120
|
-
this.validateObjectProperties(pdef.property, expectedPropertyDefs, propertyValue as UnknownObjectType, newContext)
|
|
121
|
-
validator.apply(null, [propertyValue, this.validationResult, newContext, pdef])
|
|
134
|
+
throw new Error(`Expected single type '${propertyDef.expectedType}' for '${propertyDef.property}' at ${newContext.toString()} has neither property defs, nor a validator.`)
|
|
122
135
|
}
|
|
123
|
-
} else {
|
|
124
|
-
throw new Error(`Expected single type '${pdef.expectedType}' for '${pdef.property}' at ${newContext.toString()} has neither property defs, nor a validator.`)
|
|
125
136
|
}
|
|
126
137
|
}
|
|
127
|
-
|
|
128
|
-
this.checkStrayProperties(object, propertyDef.map(pdef => pdef.property ), jsonContext)
|
|
138
|
+
this.checkStrayProperties(object, typeDef, jsonContext)
|
|
129
139
|
}
|
|
130
140
|
|
|
131
|
-
validatePrimitiveValue(propDef:
|
|
132
|
-
if (!propDef.mayBeNull && (object === null || object === undefined)) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
141
|
+
validatePrimitiveValue(propertyName: string, propDef: PrimitiveDefinition, object: unknown, jsonContext: JsonContext): boolean {
|
|
142
|
+
// if (!propDef.mayBeNull && (object === null || object === undefined)) {
|
|
143
|
+
// this.validationResult.issue(new Syntax_PropertyNullIssue(jsonContext, propDef.property))
|
|
144
|
+
// return false
|
|
145
|
+
// }
|
|
136
146
|
|
|
137
|
-
if (typeof object !== propDef.
|
|
138
|
-
this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext,
|
|
147
|
+
if (typeof object !== propDef.primitiveType) {
|
|
148
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, propertyName, propDef.primitiveType,typeof object))
|
|
139
149
|
return false
|
|
140
150
|
}
|
|
151
|
+
propDef.validate!(object, this.validationResult, jsonContext)
|
|
141
152
|
return true
|
|
142
153
|
}
|
|
143
154
|
|
|
@@ -147,17 +158,13 @@ export class SyntaxValidator {
|
|
|
147
158
|
* @param properties The names of the expected properties
|
|
148
159
|
* @param context Location in JSON
|
|
149
160
|
*/
|
|
150
|
-
checkStrayProperties(obj: UnknownObjectType,
|
|
161
|
+
checkStrayProperties(obj: UnknownObjectType, def: ObjectDefinition, context: JsonContext) {
|
|
151
162
|
const own = Object.getOwnPropertyNames(obj)
|
|
163
|
+
const defined = def.map(pdef => pdef.property)
|
|
152
164
|
own.forEach((ownProp) => {
|
|
153
|
-
if (!
|
|
165
|
+
if (!defined.includes(ownProp)) {
|
|
154
166
|
this.validationResult.issue(new Syntax_PropertyUnknownIssue(context, ownProp))
|
|
155
167
|
}
|
|
156
168
|
})
|
|
157
|
-
properties.forEach((prop) => {
|
|
158
|
-
if (!own.includes(prop)) {
|
|
159
|
-
this.validationResult.issue(new Syntax_PropertyMissingIssue(context, prop))
|
|
160
|
-
}
|
|
161
|
-
})
|
|
162
169
|
}
|
|
163
170
|
}
|
|
@@ -27,7 +27,11 @@ export type PropertyDefinition = {
|
|
|
27
27
|
/**
|
|
28
28
|
* IS this a list property?
|
|
29
29
|
*/
|
|
30
|
-
isList?: boolean
|
|
30
|
+
isList?: boolean,
|
|
31
|
+
/**
|
|
32
|
+
* Is this property optional?
|
|
33
|
+
*/
|
|
34
|
+
isOptional?: boolean,
|
|
31
35
|
/**
|
|
32
36
|
* Additional validation function
|
|
33
37
|
*/
|
|
@@ -55,12 +59,46 @@ export const MAY_BE_NULL = true
|
|
|
55
59
|
* @constructor
|
|
56
60
|
*/
|
|
57
61
|
export function PropertyDef(propDef: PropertyDefinition): PropertyDefinition {
|
|
58
|
-
const { property, expectedType, mayBeNull = false, isList = false, validate = emptyValidation } = propDef
|
|
62
|
+
const { property, expectedType, mayBeNull = false, isList = false, isOptional = false, validate = emptyValidation } = propDef
|
|
59
63
|
return {
|
|
60
64
|
property: property,
|
|
61
65
|
expectedType: expectedType,
|
|
62
66
|
isList: isList,
|
|
63
67
|
mayBeNull: mayBeNull,
|
|
68
|
+
isOptional: isOptional,
|
|
69
|
+
validate: validate
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export type PrimitiveDefinition = {
|
|
74
|
+
/**
|
|
75
|
+
* The expected type of the property value
|
|
76
|
+
*/
|
|
77
|
+
primitiveType: string
|
|
78
|
+
/**
|
|
79
|
+
* Additional validation function
|
|
80
|
+
*/
|
|
81
|
+
validate?: ValidatorFunction
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Easy way to create a PropertyDefinition typed object with default values.
|
|
85
|
+
* @param propDef
|
|
86
|
+
* @constructor
|
|
87
|
+
*/
|
|
88
|
+
export function PrimitiveDef(propDef: PrimitiveDefinition): PrimitiveDefinition {
|
|
89
|
+
const { primitiveType, validate = emptyValidation } = propDef
|
|
90
|
+
return {
|
|
91
|
+
primitiveType: primitiveType,
|
|
64
92
|
validate: validate
|
|
65
93
|
}
|
|
66
94
|
}
|
|
95
|
+
export type ObjectDefinition = PropertyDefinition[]
|
|
96
|
+
export type TypeDefinition = ObjectDefinition | PrimitiveDefinition
|
|
97
|
+
|
|
98
|
+
export function isObjectDefinition(def: TypeDefinition): def is ObjectDefinition {
|
|
99
|
+
return Array.isArray(def)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function isPrimitiveDefinition(def: TypeDefinition): def is PrimitiveDefinition {
|
|
103
|
+
return (def as PrimitiveDefinition)?.primitiveType !== undefined
|
|
104
|
+
}
|