@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.
Files changed (202) hide show
  1. package/dist/diff/DiffResult.d.ts +9 -0
  2. package/dist/diff/DiffResult.d.ts.map +1 -0
  3. package/dist/diff/DiffResult.js +18 -0
  4. package/dist/diff/DiffResult.js.map +1 -0
  5. package/dist/diff/LionWebJsonDiff.d.ts +25 -0
  6. package/dist/diff/LionWebJsonDiff.d.ts.map +1 -0
  7. package/dist/diff/LionWebJsonDiff.js +249 -0
  8. package/dist/diff/LionWebJsonDiff.js.map +1 -0
  9. package/dist/diff/changes/Change.d.ts +34 -0
  10. package/dist/diff/changes/Change.d.ts.map +1 -0
  11. package/dist/diff/changes/Change.js +38 -0
  12. package/dist/diff/changes/Change.js.map +1 -0
  13. package/dist/diff/changes/ChunkChange.d.ts +43 -0
  14. package/dist/diff/changes/ChunkChange.d.ts.map +1 -0
  15. package/dist/diff/changes/ChunkChange.js +57 -0
  16. package/dist/diff/changes/ChunkChange.js.map +1 -0
  17. package/dist/diff/changes/ContainmentChange.d.ts +25 -0
  18. package/dist/diff/changes/ContainmentChange.d.ts.map +1 -0
  19. package/dist/diff/changes/ContainmentChange.js +34 -0
  20. package/dist/diff/changes/ContainmentChange.js.map +1 -0
  21. package/dist/diff/changes/NodeChange.d.ts +41 -0
  22. package/dist/diff/changes/NodeChange.d.ts.map +1 -0
  23. package/dist/diff/changes/NodeChange.js +54 -0
  24. package/dist/diff/changes/NodeChange.js.map +1 -0
  25. package/dist/diff/changes/PropertyChange.d.ts +24 -0
  26. package/dist/diff/changes/PropertyChange.d.ts.map +1 -0
  27. package/dist/diff/changes/PropertyChange.js +29 -0
  28. package/dist/diff/changes/PropertyChange.js.map +1 -0
  29. package/dist/diff/changes/ReferenceChange.d.ts +25 -0
  30. package/dist/diff/changes/ReferenceChange.d.ts.map +1 -0
  31. package/dist/diff/changes/ReferenceChange.js +34 -0
  32. package/dist/diff/changes/ReferenceChange.js.map +1 -0
  33. package/dist/diff/changes/index.d.ts +7 -0
  34. package/dist/diff/changes/index.d.ts.map +1 -0
  35. package/dist/diff/changes/index.js +7 -0
  36. package/dist/diff/changes/index.js.map +1 -0
  37. package/dist/diff/index.d.ts +4 -0
  38. package/dist/diff/index.d.ts.map +1 -0
  39. package/dist/diff/index.js +4 -0
  40. package/dist/diff/index.js.map +1 -0
  41. package/dist/index.d.ts +3 -0
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +3 -0
  44. package/dist/index.js.map +1 -1
  45. package/dist/issues/LanguageIssues.d.ts +4 -4
  46. package/dist/issues/LanguageIssues.d.ts.map +1 -1
  47. package/dist/issues/LanguageIssues.js +1 -1
  48. package/dist/issues/LanguageIssues.js.map +1 -1
  49. package/dist/issues/ReferenceIssues.d.ts +6 -6
  50. package/dist/issues/ReferenceIssues.d.ts.map +1 -1
  51. package/dist/issues/ReferenceIssues.js +3 -3
  52. package/dist/issues/ReferenceIssues.js.map +1 -1
  53. package/dist/issues/SyntaxIssues.d.ts +1 -1
  54. package/dist/issues/SyntaxIssues.d.ts.map +1 -1
  55. package/dist/issues/ValidationIssue.d.ts +1 -1
  56. package/dist/issues/ValidationIssue.d.ts.map +1 -1
  57. package/dist/issues/ValidationIssue.js.map +1 -1
  58. package/dist/issues/index.d.ts +1 -0
  59. package/dist/issues/index.d.ts.map +1 -1
  60. package/dist/issues/index.js +1 -0
  61. package/dist/issues/index.js.map +1 -1
  62. package/dist/json/ChunkUtils.d.ts +26 -0
  63. package/dist/json/ChunkUtils.d.ts.map +1 -0
  64. package/dist/json/ChunkUtils.js +46 -0
  65. package/dist/json/ChunkUtils.js.map +1 -0
  66. package/dist/json/JsonContext.d.ts +11 -0
  67. package/dist/json/JsonContext.d.ts.map +1 -0
  68. package/dist/json/JsonContext.js +25 -0
  69. package/dist/json/JsonContext.js.map +1 -0
  70. package/dist/json/LionWebJson.d.ts +50 -0
  71. package/dist/json/LionWebJson.d.ts.map +1 -0
  72. package/dist/json/LionWebJson.js +27 -0
  73. package/dist/json/LionWebJson.js.map +1 -0
  74. package/dist/json/LionWebJsonChunkWrapper.d.ts +39 -0
  75. package/dist/json/LionWebJsonChunkWrapper.d.ts.map +1 -0
  76. package/dist/json/LionWebJsonChunkWrapper.js +119 -0
  77. package/dist/json/LionWebJsonChunkWrapper.js.map +1 -0
  78. package/dist/json/M3definitions.d.ts +176 -0
  79. package/dist/json/M3definitions.d.ts.map +1 -0
  80. package/dist/json/M3definitions.js +174 -0
  81. package/dist/json/M3definitions.js.map +1 -0
  82. package/dist/json/NodeUtils.d.ts +23 -0
  83. package/dist/json/NodeUtils.d.ts.map +1 -0
  84. package/dist/json/NodeUtils.js +57 -0
  85. package/dist/json/NodeUtils.js.map +1 -0
  86. package/dist/json/index.d.ts +7 -0
  87. package/dist/json/index.d.ts.map +1 -0
  88. package/dist/json/index.js +7 -0
  89. package/dist/json/index.js.map +1 -0
  90. package/dist/languages/CompositeLionWebLanguageWrapper.d.ts +1 -1
  91. package/dist/languages/CompositeLionWebLanguageWrapper.d.ts.map +1 -1
  92. package/dist/languages/LanguageRegistry.d.ts +1 -1
  93. package/dist/languages/LanguageRegistry.d.ts.map +1 -1
  94. package/dist/languages/LanguageUtils.d.ts +1 -1
  95. package/dist/languages/LanguageUtils.d.ts.map +1 -1
  96. package/dist/languages/LanguageUtils.js +5 -5
  97. package/dist/languages/LanguageUtils.js.map +1 -1
  98. package/dist/languages/LionWebLanguageWrapper.d.ts +3 -3
  99. package/dist/languages/LionWebLanguageWrapper.d.ts.map +1 -1
  100. package/dist/languages/LionWebLanguageWrapper.js +2 -2
  101. package/dist/languages/LionWebLanguageWrapper.js.map +1 -1
  102. package/dist/languages/MetaPointerMap.d.ts +2 -2
  103. package/dist/languages/MetaPointerMap.d.ts.map +1 -1
  104. package/dist/languages/MetaPointerMap.js +1 -1
  105. package/dist/languages/MetaPointerMap.js.map +1 -1
  106. package/dist/runners/FileUtils.d.ts.map +1 -1
  107. package/dist/runners/FileUtils.js +1 -1
  108. package/dist/runners/FileUtils.js.map +1 -1
  109. package/dist/runners/RunCheckFolderWithLanguage.js.map +1 -1
  110. package/dist/runners/RunLioncoreDiff.js +1 -1
  111. package/dist/runners/RunLioncoreDiff.js.map +1 -1
  112. package/dist/util/graphs.d.ts +18 -0
  113. package/dist/util/graphs.d.ts.map +1 -0
  114. package/dist/util/graphs.js +27 -0
  115. package/dist/util/graphs.js.map +1 -0
  116. package/dist/validators/LionWebChunkDefinitions.d.ts +2 -2
  117. package/dist/validators/LionWebChunkDefinitions.d.ts.map +1 -1
  118. package/dist/validators/LionWebChunkDefinitions.js +20 -33
  119. package/dist/validators/LionWebChunkDefinitions.js.map +1 -1
  120. package/dist/validators/LionWebLanguageReferenceValidator.d.ts +4 -2
  121. package/dist/validators/LionWebLanguageReferenceValidator.d.ts.map +1 -1
  122. package/dist/validators/LionWebLanguageReferenceValidator.js +6 -2
  123. package/dist/validators/LionWebLanguageReferenceValidator.js.map +1 -1
  124. package/dist/validators/LionWebLanguageValidator.d.ts +3 -2
  125. package/dist/validators/LionWebLanguageValidator.d.ts.map +1 -1
  126. package/dist/validators/LionWebLanguageValidator.js +4 -2
  127. package/dist/validators/LionWebLanguageValidator.js.map +1 -1
  128. package/dist/validators/LionWebReferenceValidator.d.ts +4 -3
  129. package/dist/validators/LionWebReferenceValidator.d.ts.map +1 -1
  130. package/dist/validators/LionWebReferenceValidator.js +3 -2
  131. package/dist/validators/LionWebReferenceValidator.js.map +1 -1
  132. package/dist/validators/LionWebSyntaxValidator.d.ts.map +1 -1
  133. package/dist/validators/LionWebSyntaxValidator.js +1 -1
  134. package/dist/validators/LionWebSyntaxValidator.js.map +1 -1
  135. package/dist/validators/LionWebValidator.d.ts +1 -1
  136. package/dist/validators/LionWebValidator.d.ts.map +1 -1
  137. package/dist/validators/LionWebValidator.js +2 -2
  138. package/dist/validators/LionWebValidator.js.map +1 -1
  139. package/dist/validators/ValidationFunctions.d.ts +1 -5
  140. package/dist/validators/ValidationFunctions.d.ts.map +1 -1
  141. package/dist/validators/ValidationFunctions.js +4 -0
  142. package/dist/validators/ValidationFunctions.js.map +1 -1
  143. package/dist/validators/generic/SyntaxValidator.d.ts +9 -9
  144. package/dist/validators/generic/SyntaxValidator.d.ts.map +1 -1
  145. package/dist/validators/generic/SyntaxValidator.js +58 -61
  146. package/dist/validators/generic/SyntaxValidator.js.map +1 -1
  147. package/dist/validators/generic/ValidationTypes.d.ts +1 -25
  148. package/dist/validators/generic/ValidationTypes.d.ts.map +1 -1
  149. package/dist/validators/generic/ValidationTypes.js +1 -20
  150. package/dist/validators/generic/ValidationTypes.js.map +1 -1
  151. package/dist/validators/generic/index.d.ts +1 -0
  152. package/dist/validators/generic/index.d.ts.map +1 -1
  153. package/dist/validators/generic/index.js +1 -0
  154. package/dist/validators/generic/index.js.map +1 -1
  155. package/dist/validators/index.d.ts +1 -0
  156. package/dist/validators/index.d.ts.map +1 -1
  157. package/dist/validators/index.js +1 -0
  158. package/dist/validators/index.js.map +1 -1
  159. package/package.json +3 -8
  160. package/src/diff/DiffResult.ts +21 -0
  161. package/src/diff/LionWebJsonDiff.ts +309 -0
  162. package/src/diff/changes/Change.ts +76 -0
  163. package/src/diff/changes/ChunkChange.ts +68 -0
  164. package/src/diff/changes/ContainmentChange.ts +32 -0
  165. package/src/diff/changes/NodeChange.ts +63 -0
  166. package/src/diff/changes/PropertyChange.ts +31 -0
  167. package/src/diff/changes/ReferenceChange.ts +34 -0
  168. package/src/diff/changes/index.ts +6 -0
  169. package/src/diff/index.ts +3 -0
  170. package/src/index.ts +3 -0
  171. package/src/issues/LanguageIssues.ts +14 -12
  172. package/src/issues/ReferenceIssues.ts +9 -9
  173. package/src/issues/SyntaxIssues.ts +5 -5
  174. package/src/issues/ValidationIssue.ts +4 -7
  175. package/src/issues/index.ts +1 -0
  176. package/src/json/ChunkUtils.ts +49 -0
  177. package/src/json/JsonContext.ts +31 -0
  178. package/src/json/LionWebJson.ts +80 -0
  179. package/src/json/LionWebJsonChunkWrapper.ts +136 -0
  180. package/src/json/M3definitions.ts +177 -0
  181. package/src/json/NodeUtils.ts +75 -0
  182. package/src/json/index.ts +6 -0
  183. package/src/languages/CompositeLionWebLanguageWrapper.ts +1 -1
  184. package/src/languages/LanguageRegistry.ts +1 -1
  185. package/src/languages/LanguageUtils.ts +6 -6
  186. package/src/languages/LionWebLanguageWrapper.ts +9 -5
  187. package/src/languages/MetaPointerMap.ts +6 -7
  188. package/src/runners/FileUtils.ts +1 -1
  189. package/src/runners/RunCheckFolderWithLanguage.ts +1 -1
  190. package/src/runners/RunLioncoreDiff.ts +2 -2
  191. package/src/util/graphs.ts +36 -0
  192. package/src/validators/LionWebChunkDefinitions.ts +27 -35
  193. package/src/validators/LionWebLanguageReferenceValidator.ts +20 -18
  194. package/src/validators/LionWebLanguageValidator.ts +4 -2
  195. package/src/validators/LionWebReferenceValidator.ts +14 -6
  196. package/src/validators/LionWebSyntaxValidator.ts +1 -1
  197. package/src/validators/LionWebValidator.ts +3 -3
  198. package/src/validators/ValidationFunctions.ts +1 -1
  199. package/src/validators/generic/SyntaxValidator.ts +91 -98
  200. package/src/validators/generic/ValidationTypes.ts +4 -41
  201. package/src/validators/generic/index.ts +1 -0
  202. 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: LionWebJsonUsedLanguage[], context: JsonContext) {
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: LionWebId, index: number) => {
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: LionWebId, annotationIndex: number) => {
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 "@lionweb/json"
2
- import { LionWebJsonChunkWrapper } from "@lionweb/json-utils"
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 `typeDefinitions`.
14
+ * definitions given in `expectedTypes`.
22
15
  */
23
16
  export class SyntaxValidator {
24
17
  validationResult: ValidationResult
25
- typeDefinitions: Map<string, TypeDefinition>
18
+ expectedTypes: Map<string, PropertyDefinition[]>
26
19
 
27
- constructor(validationResult: ValidationResult, expectedTypes: Map<string, TypeDefinition>) {
20
+ constructor(validationResult: ValidationResult, expectedTypes: Map<string, PropertyDefinition[]>) {
28
21
  this.validationResult = validationResult
29
- this.typeDefinitions = expectedTypes
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 typeDef = this.typeDefinitions.get(expectedType)
41
- if (typeDef === undefined) {
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 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, ["$"]))
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 typeDef The property definitions that are being validated
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, typeDef: ObjectDefinition, object: UnknownObjectType, jsonContext: JsonContext) {
59
- if (typeDef === null || typeDef === undefined) {
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
- if ((typeof object) !== "object") {
63
- this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, originalProperty, "object", typeof object))
64
- return
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
- 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
75
- }
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
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
- 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])
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
- throw new Error(`Expected type '${propertyDef.expectedType} has neither property defs, nor a validator.`)
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
- // 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")
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
- throw new Error(`Expected single type '${propertyDef.expectedType}' for '${propertyDef.property}' at ${newContext.toString()} has neither property defs, nor a validator.`)
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
- this.checkStrayProperties(object, typeDef, jsonContext)
127
+ }
128
+ this.checkStrayProperties(object, propertyDef.map(pdef => pdef.property ), jsonContext)
139
129
  }
140
130
 
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
- // }
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.primitiveType) {
148
- this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, propertyName, propDef.primitiveType,typeof object))
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, def: ObjectDefinition, context: JsonContext) {
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 (!defined.includes(ownProp)) {
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 "@lionweb/json-utils"
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, pdef?: PropertyDefinition): void {}
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, isOptional = false, validate = emptyValidation } = propDef
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
- }
@@ -1,3 +1,4 @@
1
1
  export * from "./ValidationTypes.js"
2
2
  export * from "./ValidationResult.js"
3
+ export * from "../ValidationFunctions.js"
3
4
  export * from "./SyntaxValidator.js"
@@ -1,4 +1,5 @@
1
1
  export * from "./generic/index.js"
2
+ export * from "./generic/ValidationResult.js"
2
3
  export * from "./LionWebSyntaxValidator.js"
3
4
  export * from "./LionWebReferenceValidator.js"
4
5
  export * from "./LionWebLanguageValidator.js"