@lionweb/validation 0.7.0-beta.0 → 0.7.0-beta.10

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 (142) hide show
  1. package/package.json +7 -6
  2. package/src/issues/ValidationIssue.ts +4 -4
  3. package/src/runners/Utils.ts +2 -2
  4. package/src/validators/LionWebChunkDefinitions.ts +80 -90
  5. package/src/validators/LionWebSyntaxValidator.ts +2 -2
  6. package/src/validators/LionWebValidator.ts +1 -1
  7. package/src/validators/ValidationFunctions.ts +5 -5
  8. package/src/validators/generic/SyntaxValidator.ts +133 -87
  9. package/src/validators/generic/index.ts +1 -1
  10. package/src/validators/generic/schema/DefinitionSchema.ts +52 -0
  11. package/src/validators/generic/{ValidationTypes.ts → schema/ValidationTypes.ts} +65 -35
  12. package/src/validators/generic/schema/index.ts +2 -0
  13. package/dist/index.d.ts +0 -5
  14. package/dist/index.d.ts.map +0 -1
  15. package/dist/index.js +0 -5
  16. package/dist/index.js.map +0 -1
  17. package/dist/issues/LanguageIssues.d.ts +0 -99
  18. package/dist/issues/LanguageIssues.d.ts.map +0 -1
  19. package/dist/issues/LanguageIssues.js +0 -146
  20. package/dist/issues/LanguageIssues.js.map +0 -1
  21. package/dist/issues/ReferenceIssues.d.ts +0 -43
  22. package/dist/issues/ReferenceIssues.d.ts.map +0 -1
  23. package/dist/issues/ReferenceIssues.js +0 -53
  24. package/dist/issues/ReferenceIssues.js.map +0 -1
  25. package/dist/issues/SyntaxIssues.d.ts +0 -53
  26. package/dist/issues/SyntaxIssues.d.ts.map +0 -1
  27. package/dist/issues/SyntaxIssues.js +0 -81
  28. package/dist/issues/SyntaxIssues.js.map +0 -1
  29. package/dist/issues/ValidationIssue.d.ts +0 -15
  30. package/dist/issues/ValidationIssue.d.ts.map +0 -1
  31. package/dist/issues/ValidationIssue.js +0 -17
  32. package/dist/issues/ValidationIssue.js.map +0 -1
  33. package/dist/issues/index.d.ts +0 -5
  34. package/dist/issues/index.d.ts.map +0 -1
  35. package/dist/issues/index.js +0 -5
  36. package/dist/issues/index.js.map +0 -1
  37. package/dist/languages/CompositeLionWebLanguageWrapper.d.ts +0 -24
  38. package/dist/languages/CompositeLionWebLanguageWrapper.d.ts.map +0 -1
  39. package/dist/languages/CompositeLionWebLanguageWrapper.js +0 -56
  40. package/dist/languages/CompositeLionWebLanguageWrapper.js.map +0 -1
  41. package/dist/languages/LanguageRegistry.d.ts +0 -19
  42. package/dist/languages/LanguageRegistry.d.ts.map +0 -1
  43. package/dist/languages/LanguageRegistry.js +0 -37
  44. package/dist/languages/LanguageRegistry.js.map +0 -1
  45. package/dist/languages/LanguageUtils.d.ts +0 -35
  46. package/dist/languages/LanguageUtils.d.ts.map +0 -1
  47. package/dist/languages/LanguageUtils.js +0 -54
  48. package/dist/languages/LanguageUtils.js.map +0 -1
  49. package/dist/languages/LionCore-M3.json +0 -2356
  50. package/dist/languages/LionCore-builtins.json +0 -372
  51. package/dist/languages/LionWebLanguageWrapper.d.ts +0 -31
  52. package/dist/languages/LionWebLanguageWrapper.d.ts.map +0 -1
  53. package/dist/languages/LionWebLanguageWrapper.js +0 -69
  54. package/dist/languages/LionWebLanguageWrapper.js.map +0 -1
  55. package/dist/languages/MetaPointerMap.d.ts +0 -11
  56. package/dist/languages/MetaPointerMap.d.ts.map +0 -1
  57. package/dist/languages/MetaPointerMap.js +0 -39
  58. package/dist/languages/MetaPointerMap.js.map +0 -1
  59. package/dist/languages/index.d.ts +0 -3
  60. package/dist/languages/index.d.ts.map +0 -1
  61. package/dist/languages/index.js +0 -3
  62. package/dist/languages/index.js.map +0 -1
  63. package/dist/runners/FileUtils.d.ts +0 -6
  64. package/dist/runners/FileUtils.d.ts.map +0 -1
  65. package/dist/runners/FileUtils.js +0 -53
  66. package/dist/runners/FileUtils.js.map +0 -1
  67. package/dist/runners/RunCheckFolder.d.ts +0 -2
  68. package/dist/runners/RunCheckFolder.d.ts.map +0 -1
  69. package/dist/runners/RunCheckFolder.js +0 -6
  70. package/dist/runners/RunCheckFolder.js.map +0 -1
  71. package/dist/runners/RunCheckFolderWithLanguage.d.ts +0 -2
  72. package/dist/runners/RunCheckFolderWithLanguage.d.ts.map +0 -1
  73. package/dist/runners/RunCheckFolderWithLanguage.js +0 -40
  74. package/dist/runners/RunCheckFolderWithLanguage.js.map +0 -1
  75. package/dist/runners/RunCheckOneFile.d.ts +0 -2
  76. package/dist/runners/RunCheckOneFile.d.ts.map +0 -1
  77. package/dist/runners/RunCheckOneFile.js +0 -6
  78. package/dist/runners/RunCheckOneFile.js.map +0 -1
  79. package/dist/runners/RunCheckOneFileWithLanguage.d.ts +0 -2
  80. package/dist/runners/RunCheckOneFileWithLanguage.d.ts.map +0 -1
  81. package/dist/runners/RunCheckOneFileWithLanguage.js +0 -31
  82. package/dist/runners/RunCheckOneFileWithLanguage.js.map +0 -1
  83. package/dist/runners/RunLioncoreDiff.d.ts +0 -2
  84. package/dist/runners/RunLioncoreDiff.d.ts.map +0 -1
  85. package/dist/runners/RunLioncoreDiff.js +0 -22
  86. package/dist/runners/RunLioncoreDiff.js.map +0 -1
  87. package/dist/runners/Utils.d.ts +0 -7
  88. package/dist/runners/Utils.d.ts.map +0 -1
  89. package/dist/runners/Utils.js +0 -48
  90. package/dist/runners/Utils.js.map +0 -1
  91. package/dist/runners/index.d.ts +0 -3
  92. package/dist/runners/index.d.ts.map +0 -1
  93. package/dist/runners/index.js +0 -3
  94. package/dist/runners/index.js.map +0 -1
  95. package/dist/validators/LionWebChunkDefinitions.d.ts +0 -9
  96. package/dist/validators/LionWebChunkDefinitions.d.ts.map +0 -1
  97. package/dist/validators/LionWebChunkDefinitions.js +0 -110
  98. package/dist/validators/LionWebChunkDefinitions.js.map +0 -1
  99. package/dist/validators/LionWebLanguageReferenceValidator.d.ts +0 -21
  100. package/dist/validators/LionWebLanguageReferenceValidator.d.ts.map +0 -1
  101. package/dist/validators/LionWebLanguageReferenceValidator.js +0 -166
  102. package/dist/validators/LionWebLanguageReferenceValidator.js.map +0 -1
  103. package/dist/validators/LionWebLanguageValidator.d.ts +0 -23
  104. package/dist/validators/LionWebLanguageValidator.d.ts.map +0 -1
  105. package/dist/validators/LionWebLanguageValidator.js +0 -66
  106. package/dist/validators/LionWebLanguageValidator.js.map +0 -1
  107. package/dist/validators/LionWebReferenceValidator.d.ts +0 -43
  108. package/dist/validators/LionWebReferenceValidator.d.ts.map +0 -1
  109. package/dist/validators/LionWebReferenceValidator.js +0 -203
  110. package/dist/validators/LionWebReferenceValidator.js.map +0 -1
  111. package/dist/validators/LionWebSyntaxValidator.d.ts +0 -9
  112. package/dist/validators/LionWebSyntaxValidator.d.ts.map +0 -1
  113. package/dist/validators/LionWebSyntaxValidator.js +0 -11
  114. package/dist/validators/LionWebSyntaxValidator.js.map +0 -1
  115. package/dist/validators/LionWebValidator.d.ts +0 -24
  116. package/dist/validators/LionWebValidator.d.ts.map +0 -1
  117. package/dist/validators/LionWebValidator.js +0 -55
  118. package/dist/validators/LionWebValidator.js.map +0 -1
  119. package/dist/validators/ValidationFunctions.d.ts +0 -60
  120. package/dist/validators/ValidationFunctions.d.ts.map +0 -1
  121. package/dist/validators/ValidationFunctions.js +0 -110
  122. package/dist/validators/ValidationFunctions.js.map +0 -1
  123. package/dist/validators/generic/SyntaxValidator.d.ts +0 -37
  124. package/dist/validators/generic/SyntaxValidator.d.ts.map +0 -1
  125. package/dist/validators/generic/SyntaxValidator.js +0 -157
  126. package/dist/validators/generic/SyntaxValidator.js.map +0 -1
  127. package/dist/validators/generic/ValidationResult.d.ts +0 -8
  128. package/dist/validators/generic/ValidationResult.d.ts.map +0 -1
  129. package/dist/validators/generic/ValidationResult.js +0 -15
  130. package/dist/validators/generic/ValidationResult.js.map +0 -1
  131. package/dist/validators/generic/ValidationTypes.d.ts +0 -68
  132. package/dist/validators/generic/ValidationTypes.d.ts.map +0 -1
  133. package/dist/validators/generic/ValidationTypes.js +0 -46
  134. package/dist/validators/generic/ValidationTypes.js.map +0 -1
  135. package/dist/validators/generic/index.d.ts +0 -4
  136. package/dist/validators/generic/index.d.ts.map +0 -1
  137. package/dist/validators/generic/index.js +0 -4
  138. package/dist/validators/generic/index.js.map +0 -1
  139. package/dist/validators/index.d.ts +0 -9
  140. package/dist/validators/index.d.ts.map +0 -1
  141. package/dist/validators/index.js +0 -9
  142. package/dist/validators/index.js.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lionweb/validation",
