@lionweb/validation 0.6.13-beta.6 → 0.6.13-beta.7
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/diff/DiffResult.d.ts +9 -0
- package/dist/diff/DiffResult.d.ts.map +1 -0
- package/dist/diff/DiffResult.js +18 -0
- package/dist/diff/DiffResult.js.map +1 -0
- package/dist/diff/LionWebJsonDiff.d.ts +25 -0
- package/dist/diff/LionWebJsonDiff.d.ts.map +1 -0
- package/dist/diff/LionWebJsonDiff.js +249 -0
- package/dist/diff/LionWebJsonDiff.js.map +1 -0
- package/dist/diff/changes/Change.d.ts +34 -0
- package/dist/diff/changes/Change.d.ts.map +1 -0
- package/dist/diff/changes/Change.js +38 -0
- package/dist/diff/changes/Change.js.map +1 -0
- package/dist/diff/changes/ChunkChange.d.ts +43 -0
- package/dist/diff/changes/ChunkChange.d.ts.map +1 -0
- package/dist/diff/changes/ChunkChange.js +57 -0
- package/dist/diff/changes/ChunkChange.js.map +1 -0
- package/dist/diff/changes/ContainmentChange.d.ts +25 -0
- package/dist/diff/changes/ContainmentChange.d.ts.map +1 -0
- package/dist/diff/changes/ContainmentChange.js +34 -0
- package/dist/diff/changes/ContainmentChange.js.map +1 -0
- package/dist/diff/changes/NodeChange.d.ts +41 -0
- package/dist/diff/changes/NodeChange.d.ts.map +1 -0
- package/dist/diff/changes/NodeChange.js +54 -0
- package/dist/diff/changes/NodeChange.js.map +1 -0
- package/dist/diff/changes/PropertyChange.d.ts +24 -0
- package/dist/diff/changes/PropertyChange.d.ts.map +1 -0
- package/dist/diff/changes/PropertyChange.js +29 -0
- package/dist/diff/changes/PropertyChange.js.map +1 -0
- package/dist/diff/changes/ReferenceChange.d.ts +25 -0
- package/dist/diff/changes/ReferenceChange.d.ts.map +1 -0
- package/dist/diff/changes/ReferenceChange.js +34 -0
- package/dist/diff/changes/ReferenceChange.js.map +1 -0
- package/dist/diff/changes/index.d.ts +7 -0
- package/dist/diff/changes/index.d.ts.map +1 -0
- package/dist/diff/changes/index.js +7 -0
- package/dist/diff/changes/index.js.map +1 -0
- package/dist/diff/index.d.ts +4 -0
- package/dist/diff/index.d.ts.map +1 -0
- package/dist/diff/index.js +4 -0
- package/dist/diff/index.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/issues/LanguageIssues.d.ts +4 -4
- package/dist/issues/LanguageIssues.d.ts.map +1 -1
- package/dist/issues/LanguageIssues.js +1 -1
- package/dist/issues/LanguageIssues.js.map +1 -1
- package/dist/issues/ReferenceIssues.d.ts +6 -6
- package/dist/issues/ReferenceIssues.d.ts.map +1 -1
- package/dist/issues/ReferenceIssues.js +3 -3
- package/dist/issues/ReferenceIssues.js.map +1 -1
- package/dist/issues/SyntaxIssues.d.ts +1 -1
- package/dist/issues/SyntaxIssues.d.ts.map +1 -1
- package/dist/issues/ValidationIssue.d.ts +1 -1
- package/dist/issues/ValidationIssue.d.ts.map +1 -1
- package/dist/issues/ValidationIssue.js.map +1 -1
- package/dist/issues/index.d.ts +1 -0
- package/dist/issues/index.d.ts.map +1 -1
- package/dist/issues/index.js +1 -0
- package/dist/issues/index.js.map +1 -1
- package/dist/json/ChunkUtils.d.ts +26 -0
- package/dist/json/ChunkUtils.d.ts.map +1 -0
- package/dist/json/ChunkUtils.js +46 -0
- package/dist/json/ChunkUtils.js.map +1 -0
- package/dist/json/JsonContext.d.ts +11 -0
- package/dist/json/JsonContext.d.ts.map +1 -0
- package/dist/json/JsonContext.js +25 -0
- package/dist/json/JsonContext.js.map +1 -0
- package/dist/json/LionWebJson.d.ts +50 -0
- package/dist/json/LionWebJson.d.ts.map +1 -0
- package/dist/json/LionWebJson.js +27 -0
- package/dist/json/LionWebJson.js.map +1 -0
- package/dist/json/LionWebJsonChunkWrapper.d.ts +39 -0
- package/dist/json/LionWebJsonChunkWrapper.d.ts.map +1 -0
- package/dist/json/LionWebJsonChunkWrapper.js +119 -0
- package/dist/json/LionWebJsonChunkWrapper.js.map +1 -0
- package/dist/json/M3definitions.d.ts +176 -0
- package/dist/json/M3definitions.d.ts.map +1 -0
- package/dist/json/M3definitions.js +174 -0
- package/dist/json/M3definitions.js.map +1 -0
- package/dist/json/NodeUtils.d.ts +23 -0
- package/dist/json/NodeUtils.d.ts.map +1 -0
- package/dist/json/NodeUtils.js +57 -0
- package/dist/json/NodeUtils.js.map +1 -0
- package/dist/json/index.d.ts +7 -0
- package/dist/json/index.d.ts.map +1 -0
- package/dist/json/index.js +7 -0
- package/dist/json/index.js.map +1 -0
- package/dist/languages/CompositeLionWebLanguageWrapper.d.ts +1 -1
- package/dist/languages/CompositeLionWebLanguageWrapper.d.ts.map +1 -1
- package/dist/languages/LanguageRegistry.d.ts +1 -1
- package/dist/languages/LanguageRegistry.d.ts.map +1 -1
- package/dist/languages/LanguageUtils.d.ts +1 -1
- package/dist/languages/LanguageUtils.d.ts.map +1 -1
- package/dist/languages/LanguageUtils.js +5 -5
- package/dist/languages/LanguageUtils.js.map +1 -1
- package/dist/languages/LionWebLanguageWrapper.d.ts +3 -3
- package/dist/languages/LionWebLanguageWrapper.d.ts.map +1 -1
- package/dist/languages/LionWebLanguageWrapper.js +2 -2
- package/dist/languages/LionWebLanguageWrapper.js.map +1 -1
- package/dist/languages/MetaPointerMap.d.ts +2 -2
- package/dist/languages/MetaPointerMap.d.ts.map +1 -1
- package/dist/languages/MetaPointerMap.js +1 -1
- package/dist/languages/MetaPointerMap.js.map +1 -1
- package/dist/runners/FileUtils.d.ts.map +1 -1
- package/dist/runners/FileUtils.js +1 -1
- package/dist/runners/FileUtils.js.map +1 -1
- package/dist/runners/RunCheckFolderWithLanguage.js.map +1 -1
- package/dist/runners/RunLioncoreDiff.js +1 -1
- package/dist/runners/RunLioncoreDiff.js.map +1 -1
- package/dist/util/graphs.d.ts +18 -0
- package/dist/util/graphs.d.ts.map +1 -0
- package/dist/util/graphs.js +27 -0
- package/dist/util/graphs.js.map +1 -0
- package/dist/validators/LionWebChunkDefinitions.d.ts +2 -2
- package/dist/validators/LionWebChunkDefinitions.d.ts.map +1 -1
- package/dist/validators/LionWebChunkDefinitions.js +20 -33
- package/dist/validators/LionWebChunkDefinitions.js.map +1 -1
- package/dist/validators/LionWebLanguageReferenceValidator.d.ts +4 -2
- package/dist/validators/LionWebLanguageReferenceValidator.d.ts.map +1 -1
- package/dist/validators/LionWebLanguageReferenceValidator.js +6 -2
- package/dist/validators/LionWebLanguageReferenceValidator.js.map +1 -1
- package/dist/validators/LionWebLanguageValidator.d.ts +3 -2
- package/dist/validators/LionWebLanguageValidator.d.ts.map +1 -1
- package/dist/validators/LionWebLanguageValidator.js +4 -2
- package/dist/validators/LionWebLanguageValidator.js.map +1 -1
- package/dist/validators/LionWebReferenceValidator.d.ts +4 -3
- package/dist/validators/LionWebReferenceValidator.d.ts.map +1 -1
- package/dist/validators/LionWebReferenceValidator.js +3 -2
- package/dist/validators/LionWebReferenceValidator.js.map +1 -1
- package/dist/validators/LionWebSyntaxValidator.d.ts.map +1 -1
- package/dist/validators/LionWebSyntaxValidator.js +1 -1
- package/dist/validators/LionWebSyntaxValidator.js.map +1 -1
- package/dist/validators/LionWebValidator.d.ts +1 -1
- package/dist/validators/LionWebValidator.d.ts.map +1 -1
- package/dist/validators/LionWebValidator.js +2 -2
- package/dist/validators/LionWebValidator.js.map +1 -1
- package/dist/validators/ValidationFunctions.d.ts +1 -5
- package/dist/validators/ValidationFunctions.d.ts.map +1 -1
- package/dist/validators/ValidationFunctions.js +4 -0
- package/dist/validators/ValidationFunctions.js.map +1 -1
- package/dist/validators/generic/SyntaxValidator.d.ts +9 -9
- package/dist/validators/generic/SyntaxValidator.d.ts.map +1 -1
- package/dist/validators/generic/SyntaxValidator.js +58 -61
- package/dist/validators/generic/SyntaxValidator.js.map +1 -1
- package/dist/validators/generic/ValidationTypes.d.ts +1 -25
- package/dist/validators/generic/ValidationTypes.d.ts.map +1 -1
- package/dist/validators/generic/ValidationTypes.js +1 -20
- package/dist/validators/generic/ValidationTypes.js.map +1 -1
- package/dist/validators/generic/index.d.ts +1 -0
- package/dist/validators/generic/index.d.ts.map +1 -1
- package/dist/validators/generic/index.js +1 -0
- package/dist/validators/generic/index.js.map +1 -1
- package/dist/validators/index.d.ts +1 -0
- package/dist/validators/index.d.ts.map +1 -1
- package/dist/validators/index.js +1 -0
- package/dist/validators/index.js.map +1 -1
- package/package.json +3 -8
- package/src/diff/DiffResult.ts +21 -0
- package/src/diff/LionWebJsonDiff.ts +309 -0
- package/src/diff/changes/Change.ts +76 -0
- package/src/diff/changes/ChunkChange.ts +68 -0
- package/src/diff/changes/ContainmentChange.ts +32 -0
- package/src/diff/changes/NodeChange.ts +63 -0
- package/src/diff/changes/PropertyChange.ts +31 -0
- package/src/diff/changes/ReferenceChange.ts +34 -0
- package/src/diff/changes/index.ts +6 -0
- package/src/diff/index.ts +3 -0
- package/src/index.ts +3 -0
- package/src/issues/LanguageIssues.ts +14 -12
- package/src/issues/ReferenceIssues.ts +9 -9
- package/src/issues/SyntaxIssues.ts +5 -5
- package/src/issues/ValidationIssue.ts +4 -7
- package/src/issues/index.ts +1 -0
- package/src/json/ChunkUtils.ts +49 -0
- package/src/json/JsonContext.ts +31 -0
- package/src/json/LionWebJson.ts +80 -0
- package/src/json/LionWebJsonChunkWrapper.ts +136 -0
- package/src/json/M3definitions.ts +177 -0
- package/src/json/NodeUtils.ts +75 -0
- package/src/json/index.ts +6 -0
- package/src/languages/CompositeLionWebLanguageWrapper.ts +1 -1
- package/src/languages/LanguageRegistry.ts +1 -1
- package/src/languages/LanguageUtils.ts +6 -6
- package/src/languages/LionWebLanguageWrapper.ts +9 -5
- package/src/languages/MetaPointerMap.ts +6 -7
- package/src/runners/FileUtils.ts +1 -1
- package/src/runners/RunCheckFolderWithLanguage.ts +1 -1
- package/src/runners/RunLioncoreDiff.ts +2 -2
- package/src/util/graphs.ts +36 -0
- package/src/validators/LionWebChunkDefinitions.ts +27 -35
- package/src/validators/LionWebLanguageReferenceValidator.ts +20 -18
- package/src/validators/LionWebLanguageValidator.ts +4 -2
- package/src/validators/LionWebReferenceValidator.ts +14 -6
- package/src/validators/LionWebSyntaxValidator.ts +1 -1
- package/src/validators/LionWebValidator.ts +3 -3
- package/src/validators/ValidationFunctions.ts +1 -1
- package/src/validators/generic/SyntaxValidator.ts +91 -98
- package/src/validators/generic/ValidationTypes.ts +4 -41
- package/src/validators/generic/index.ts +1 -0
- package/src/validators/index.ts +1 -0
|
@@ -1,13 +1,21 @@
|
|
|
1
|
-
import { LionWebId, LionWebJsonChunk, LionWebJsonContainment, LionWebJsonMetaPointer, LionWebJsonNode, LionWebJsonUsedLanguage } from "@lionweb/json"
|
|
2
|
-
import { ChunkUtils, JsonContext, LionWebJsonChunkWrapper } from "@lionweb/json-utils"
|
|
3
1
|
import {
|
|
4
2
|
Duplicates_Issue,
|
|
5
3
|
Reference_ChildMissingInParent_Issue,
|
|
6
4
|
Reference_CirculairParent_Issue,
|
|
7
5
|
Reference_DuplicateNodeId_Issue,
|
|
8
6
|
Reference_LanguageUnknown_Issue,
|
|
9
|
-
Reference_ParentMissingInChild_Issue
|
|
7
|
+
Reference_ParentMissingInChild_Issue,
|
|
10
8
|
} from "../issues/ReferenceIssues.js"
|
|
9
|
+
import { JsonContext } from "../json/JsonContext.js"
|
|
10
|
+
import { ChunkUtils } from "../json/ChunkUtils.js"
|
|
11
|
+
import {
|
|
12
|
+
LionWebJsonContainment,
|
|
13
|
+
LionWebJsonChunk,
|
|
14
|
+
LionWebJsonMetaPointer,
|
|
15
|
+
LionWebJsonNode,
|
|
16
|
+
LwJsonUsedLanguage,
|
|
17
|
+
} from "../json/LionWebJson.js"
|
|
18
|
+
import { LionWebJsonChunkWrapper } from "../json/LionWebJsonChunkWrapper.js"
|
|
11
19
|
import { ValidationResult } from "./generic/ValidationResult.js"
|
|
12
20
|
|
|
13
21
|
/**
|
|
@@ -128,7 +136,7 @@ export class LionWebReferenceValidator {
|
|
|
128
136
|
* @param usedLanguages
|
|
129
137
|
* @param context
|
|
130
138
|
*/
|
|
131
|
-
checkDuplicateUsedLanguage(usedLanguages:
|
|
139
|
+
checkDuplicateUsedLanguage(usedLanguages: LwJsonUsedLanguage[], context: JsonContext) {
|
|
132
140
|
if (usedLanguages === null || usedLanguages === undefined) {
|
|
133
141
|
return
|
|
134
142
|
}
|
|
@@ -189,7 +197,7 @@ export class LionWebReferenceValidator {
|
|
|
189
197
|
|
|
190
198
|
validateChildrenHaveCorrectParent(node: LionWebJsonNode, context: JsonContext) {
|
|
191
199
|
node.containments.forEach((child: LionWebJsonContainment) => {
|
|
192
|
-
child.children.forEach((childId:
|
|
200
|
+
child.children.forEach((childId: string, index: number) => {
|
|
193
201
|
const childNode = this.nodesIdMap.get(childId)
|
|
194
202
|
if (childNode !== undefined) {
|
|
195
203
|
if (childNode.parent !== node.id) {
|
|
@@ -203,7 +211,7 @@ export class LionWebReferenceValidator {
|
|
|
203
211
|
}
|
|
204
212
|
})
|
|
205
213
|
})
|
|
206
|
-
node.annotations.forEach((annotationId:
|
|
214
|
+
node.annotations.forEach((annotationId: string, annotationIndex: number) => {
|
|
207
215
|
const childNode = this.nodesIdMap.get(annotationId)
|
|
208
216
|
if (childNode !== undefined) {
|
|
209
217
|
if (childNode.parent === null || childNode.parent === undefined) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { expectedTypes } from "./LionWebChunkDefinitions.js"
|
|
1
2
|
import { SyntaxValidator } from "./generic/SyntaxValidator.js"
|
|
2
3
|
import { ValidationResult } from "./generic/ValidationResult.js"
|
|
3
|
-
import { expectedTypes } from "./LionWebChunkDefinitions.js"
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* LionWebSyntaxValidator can check whether objects are structurally LionWeb objects.
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { LionWebJsonChunk } from "
|
|
2
|
-
import { LionWebJsonChunkWrapper } from "
|
|
1
|
+
import { LionWebJsonChunk } from "../json/LionWebJson.js"
|
|
2
|
+
import { LionWebJsonChunkWrapper } from "../json/LionWebJsonChunkWrapper.js"
|
|
3
3
|
import { LanguageRegistry } from "../languages/index.js"
|
|
4
|
-
import { ValidationResult } from "./generic/ValidationResult.js"
|
|
5
4
|
import { LionWebLanguageReferenceValidator } from "./LionWebLanguageReferenceValidator.js"
|
|
6
5
|
import { LionWebReferenceValidator } from "./LionWebReferenceValidator.js"
|
|
7
6
|
import { LionWebSyntaxValidator } from "./LionWebSyntaxValidator.js"
|
|
7
|
+
import { ValidationResult } from "./generic/ValidationResult.js"
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Combined validator that calls all available validators.
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
* A list of functions that are used to validate primitive fields for LionWeb conformance.
|
|
3
3
|
* Used in the LionWebSyntaxValidator.
|
|
4
4
|
*/
|
|
5
|
-
import { JsonContext } from "@lionweb/json-utils"
|
|
6
5
|
import { Language_PropertyValue_Issue } from "../issues/LanguageIssues.js"
|
|
7
6
|
import {
|
|
8
7
|
Syntax_IdFormat_Issue,
|
|
@@ -11,6 +10,7 @@ import {
|
|
|
11
10
|
Syntax_SerializationFormatVersion_Issue,
|
|
12
11
|
Syntax_VersionFormat_Issue
|
|
13
12
|
} from "../issues/SyntaxIssues.js"
|
|
13
|
+
import { JsonContext } from "../json/JsonContext.js"
|
|
14
14
|
import { ValidationResult } from "./generic/ValidationResult.js"
|
|
15
15
|
import { PropertyDefinition } from "./generic/ValidationTypes.js"
|
|
16
16
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { JsonContext } from "@lionweb/json-utils"
|
|
2
1
|
import {
|
|
3
2
|
Syntax_ArrayContainsNull_Issue,
|
|
4
3
|
Syntax_PropertyMissingIssue,
|
|
@@ -6,27 +5,21 @@ import {
|
|
|
6
5
|
Syntax_PropertyTypeIssue,
|
|
7
6
|
Syntax_PropertyUnknownIssue
|
|
8
7
|
} from "../../issues/index.js"
|
|
8
|
+
import { JsonContext } from "../../json/JsonContext.js"
|
|
9
9
|
import { ValidationResult } from "./ValidationResult.js"
|
|
10
|
-
import {
|
|
11
|
-
isObjectDefinition,
|
|
12
|
-
isPrimitiveDefinition,
|
|
13
|
-
ObjectDefinition,
|
|
14
|
-
PrimitiveDefinition,
|
|
15
|
-
TypeDefinition,
|
|
16
|
-
UnknownObjectType
|
|
17
|
-
} from "./ValidationTypes.js"
|
|
10
|
+
import { PropertyDefinition, UnknownObjectType } from "./ValidationTypes.js"
|
|
18
11
|
|
|
19
12
|
/**
|
|
20
13
|
* Syntax Validator can check whether objects are structurally conforming to the
|
|
21
|
-
* definitions given in `
|
|
14
|
+
* definitions given in `expectedTypes`.
|
|
22
15
|
*/
|
|
23
16
|
export class SyntaxValidator {
|
|
24
17
|
validationResult: ValidationResult
|
|
25
|
-
|
|
18
|
+
expectedTypes: Map<string, PropertyDefinition[]>
|
|
26
19
|
|
|
27
|
-
constructor(validationResult: ValidationResult, expectedTypes: Map<string,
|
|
20
|
+
constructor(validationResult: ValidationResult, expectedTypes: Map<string, PropertyDefinition[]>) {
|
|
28
21
|
this.validationResult = validationResult
|
|
29
|
-
this.
|
|
22
|
+
this.expectedTypes = expectedTypes
|
|
30
23
|
}
|
|
31
24
|
|
|
32
25
|
/**
|
|
@@ -36,119 +29,115 @@ export class SyntaxValidator {
|
|
|
36
29
|
* @param expectedType The expected type of the object.
|
|
37
30
|
*/
|
|
38
31
|
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
|
+
}
|
|
39
35
|
const object = obj as UnknownObjectType
|
|
40
|
-
const
|
|
41
|
-
if (
|
|
36
|
+
const defs = this.expectedTypes.get(expectedType)
|
|
37
|
+
if (defs === undefined) {
|
|
42
38
|
throw new Error(`SyntaxValidator.validate: cannot find definition for ${expectedType}`)
|
|
43
|
-
} else
|
|
44
|
-
this.validateObjectProperties(expectedType,
|
|
45
|
-
} else if( isPrimitiveDefinition(typeDef)) {
|
|
46
|
-
this.validatePrimitiveValue("$", typeDef, object, new JsonContext(null, ["$"]))
|
|
39
|
+
} else {
|
|
40
|
+
this.validateObjectProperties(expectedType, defs, object, new JsonContext(null, ["$"]))
|
|
47
41
|
}
|
|
48
42
|
}
|
|
49
43
|
|
|
50
44
|
/**
|
|
51
45
|
* Validate whether `object` is structured conform the properties in `propertyDef`
|
|
52
46
|
* @param originalProperty The property of which `object` it the value
|
|
53
|
-
* @param
|
|
47
|
+
* @param propertyDef The property definitions that are being validated
|
|
54
48
|
* @param object The object being validated
|
|
55
49
|
* @param jsonContext The location in the JSON
|
|
56
50
|
* @private
|
|
57
51
|
*/
|
|
58
|
-
validateObjectProperties(originalProperty: string,
|
|
59
|
-
if (
|
|
52
|
+
private validateObjectProperties(originalProperty: string, propertyDef: PropertyDefinition[], object: UnknownObjectType, jsonContext: JsonContext) {
|
|
53
|
+
if (propertyDef.length === 0) {
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
if ((typeof object) !== "object") {
|
|
57
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, originalProperty, "object", typeof object))
|
|
60
58
|
return
|
|
61
59
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
for (const pdef of propertyDef) {
|
|
61
|
+
const expectedPropertyDefs = this.expectedTypes.get(pdef.expectedType)
|
|
62
|
+
const validator = pdef.validate!
|
|
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
|
|
65
67
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (!
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (propertyDef.mayBeNull && propertyValue === null) {
|
|
81
|
-
// Ok, stop checking, continue with next property def
|
|
82
|
-
continue
|
|
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
|
|
75
|
+
}
|
|
76
|
+
if (pdef.isList) {
|
|
77
|
+
// Check whether value is an array
|
|
78
|
+
if (!Array.isArray(propertyValue)) {
|
|
79
|
+
const newContext = jsonContext.concat(pdef.property)
|
|
80
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(newContext, pdef.property, "array", typeof propertyValue))
|
|
81
|
+
return
|
|
83
82
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
this.validationResult.issue(new
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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])
|
|
83
|
+
// If an array, validate every item in the array
|
|
84
|
+
(propertyValue as UnknownObjectType[]).forEach( (item, index) => {
|
|
85
|
+
const newContext = jsonContext.concat(pdef.property, index)
|
|
86
|
+
if (item === null) {
|
|
87
|
+
this.validationResult.issue(new Syntax_ArrayContainsNull_Issue(newContext, pdef.property,index ))
|
|
88
|
+
} else {
|
|
89
|
+
if (expectedPropertyDefs !== undefined) {
|
|
90
|
+
if (expectedPropertyDefs.length === 0) {
|
|
91
|
+
// propertyValue should be a primitive as it has no property definitions
|
|
92
|
+
if (this.validatePrimitiveValue(pdef, item, jsonContext)) {
|
|
93
|
+
validator.apply(null, [item, this.validationResult, newContext, pdef])
|
|
107
94
|
}
|
|
108
95
|
} else {
|
|
109
|
-
|
|
96
|
+
// propertyValue should be an object, validate its properties
|
|
97
|
+
this.validateObjectProperties(pdef.property, expectedPropertyDefs, item as UnknownObjectType, newContext)
|
|
98
|
+
validator.apply(null, [item, this.validationResult, newContext, pdef])
|
|
110
99
|
}
|
|
100
|
+
} else {
|
|
101
|
+
throw new Error(`Expected type '${pdef.expectedType} has neither property defs, nor a validator.`)
|
|
111
102
|
}
|
|
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
|
|
118
103
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
104
|
+
})
|
|
105
|
+
} else {
|
|
106
|
+
const newContext = jsonContext.concat(pdef.property)
|
|
107
|
+
if (Array.isArray(propertyValue)) {
|
|
108
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(newContext, pdef.property, pdef.expectedType, "array"))
|
|
109
|
+
return
|
|
110
|
+
}
|
|
111
|
+
// Single valued property, validate it
|
|
112
|
+
if (expectedPropertyDefs !== undefined) {
|
|
113
|
+
if (expectedPropertyDefs.length === 0) {
|
|
114
|
+
// propertyValue should be a primitive as it has no property definitions
|
|
115
|
+
if (this.validatePrimitiveValue(pdef, propertyValue, jsonContext)) {
|
|
116
|
+
validator.apply(null, [propertyValue, this.validationResult, newContext, pdef])
|
|
132
117
|
}
|
|
133
118
|
} else {
|
|
134
|
-
|
|
119
|
+
// propertyValue should be an object, validate its properties
|
|
120
|
+
this.validateObjectProperties(pdef.property, expectedPropertyDefs, propertyValue as UnknownObjectType, newContext)
|
|
121
|
+
validator.apply(null, [propertyValue, this.validationResult, newContext, pdef])
|
|
135
122
|
}
|
|
123
|
+
} else {
|
|
124
|
+
throw new Error(`Expected single type '${pdef.expectedType}' for '${pdef.property}' at ${newContext.toString()} has neither property defs, nor a validator.`)
|
|
136
125
|
}
|
|
137
126
|
}
|
|
138
|
-
|
|
127
|
+
}
|
|
128
|
+
this.checkStrayProperties(object, propertyDef.map(pdef => pdef.property ), jsonContext)
|
|
139
129
|
}
|
|
140
130
|
|
|
141
|
-
validatePrimitiveValue(
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
131
|
+
validatePrimitiveValue(propDef: PropertyDefinition, object: unknown, jsonContext: JsonContext): boolean {
|
|
132
|
+
if (!propDef.mayBeNull && (object === null || object === undefined)) {
|
|
133
|
+
this.validationResult.issue(new Syntax_PropertyNullIssue(jsonContext, propDef.property))
|
|
134
|
+
return false
|
|
135
|
+
}
|
|
146
136
|
|
|
147
|
-
if (typeof object !== propDef.
|
|
148
|
-
this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext,
|
|
137
|
+
if (typeof object !== propDef.expectedType) {
|
|
138
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, propDef.property, propDef.expectedType,typeof object))
|
|
149
139
|
return false
|
|
150
140
|
}
|
|
151
|
-
propDef.validate!(object, this.validationResult, jsonContext)
|
|
152
141
|
return true
|
|
153
142
|
}
|
|
154
143
|
|
|
@@ -158,13 +147,17 @@ export class SyntaxValidator {
|
|
|
158
147
|
* @param properties The names of the expected properties
|
|
159
148
|
* @param context Location in JSON
|
|
160
149
|
*/
|
|
161
|
-
checkStrayProperties(obj: UnknownObjectType,
|
|
150
|
+
checkStrayProperties(obj: UnknownObjectType, properties: string[], context: JsonContext) {
|
|
162
151
|
const own = Object.getOwnPropertyNames(obj)
|
|
163
|
-
const defined = def.map(pdef => pdef.property)
|
|
164
152
|
own.forEach((ownProp) => {
|
|
165
|
-
if (!
|
|
153
|
+
if (!properties.includes(ownProp)) {
|
|
166
154
|
this.validationResult.issue(new Syntax_PropertyUnknownIssue(context, ownProp))
|
|
167
155
|
}
|
|
168
156
|
})
|
|
157
|
+
properties.forEach((prop) => {
|
|
158
|
+
if (!own.includes(prop)) {
|
|
159
|
+
this.validationResult.issue(new Syntax_PropertyMissingIssue(context, prop))
|
|
160
|
+
}
|
|
161
|
+
})
|
|
169
162
|
}
|
|
170
163
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { JsonContext } from "
|
|
1
|
+
import { JsonContext } from "../../json/index.js"
|
|
2
2
|
import { ValidationResult } from "./ValidationResult.js"
|
|
3
3
|
|
|
4
4
|
export type UnknownObjectType = { [key: string]: unknown }
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Definition of a property, used by the SyntaxValidator to validate objects.
|
|
8
|
-
*
|
|
8
|
+
*
|
|
9
9
|
* **Note** that some of the properties are defined as optional.
|
|
10
10
|
* They should not be empty ever!! But being optional allows to leave them out in the `PropertyDef` function.
|
|
11
11
|
* The `PropertyDef` function sets default values for all optional fields.
|
|
@@ -28,10 +28,6 @@ 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,
|
|
35
31
|
/**
|
|
36
32
|
* Additional validation function
|
|
37
33
|
*/
|
|
@@ -48,7 +44,7 @@ export type ValidatorFunction = <T>(obj: T, result: ValidationResult, ctx: JsonC
|
|
|
48
44
|
* @param pdef
|
|
49
45
|
*/
|
|
50
46
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
51
|
-
function emptyValidation<T>(object: T, result: ValidationResult, ctx: JsonContext,
|
|
47
|
+
function emptyValidation<T>(object: T, result: ValidationResult, ctx: JsonContext, pdef?: PropertyDefinition): void {}
|
|
52
48
|
|
|
53
49
|
// Make boolean argument more readable.
|
|
54
50
|
export const MAY_BE_NULL = true
|
|
@@ -59,46 +55,13 @@ export const MAY_BE_NULL = true
|
|
|
59
55
|
* @constructor
|
|
60
56
|
*/
|
|
61
57
|
export function PropertyDef(propDef: PropertyDefinition): PropertyDefinition {
|
|
62
|
-
const { property, expectedType, mayBeNull = false, isList = false,
|
|
58
|
+
const { property, expectedType, mayBeNull = false, isList = false, validate = emptyValidation } = propDef
|
|
63
59
|
return {
|
|
64
60
|
property: property,
|
|
65
61
|
expectedType: expectedType,
|
|
66
62
|
isList: isList,
|
|
67
63
|
mayBeNull: mayBeNull,
|
|
68
|
-
isOptional: isOptional,
|
|
69
64
|
validate: validate
|
|
70
65
|
}
|
|
71
66
|
}
|
|
72
67
|
|
|
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,
|
|
92
|
-
validate: validate
|
|
93
|
-
}
|
|
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
|
-
}
|