3
- "version": "0.7.0-beta.0",
3
+ "version": "0.7.0-beta.10",
4
4
  "license": "Apache 2.0",
5
5
  "description": "LionWeb Serialization validation",
6
6
  "author": "jos.warmer@openmodeling.nl",
@@ -14,14 +14,15 @@
14
14
  "testing"
15
15
  ],
16
16
  "scripts": {
17
- "clean": "rm -rf dist/ node_modules/ lionweb-validation-*.tgz",
17
+ "clean": "npx rimraf dist node_modules -g lionweb-validation-*.tgz",
18
18
  "build": "tsc && npm run copyM3 && npm run copyBuiltins",
19
19
  "copyM3": "node -e \"const fs = require('fs'); fs.copyFileSync('./src/languages/LionCore-M3.json', './dist/languages/LionCore-M3.json');\"",
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 --registry https://registry.npmjs.org",
23
+ "release-alpha": "npm publish --registry https://registry.npmjs.org --tag alpha",
24
24
  "release-beta": "npm publish --registry https://registry.npmjs.org --tag beta",
25
+ "release": "npm publish --registry https://registry.npmjs.org",
25
26
  "testValid": "node dist/runners/RunCheckFolder.js ../test/testset/valid",
26
27
  "testInvalid": "node dist/runners/RunCheckFolder.js ../test/testset/invalid",
27
28
  "testInvalidWithLanguage": "node dist/runners/RunCheckFolderWithLanguage.js ../test/testset/withLanguage/invalid/ ../test/testset/withLanguage/myLang.language.json",
@@ -31,8 +32,8 @@
31
32
  "republish-local": "yarn unpublish-local && yarn publish-local"
32
33
  },
33
34
  "dependencies": {
34
- "@lionweb/json": "0.7.0-beta.0",
35
- "@lionweb/ts-utils": "0.7.0-beta.0",
36
- "@lionweb/json-utils": "0.7.0-beta.0"
35
+ "@lionweb/json": "0.7.0-beta.10",
36
+ "@lionweb/ts-utils": "0.7.0-beta.10",
37
+ "@lionweb/json-utils": "0.7.0-beta.10"
37
38
  }
38
39
  }
@@ -2,16 +2,16 @@ import { JsonContext } from "@lionweb/json-utils"
2
2
 
3
3
  export abstract class ValidationIssue {
4
4
  abstract readonly issueType: string
5
- context: JsonContext
5
+ context: JsonContext | null
6
6
 
7
- constructor(context: JsonContext) {
7
+ constructor(context: JsonContext | null) {
8
8
  this.context = context
9
9
  }
10
10
 
11
11
  protected abstract msg(): string
12
12
 
13
13
  public errorMsg(): string {
14
- return `${this.issueType}: ${this.msg()} at ${this.context.toString()} `
14
+ return `${this.issueType}: ${this.msg()} at ${this.context?.toString()} `
15
15
  }
16
16
  }
17
17
 
@@ -19,7 +19,7 @@ export class GenericIssue extends ValidationIssue {
19
19
  readonly issueType = "GenericIssue"
20
20
 
21
21
  constructor(
22
- context: JsonContext,
22
+ context: JsonContext | null,
23
23
  public text: string
24
24
  ) {
25
25
  super(context)
@@ -48,7 +48,7 @@ export function printIssues(result: ValidationResult, file?: string): void {
48
48
  }
49
49
 
50
50
  export function issuestoString(vresult: ValidationResult, file?: string): string {
51
- let result = ""
52
- vresult.issues.forEach(issue => (result += (file == undefined ? "" : `File ${file}: `) + issue.errorMsg() + "\n"))
51
+ let result = "ISSUES: "
52
+ vresult.issues.forEach(issue => (result += (file === undefined ? "NOFILE" : `File ${file}: `) + issue.errorMsg() + "\n"))
53
53
  return result
54
54
  }
@@ -1,112 +1,102 @@
1
- import { MAY_BE_NULL, PrimitiveDef, PropertyDef, TypeDefinition } from "./generic/ValidationTypes.js"
1
+ import { DefinitionSchema, MAY_BE_NULL, PropertyDef, PrimitiveDef } from "./generic/index.js"
2
2
  import { validateId, validateKey, validateSerializationFormatVersion, validateVersion } from "./ValidationFunctions.js"
3
3
 
4
4
  /**
5
5
  * The structure below defines the structure of a LionWeb Chunk by defining all the properties.
6
6
  * It can
7
- * - be fed to the SyntaxValidator to validate an object sat runtime.
8
- * - used to generate all the types for a LionWebChunk.
7
+ * - be used by the SyntaxValidator to validate an object sat runtime.
8
+ * - used to generate all the TypeScript types for a LionWebChunk.
9
9
  */
10
- export const expectedTypes: Map<string, TypeDefinition> = new Map<string, TypeDefinition>([
11
- [
12
- "LionWebMetaPointer",
13
- [
14
- PropertyDef({ property: "key", expectedType: "LionWebKey" }),
15
- PropertyDef({ property: "version", expectedType: "LionWebVersion" }),
16
- PropertyDef({ property: "language", expectedType: "LionWebKey" })
10
+ export const LionWebSchema: DefinitionSchema = new DefinitionSchema([
11
+ {
12
+ name: "LionWebJsonMetaPointer",
13
+ properties: [
14
+ PropertyDef({ name: "key", type: "LionWebKey" }),
15
+ PropertyDef({ name: "version", type: "LionWebVersion" }),
16
+ PropertyDef({ name: "language", type: "LionWebKey" })
17
17
  ]
18
- ],
19
- [
20
- "ResponseMessage",
21
- [
22
- PropertyDef({ property: "kind", expectedType: "string" }),
23
- PropertyDef({ property: "message", expectedType: "string" }),
24
- PropertyDef({ property: "data", expectedType: "object", mayBeNull: true, isOptional: true })
18
+ },
19
+ {
20
+ name: "LionWebJsonMetaPointer",
21
+ properties: [
22
+ PropertyDef({ name: "key", type: "LionWebKey" }),
23
+ PropertyDef({ name: "version", type: "LionWebVersion" }),
24
+ PropertyDef({ name: "language", type: "LionWebKey" }),
25
25
  ]
26
- ],
27
- [
28
- "LionWebChunk",
29
- [
30
- PropertyDef({ property: "serializationFormatVersion", expectedType: "LionWebSerializationFormatVersion" }),
31
- PropertyDef({ property: "languages", expectedType: "LionWebUsedLanguage", isList: true }),
32
- PropertyDef({ property: "nodes", expectedType: "LionWebNode", isList: true })
26
+ },
27
+ {
28
+ name: "ResponseMessage",
29
+ properties: [
30
+ PropertyDef({ name: "kind", type: "JSstring" }),
31
+ PropertyDef({ name: "message", type: "JSstring" }),
32
+ PropertyDef({ name: "data", type: "JSobject", mayBeNull: true, isOptional: true })
33
33
  ]
34
- ],
35
- [
36
- "LionWebUsedLanguage",
37
- [
38
- PropertyDef({ property: "key", expectedType: "LionWebKey" }),
39
- PropertyDef({ property: "version", expectedType: "LionWebVersion" })
34
+ },
35
+ {
36
+ name: "LionWebJsonChunk",
37
+ properties: [
38
+ PropertyDef({ name: "serializationFormatVersion", type: "LionWebSerializationFormatVersion" }),
39
+ PropertyDef({ name: "languages", type: "LionWebJsonUsedLanguage", isList: true }),
40
+ PropertyDef({ name: "nodes", type: "LionWebJsonNode", isList: true })
40
41
  ]
41
- ],
42
- [
43
- "LionWebNode",
44
- [
45
- PropertyDef({ property: "id", expectedType: "LionWebId" }),
46
- PropertyDef({ property: "classifier", expectedType: "LionWebMetaPointer" }),
47
- PropertyDef({ property: "properties", expectedType: "LionWebProperty", isList: true }),
48
- PropertyDef({ property: "containments", expectedType: "LionWebContainment", isList: true }),
49
- PropertyDef({ property: "references", expectedType: "LionWebReference", isList: true }),
50
- PropertyDef({ property: "annotations", expectedType: "LionWebId", isList: true }),
51
- PropertyDef({ property: "parent", expectedType: "LionWebId", mayBeNull: MAY_BE_NULL }),
42
+ },
43
+ {
44
+ name: "LionWebJsonUsedLanguage",
45
+ properties: [
46
+ PropertyDef({ name: "key", type: "LionWebKey" }),
47
+ PropertyDef({ name: "version", type: "LionWebVersion" })
52
48
  ]
53
- ],
54
- [
55
- "LionWebProperty",
56
- [
57
- PropertyDef({ property: "property", expectedType: "LionWebMetaPointer" }),
58
- PropertyDef({ property: "value", expectedType: "string", mayBeNull: MAY_BE_NULL }),
49
+ },
50
+ {
51
+ name: "LionWebJsonNode",
52
+ properties: [
53
+ PropertyDef({ name: "id", type: "LionWebId" }),
54
+ PropertyDef({ name: "classifier", type: "LionWebJsonMetaPointer" }),
55
+ PropertyDef({ name: "properties", type: "LionWebJsonProperty", isList: true }),
56
+ PropertyDef({ name: "containments", type: "LionWebJsonContainment", isList: true }),
57
+ PropertyDef({ name: "references", type: "LionWebJsonReference", isList: true }),
58
+ PropertyDef({ name: "annotations", type: "LionWebId", isList: true }),
59
+ PropertyDef({ name: "parent", type: "LionWebId", mayBeNull: MAY_BE_NULL }),
59
60
  ]
60
- ],
61
- [
62
- "LionWebContainment",
63
- [
64
- PropertyDef({ property: "containment", expectedType: "LionWebMetaPointer" }),
65
- PropertyDef({ property: "children", expectedType: "LionWebId", isList: true }),
61
+ },
62
+ {
63
+ name: "LionWebJsonProperty",
64
+ properties: [
65
+ PropertyDef({ name: "property", type: "LionWebJsonMetaPointer" }),
66
+ PropertyDef({ name: "value", type: "JSstring", mayBeNull: MAY_BE_NULL }),
66
67
  ]
67
- ],
68
- [
69
- "LionWebReference",
70
- [
71
- PropertyDef({ property: "reference", expectedType: "LionWebMetaPointer"}),
72
- PropertyDef({ property: "targets", expectedType: "LionWebReferenceTarget", isList: true}),
68
+ },
69
+ {
70
+ name: "LionWebJsonContainment",
71
+ properties: [
72
+ PropertyDef({ name: "containment", type: "LionWebJsonMetaPointer" }),
73
+ PropertyDef({ name: "children", type: "LionWebId", isList: true }),
73
74
  ]
74
- ],
75
- [
76
- "LionWebReferenceTarget",
77
- [
78
- PropertyDef({ property: "resolveInfo", expectedType: "string", mayBeNull: MAY_BE_NULL }),
79
- PropertyDef({ property: "reference", expectedType: "LionWebId", mayBeNull: MAY_BE_NULL }),
75
+ },
76
+ {
77
+ name: "LionWebJsonReference",
78
+ properties: [
79
+ PropertyDef({ name: "reference", type: "LionWebJsonMetaPointer"}),
80
+ PropertyDef({ name: "targets", type: "LionWebJsonReferenceTarget", isList: true}),
80
81
  ]
81
- ],
82
+ },
83
+ {
84
+ name: "LionWebJsonReferenceTarget",
85
+ properties: [
86
+ PropertyDef({ name: "resolveInfo", type: "JSstring", mayBeNull: MAY_BE_NULL }),
87
+ PropertyDef({ name: "reference", type: "LionWebId", mayBeNull: MAY_BE_NULL }),
88
+ ]
89
+ },
82
90
  /**
83
91
  * Elements without properties are assumed to be JSON/JS primitive values, and tested using `typeof`
84
92
  * and the (optional) validate function.
85
93
  */
86
- [
87
- "LionWebId",
88
- PrimitiveDef({ primitiveType: "string", validate: validateId }),
89
- ],
90
- [
91
- "LionWebKey",
92
- PrimitiveDef({ primitiveType: "string", validate: validateKey }),
93
- ],
94
- [
95
- "LionWebVersion",
96
- PrimitiveDef({ primitiveType: "string", validate: validateVersion }),
97
- ],
98
- [
99
- "LionWebSerializationFormatVersion",
100
- PrimitiveDef({ primitiveType: "string", validate: validateSerializationFormatVersion }),
101
- ],
102
- [
103
- "string",
104
- PrimitiveDef({ primitiveType: "string" }),
105
- ],
106
- [
107
- "object",
108
- PrimitiveDef({ primitiveType: "object" }),
109
- ]
94
+ PrimitiveDef({ name: "LionWebId", primitiveType: "string", validate: validateId }),
95
+ PrimitiveDef({ name: "LionWebKey", primitiveType: "string", validate: validateKey }),
96
+ PrimitiveDef({ name: "LionWebVersion",primitiveType: "string", validate: validateVersion }),
97
+ PrimitiveDef({ name: "LionWebSerializationFormatVersion",primitiveType: "string", validate: validateSerializationFormatVersion }),
98
+ PrimitiveDef({ name: "JSstring", primitiveType: "string" }),
99
+ PrimitiveDef({ name: "JSobject",primitiveType: "object" }),
110
100
  ])
111
101
 
112
102
 
@@ -1,6 +1,6 @@
1
1
  import { SyntaxValidator } from "./generic/SyntaxValidator.js"
2
2
  import { ValidationResult } from "./generic/ValidationResult.js"
3
- import { expectedTypes } from "./LionWebChunkDefinitions.js"
3
+ import { LionWebSchema } from "./LionWebChunkDefinitions.js"
4
4
 
5
5
  /**
6
6
  * LionWebSyntaxValidator can check whether objects are structurally LionWeb objects.
@@ -8,7 +8,7 @@ import { expectedTypes } from "./LionWebChunkDefinitions.js"
8
8
  export class LionWebSyntaxValidator extends SyntaxValidator {
9
9
 
10
10
  constructor(validationResult: ValidationResult) {
11
- super(validationResult, expectedTypes)
11
+ super(validationResult, LionWebSchema)
12
12
  }
13
13
  }
14
14
 
@@ -34,7 +34,7 @@ export class LionWebValidator {
34
34
  }
35
35
 
36
36
  validateSyntax() {
37
- this.syntaxValidator.validate(this.object, "LionWebChunk")
37
+ this.syntaxValidator.validate(this.object, "LionWebJsonChunk")
38
38
  this.syntaxCorrect = !this.validationResult.hasErrors()
39
39
  if (this.syntaxCorrect) {
40
40
  this.chunk = new LionWebJsonChunkWrapper(this.object as LionWebJsonChunk)
@@ -12,7 +12,7 @@ import {
12
12
  Syntax_VersionFormat_Issue
13
13
  } from "../issues/SyntaxIssues.js"
14
14
  import { ValidationResult } from "./generic/ValidationResult.js"
15
- import { PropertyDefinition } from "./generic/ValidationTypes.js"
15
+ import { PropertyDefinition } from "./generic/schema/ValidationTypes.js"
16
16
 
17
17
  /**
18
18
  * Check whether `id` is a valid LionWeb id.
@@ -72,7 +72,7 @@ export function validateBoolean<String>(value: String, result: ValidationResult,
72
72
  result.issue(
73
73
  new Language_PropertyValue_Issue(
74
74
  context,
75
- propDef ? propDef.property : "unknown",
75
+ propDef ? propDef.name : "unknown",
76
76
  valueAsPrimitive,
77
77
  "boolean " + JSON.stringify(value)
78
78
  )
@@ -92,7 +92,7 @@ export function validateInteger<String>(value: String, result: ValidationResult,
92
92
  const valueAsPrimitive = "" + value
93
93
  const regexp = /^[+-]?(0|[1-9][0-9]*)$/
94
94
  if (valueAsPrimitive === null || !regexp.test(valueAsPrimitive)) {
95
- result.issue(new Language_PropertyValue_Issue(context, propDef ? propDef.property : "unknown", valueAsPrimitive, "integer"))
95
+ result.issue(new Language_PropertyValue_Issue(context, propDef ? propDef.name : "unknown", valueAsPrimitive, "integer"))
96
96
  }
97
97
  }
98
98
 
@@ -107,12 +107,12 @@ export function validateInteger<String>(value: String, result: ValidationResult,
107
107
  export function validateJSON<String>(value: String, result: ValidationResult, context: JsonContext, propDef?: PropertyDefinition): void {
108
108
  const valueAsPrimitive = "" + value
109
109
  if (value === null) {
110
- result.issue(new Syntax_PropertyNullIssue(context, propDef!.property!))
110
+ result.issue(new Syntax_PropertyNullIssue(context, propDef!.name!))
111
111
  }
112
112
  try {
113
113
  JSON.parse(valueAsPrimitive)
114
114
  } catch (e) {
115
- result.issue(new Language_PropertyValue_Issue(context, propDef ? propDef.property : "unknown", valueAsPrimitive, "JSON"))
115
+ result.issue(new Language_PropertyValue_Issue(context, propDef ? propDef.name : "unknown", valueAsPrimitive, "JSON"))
116
116
  }
117
117
  }
118
118
 
@@ -9,40 +9,38 @@ import {
9
9
  import { ValidationResult } from "./ValidationResult.js"
10
10
  import {
11
11
  isObjectDefinition,
12
- isPrimitiveDefinition,
13
- ObjectDefinition,
14
- PrimitiveDefinition,
15
- TypeDefinition,
12
+ isPrimitiveDefinition, ObjectDefinition, PrimitiveDefinition, DefinitionSchema,
16
13
  UnknownObjectType
17
- } from "./ValidationTypes.js"
14
+ } from "./schema/index.js"
18
15
 
19
16
  /**
20
- * Syntax Validator can check whether objects are structurally conforming to the
21
- * definitions given in `typeDefinitions`.
17
+ * Syntax Validator checks whether objects are structurally conforming to the
18
+ * definitions given in `schema`.
22
19
  */
23
20
  export class SyntaxValidator {
24
21
  validationResult: ValidationResult
25
- typeDefinitions: Map<string, TypeDefinition>
22
+ schema: DefinitionSchema
26
23
 
27
- constructor(validationResult: ValidationResult, expectedTypes: Map<string, TypeDefinition>) {
24
+ constructor(validationResult: ValidationResult, schema: DefinitionSchema) {
28
25
  this.validationResult = validationResult
29
- this.typeDefinitions = expectedTypes
26
+ this.schema = schema
30
27
  }
31
28
 
32
29
  /**
33
30
  * Check whether `obj` is a JSON object that conforms to the definition of `expectedType`.
34
- * All errors found will be pushed into the `errors` array, if its length is not 0, the check has failed.
31
+ * All errors found will be added to the `validationResult` object.
35
32
  * @param obj The object to validate.
36
33
  * @param expectedType The expected type of the object.
37
34
  */
38
35
  validate(obj: unknown, expectedType: string) {
39
36
  const object = obj as UnknownObjectType
40
- const typeDef = this.typeDefinitions.get(expectedType)
37
+ const typeDef = this.schema.getDefinition(expectedType)
38
+
41
39
  if (typeDef === undefined) {
42
40
  throw new Error(`SyntaxValidator.validate: cannot find definition for ${expectedType}`)
43
- } else if (isObjectDefinition(typeDef)){
41
+ } else if (isObjectDefinition(typeDef)) {
44
42
  this.validateObjectProperties(expectedType, typeDef, object, new JsonContext(null, ["$"]))
45
- } else if( isPrimitiveDefinition(typeDef)) {
43
+ } else if (isPrimitiveDefinition(typeDef)) {
46
44
  this.validatePrimitiveValue("$", typeDef, object, new JsonContext(null, ["$"]))
47
45
  }
48
46
  }
@@ -50,7 +48,7 @@ export class SyntaxValidator {
50
48
  /**
51
49
  * Validate whether `object` is structured conform the properties in `propertyDef`
52
50
  * @param originalProperty The property of which `object` it the value
53
- * @param typeDef The property definitions that are being validated
51
+ * @param typeDef The property definitions that are being validated
54
52
  * @param object The object being validated
55
53
  * @param jsonContext The location in the JSON
56
54
  * @private
@@ -59,99 +57,147 @@ export class SyntaxValidator {
59
57
  if (typeDef === null || typeDef === undefined) {
60
58
  return
61
59
  }
62
- if ((typeof object) !== "object") {
63
- this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, originalProperty, "object", typeof object))
64
- return
65
- }
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
60
+ if (typeof object !== "object") {
61
+ this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, originalProperty, "object", typeof object))
62
+ return
63
+ }
64
+ for (const propertyDef of typeDef.properties) {
65
+ // const taggedUnion = this.schema.getTaggedUnionDefinition(propertyDef.type)
66
+ // if (taggedUnion !== undefined) {
67
+ // this.validateTaggedUnion(propertyDef, typeDef, object, jsonContext.concat(propertyDef.name))
68
+ // continue
69
+ // }
70
+ const expectedTypeDef = this.schema.getDefinition(propertyDef.type)
71
+ const validator = propertyDef.validate!
72
+ const propertyValue = object[propertyDef.name]
73
+ if (propertyValue === undefined) {
74
+ if (!propertyDef.isOptional) {
75
+ this.validationResult.issue(new Syntax_PropertyMissingIssue(jsonContext, propertyDef.name))
79
76
  }
80
- if (propertyDef.mayBeNull && propertyValue === null) {
81
- // Ok, stop checking, continue with next property def
82
- continue
77
+ continue
78
+ }
79
+ if (!propertyDef.mayBeNull && propertyValue === null) {
80
+ this.validationResult.issue(new Syntax_PropertyNullIssue(jsonContext, propertyDef.name))
81
+ continue
82
+ }
83
+ if (propertyDef.mayBeNull && propertyValue === null) {
84
+ // Ok, stop checking, continue with next property def
85
+ continue
86
+ }
87
+ if (propertyDef.isList) {
88
+ // Check whether value is an array
89
+ if (!Array.isArray(propertyValue)) {
90
+ const newContext = jsonContext.concat(propertyDef.name)
91
+ this.validationResult.issue(new Syntax_PropertyTypeIssue(newContext, propertyDef.name, "array", typeof propertyValue))
92
+ return
83
93
  }
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)
94
+ // If an array, validate every item in the array
95
+ (propertyValue as UnknownObjectType[]).forEach((item, index) => {
96
+ const newContext = jsonContext.concat(propertyDef.name, index)
97
+ if (item === null) {
98
+ this.validationResult.issue(new Syntax_ArrayContainsNull_Issue(newContext, propertyDef.name, index))
99
+ } else {
100
+ if (expectedTypeDef !== undefined) {
101
+ if (isPrimitiveDefinition(expectedTypeDef)) {
102
+ if (this.validatePrimitiveValue(propertyDef.name, expectedTypeDef, item, jsonContext)) {
106
103
  validator.apply(null, [item, this.validationResult, newContext, propertyDef])
107
104
  }
108
105
  } else {
109
- throw new Error(`Expected type '${propertyDef.expectedType} has neither property defs, nor a validator.`)
106
+ // propertyValue should be an object, validate its properties
107
+ this.validateObjectProperties(propertyDef.name, expectedTypeDef, item as UnknownObjectType, newContext)
108
+ validator.apply(null, [item, this.validationResult, newContext, propertyDef])
110
109
  }
110
+ } else {
111
+ throw new Error(`Expected type '${propertyDef.type} has neither property defs, nor a validator.`)
111
112
  }
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
113
  }
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)
114
+ })
115
+ } else {
116
+ const newContext = jsonContext.concat(propertyDef.name)
117
+ if (Array.isArray(propertyValue)) {
118
+ this.validationResult.issue(new Syntax_PropertyTypeIssue(newContext, propertyDef.name, propertyDef.type, "array"))
119
+ return
120
+ }
121
+ // Single valued property, validate it
122
+ if (expectedTypeDef !== undefined) {
123
+ if (isPrimitiveDefinition(expectedTypeDef)) {
124
+ // propertyValue should be a primitive as it has no property definitions
125
+ if (this.validatePrimitiveValue(propertyDef.name, expectedTypeDef, propertyValue, jsonContext)) {
129
126
  validator.apply(null, [propertyValue, this.validationResult, newContext, propertyDef])
130
- } else {
131
- throw new Error("EXPECTING ObjectDefinition or PrimitiveDefinition, but got something else")
132
127
  }
128
+ } else if (isObjectDefinition(expectedTypeDef)) {
129
+ // propertyValue should be an object, validate its properties
130
+ this.validateObjectProperties(propertyDef.name, expectedTypeDef, propertyValue as UnknownObjectType, newContext)
131
+ validator.apply(null, [propertyValue, this.validationResult, newContext, propertyDef])
133
132
  } else {
134
- throw new Error(`Expected single type '${propertyDef.expectedType}' for '${propertyDef.property}' at ${newContext.toString()} has neither property defs, nor a validator.`)
133
+ throw new Error("EXPECTING ObjectDefinition or PrimitiveDefinition, but got something else")
135
134
  }
135
+ } else {
136
+ throw new Error(
137
+ `Expected single type '${propertyDef.type}' for '${propertyDef.name}' at ${newContext.toString()} has neither property defs, nor a validator.`
138
+ )
136
139
  }
137
140
  }
138
- this.checkStrayProperties(object, typeDef, jsonContext)
141
+ }
142
+ this.checkStrayProperties(object, typeDef, jsonContext)
139
143
  }
140
-
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
- // }
146
144
 
145
+ validatePrimitiveValue(propertyName: string, propDef: PrimitiveDefinition, object: unknown, jsonContext: JsonContext): boolean {
147
146
  if (typeof object !== propDef.primitiveType) {
148
- this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, propertyName, propDef.primitiveType,typeof object))
147
+ this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, propertyName, propDef.primitiveType, typeof object))
149
148
  return false
150
149
  }
151
150
  propDef.validate!(object, this.validationResult, jsonContext)
152
151
  return true
153
152
  }
154
153
 
154
+ /**
155
+ *
156
+ * @param propertyDef the definition of the property that has the TaggedUnionType as its type
157
+ * @param typeDef The object definition containing the `propertyDef`
158
+ * @param object The object of type `typeDef` that should have the property described by `propertyDef`
159
+ * @param jsonContext
160
+ */
161
+ // validateTaggedUnion(
162
+ // propertyDef: PropertyDefinition,
163
+ // typeDef: ObjectDefinition,
164
+ // object: UnknownObjectType,
165
+ // jsonContext: JsonContext
166
+ // ): void {
167
+ // // console.log(`validateTaggedUnion ${JSON.stringify(propertyDef)}, typedef ${typeDef.name} object: ${JSON.stringify(object, null, 3)}`)
168
+ // const taggedObject = object[propertyDef.name]
169
+ // if (propertyDef.isList) {
170
+ // if (!Array.isArray(taggedObject)) {
171
+ // this.validationResult.issue(new GenericIssue(jsonContext, `Property value '${propertyDef.name}' expects an array, found '${typeof taggedObject}'`))
172
+ // } else {
173
+ // taggedObject.forEach((taggedObjectSingle, index) => {
174
+ // this.validateTaggedObject(taggedObjectSingle, jsonContext.concat(index))
175
+ // })
176
+ // }
177
+ // } else {
178
+ // if (!(typeof taggedObject === "object")) {
179
+ // this.validationResult.issue(new GenericIssue(jsonContext, `Property value '${propertyDef.name}' expects an object, found '${typeof taggedObject}'`))
180
+ // } else {
181
+ // this.validateTaggedObject(taggedObject as UnknownObjectType, jsonContext)
182
+ // }
183
+ // }
184
+ // }
185
+
186
+ /**
187
+ * Validate a single tagged object
188
+ * @param taggedObject
189
+ * @param jsonContext
190
+ */
191
+ // validateTaggedObject(taggedObject: UnknownObjectType, jsonContext: JsonContext): void {
192
+ // const actualTypeName = taggedObject["messageKind"] as string
193
+ // const actualType = this.schema.getDefinition(actualTypeName)
194
+ // if (actualType === undefined || !isObjectDefinition(actualType)) {
195
+ // this.validationResult.issue(new GenericIssue(jsonContext, `Expected object type is ${typeof taggedObject}, should be object`))
196
+ // } else {
197
+ // this.validateObjectProperties(actualTypeName, actualType, taggedObject, jsonContext)
198
+ // }
199
+ // }
200
+
155
201
  /**
156
202
  * Check whether there are extra properties that should not be there.
157
203
  * @param obj Object to be validated
@@ -160,8 +206,8 @@ export class SyntaxValidator {
160
206
  */
161
207
  checkStrayProperties(obj: UnknownObjectType, def: ObjectDefinition, context: JsonContext) {
162
208
  const own = Object.getOwnPropertyNames(obj)
163
- const defined = def.map(pdef => pdef.property)
164
- own.forEach((ownProp) => {
209
+ const defined = def.properties.map(pdef => pdef.name)
210
+ own.forEach(ownProp => {
165
211
  if (!defined.includes(ownProp)) {
166
212
  this.validationResult.issue(new Syntax_PropertyUnknownIssue(context, ownProp))
167
213
  }