@twin.org/data-core 0.0.2-next.4 → 0.0.3-next.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 (38) hide show
  1. package/dist/es/factories/dataTypeHandlerFactory.js +9 -0
  2. package/dist/es/factories/dataTypeHandlerFactory.js.map +1 -0
  3. package/dist/es/factories/identifierHandlerFactory.js +19 -0
  4. package/dist/es/factories/identifierHandlerFactory.js.map +1 -0
  5. package/dist/es/index.js +13 -0
  6. package/dist/es/index.js.map +1 -0
  7. package/dist/es/models/IDataTypeHandler.js +2 -0
  8. package/dist/es/models/IDataTypeHandler.js.map +1 -0
  9. package/dist/es/models/IIdentifierHandler.js +2 -0
  10. package/dist/es/models/IIdentifierHandler.js.map +1 -0
  11. package/dist/es/models/IJsonSchema.js +2 -0
  12. package/dist/es/models/IJsonSchema.js.map +1 -0
  13. package/dist/es/models/ISchemaValidationError.js +2 -0
  14. package/dist/es/models/ISchemaValidationError.js.map +1 -0
  15. package/dist/es/models/ISchemaValidationResult.js +2 -0
  16. package/dist/es/models/ISchemaValidationResult.js.map +1 -0
  17. package/dist/es/models/validationMode.js +25 -0
  18. package/dist/es/models/validationMode.js.map +1 -0
  19. package/dist/es/utils/dataTypeHelper.js +115 -0
  20. package/dist/es/utils/dataTypeHelper.js.map +1 -0
  21. package/dist/es/utils/jsonSchemaHelper.js +161 -0
  22. package/dist/es/utils/jsonSchemaHelper.js.map +1 -0
  23. package/dist/types/factories/dataTypeHandlerFactory.d.ts +1 -1
  24. package/dist/types/factories/identifierHandlerFactory.d.ts +1 -1
  25. package/dist/types/index.d.ts +10 -10
  26. package/dist/types/models/IDataTypeHandler.d.ts +7 -3
  27. package/dist/types/models/IJsonSchema.d.ts +2 -2
  28. package/dist/types/models/ISchemaValidationResult.d.ts +1 -1
  29. package/dist/types/utils/dataTypeHelper.d.ts +26 -1
  30. package/dist/types/utils/jsonSchemaHelper.d.ts +7 -3
  31. package/docs/changelog.md +87 -0
  32. package/docs/reference/classes/DataTypeHelper.md +93 -1
  33. package/docs/reference/classes/JsonSchemaHelper.md +12 -4
  34. package/docs/reference/interfaces/IDataTypeHandler.md +13 -5
  35. package/docs/reference/type-aliases/IJsonSchema.md +1 -1
  36. package/package.json +5 -7
  37. package/dist/cjs/index.cjs +0 -282
  38. package/dist/esm/index.mjs +0 -276
package/docs/changelog.md CHANGED
@@ -1,5 +1,92 @@
1
1
  # @twin.org/data-core - Changelog
2
2
 
3
+ ## [0.0.3-next.10](https://github.com/twinfoundation/data/compare/data-core-v0.0.3-next.9...data-core-v0.0.3-next.10) (2026-02-24)
4
+
5
+
6
+ ### Miscellaneous Chores
7
+
8
+ * **data-core:** Synchronize repo versions
9
+
10
+ ## [0.0.3-next.9](https://github.com/twinfoundation/data/compare/data-core-v0.0.3-next.8...data-core-v0.0.3-next.9) (2026-02-23)
11
+
12
+
13
+ ### Miscellaneous Chores
14
+
15
+ * **data-core:** Synchronize repo versions
16
+
17
+ ## [0.0.3-next.8](https://github.com/twinfoundation/data/compare/data-core-v0.0.3-next.7...data-core-v0.0.3-next.8) (2026-02-02)
18
+
19
+
20
+ ### Miscellaneous Chores
21
+
22
+ * **data-core:** Synchronize repo versions
23
+
24
+ ## [0.0.3-next.7](https://github.com/twinfoundation/data/compare/data-core-v0.0.3-next.6...data-core-v0.0.3-next.7) (2026-01-21)
25
+
26
+
27
+ ### Miscellaneous Chores
28
+
29
+ * **data-core:** Synchronize repo versions
30
+
31
+ ## [0.0.3-next.6](https://github.com/twinfoundation/data/compare/data-core-v0.0.3-next.5...data-core-v0.0.3-next.6) (2026-01-14)
32
+
33
+
34
+ ### Bug Fixes
35
+
36
+ * getSchemaForType async ([a26a4f0](https://github.com/twinfoundation/data/commit/a26a4f09d6e22ee0882597b71a134db3079d72d0))
37
+
38
+ ## [0.0.3-next.5](https://github.com/twinfoundation/data/compare/data-core-v0.0.3-next.4...data-core-v0.0.3-next.5) (2026-01-14)
39
+
40
+
41
+ ### Features
42
+
43
+ * improve data type registration ([#34](https://github.com/twinfoundation/data/issues/34)) ([855d110](https://github.com/twinfoundation/data/commit/855d11046a4d85317b77a5c4e0f4a7b1b6d1a767))
44
+
45
+ ## [0.0.3-next.4](https://github.com/twinfoundation/data/compare/data-core-v0.0.3-next.3...data-core-v0.0.3-next.4) (2026-01-06)
46
+
47
+
48
+ ### Features
49
+
50
+ * support JSON Schema 2019 ([#31](https://github.com/twinfoundation/data/issues/31)) ([f798f72](https://github.com/twinfoundation/data/commit/f798f721c998cf50b8ba2318bec574069aad02ae))
51
+
52
+ ## [0.0.3-next.3](https://github.com/twinfoundation/data/compare/data-core-v0.0.3-next.2...data-core-v0.0.3-next.3) (2026-01-05)
53
+
54
+
55
+ ### Miscellaneous Chores
56
+
57
+ * **data-core:** Synchronize repo versions
58
+
59
+ ## [0.0.3-next.2](https://github.com/twinfoundation/data/compare/data-core-v0.0.3-next.1...data-core-v0.0.3-next.2) (2025-11-24)
60
+
61
+
62
+ ### Miscellaneous Chores
63
+
64
+ * **data-core:** Synchronize repo versions
65
+
66
+ ## [0.0.3-next.1](https://github.com/twinfoundation/data/compare/data-core-v0.0.3-next.0...data-core-v0.0.3-next.1) (2025-11-10)
67
+
68
+
69
+ ### Features
70
+
71
+ * add context id features ([#25](https://github.com/twinfoundation/data/issues/25)) ([6592f2e](https://github.com/twinfoundation/data/commit/6592f2e4e59021cc42a079a4f46242758a54313d))
72
+ * add document cache access methods ([dbf1e36](https://github.com/twinfoundation/data/commit/dbf1e36d176c5f428f8c52628fb5a1ff7a6a174a))
73
+ * add fail on missing type option and both mode ([e8b9702](https://github.com/twinfoundation/data/commit/e8b97029a04b646497ff0e55b9610291e58ae92a))
74
+ * add validate-locales ([cf9b761](https://github.com/twinfoundation/data/commit/cf9b76160820fe0b13b4fe56ed241c1d5511b7c1))
75
+ * eslint migration to flat config ([b0db6e6](https://github.com/twinfoundation/data/commit/b0db6e69a90046fc60d29e4273fcdfee13c16088))
76
+ * expand Json LD Keyword ([70632d1](https://github.com/twinfoundation/data/commit/70632d1e11ad85cf3c57e118476b125a673f1681))
77
+ * update framework core ([c077b8c](https://github.com/twinfoundation/data/commit/c077b8c07e7ee66b5482254eab6f2a52cd911270))
78
+ * use fully qualified names for data type lookups ([b7b5c74](https://github.com/twinfoundation/data/commit/b7b5c746b0180a87baa976f6a7a76cedd53d8ff7))
79
+ * use shared store mechanism ([#3](https://github.com/twinfoundation/data/issues/3)) ([33eb221](https://github.com/twinfoundation/data/commit/33eb221ccec2b4a79549c06e9a04225009b93a46))
80
+ * use updated Is.function ([46a4715](https://github.com/twinfoundation/data/commit/46a4715f995aea34f2011138662fe003c9727d07))
81
+ * use updated JSON schema specs ([465223a](https://github.com/twinfoundation/data/commit/465223a9e9c24af546480ef084327a78fa366eaa))
82
+
83
+
84
+ ### Bug Fixes
85
+
86
+ * interface name ([6e49322](https://github.com/twinfoundation/data/commit/6e49322ec1797417220ec9e529bb124f4717f489))
87
+ * remove undici reference ([d77721e](https://github.com/twinfoundation/data/commit/d77721e21d23c7a6750c2f5cac8104851dfaa6d7))
88
+ * tests using context ([577b3bb](https://github.com/twinfoundation/data/commit/577b3bbb661eafbf6d3fd157133c106732e8eb3d))
89
+
3
90
  ## [0.0.2-next.4](https://github.com/twinfoundation/data/compare/data-core-v0.0.2-next.3...data-core-v0.0.2-next.4) (2025-10-09)
4
91
 
5
92
 
@@ -14,6 +14,98 @@ Class to help with data types.
14
14
 
15
15
  ## Methods
16
16
 
17
+ ### registerType()
18
+
19
+ > `static` **registerType**(`namespace`, `type`, `jsonLdContext`, `schema`): `void`
20
+
21
+ Register a data type.
22
+
23
+ #### Parameters
24
+
25
+ ##### namespace
26
+
27
+ `string`
28
+
29
+ The namespace for the type.
30
+
31
+ ##### type
32
+
33
+ `string`
34
+
35
+ The type for the item.
36
+
37
+ ##### jsonLdContext
38
+
39
+ The JSON LD context for the type.
40
+
41
+ `string` | `undefined`
42
+
43
+ ##### schema
44
+
45
+ The JSON schema for the type.
46
+
47
+ `SchemaObject` | `Promise`\<`SchemaObject`\>
48
+
49
+ #### Returns
50
+
51
+ `void`
52
+
53
+ ***
54
+
55
+ ### registerTypes()
56
+
57
+ > `static` **registerTypes**(`namespace`, `jsonLdContext`, `typeDefinition`): `void`
58
+
59
+ Register a list of types.
60
+
61
+ #### Parameters
62
+
63
+ ##### namespace
64
+
65
+ `string`
66
+
67
+ The namespace for the types.
68
+
69
+ ##### jsonLdContext
70
+
71
+ The JSON LD context for the types.
72
+
73
+ `string` | `undefined`
74
+
75
+ ##### typeDefinition
76
+
77
+ `object`[]
78
+
79
+ The type definitions to register.
80
+
81
+ #### Returns
82
+
83
+ `void`
84
+
85
+ ***
86
+
87
+ ### getSchemaForType()
88
+
89
+ > `static` **getSchemaForType**(`dataType`): `Promise`\<`SchemaObject` \| `undefined`\>
90
+
91
+ Get the JSON schema for a data type.
92
+
93
+ #### Parameters
94
+
95
+ ##### dataType
96
+
97
+ `string`
98
+
99
+ The data type to get the schema for.
100
+
101
+ #### Returns
102
+
103
+ `Promise`\<`SchemaObject` \| `undefined`\>
104
+
105
+ The JSON schema for the data type or undefined if not found.
106
+
107
+ ***
108
+
17
109
  ### validate()
18
110
 
19
111
  > `static` **validate**(`propertyName`, `dataType`, `data`, `validationFailures`, `options?`): `Promise`\<`boolean`\>
@@ -32,7 +124,7 @@ The name of the property being validated to use in error messages.
32
124
 
33
125
  The data type to validate.
34
126
 
35
- `undefined` | `string`
127
+ `string` | `undefined`
36
128
 
37
129
  ##### data
38
130
 
@@ -18,7 +18,15 @@ A helper for JSON schemas.
18
18
 
19
19
  > `readonly` `static` **SCHEMA\_VERSION**: `"https://json-schema.org/draft/2020-12/schema"` = `"https://json-schema.org/draft/2020-12/schema"`
20
20
 
21
- The schema version.
21
+ The schema version 2020 (default).
22
+
23
+ ***
24
+
25
+ ### SCHEMA\_VERSION\_2019
26
+
27
+ > `readonly` `static` **SCHEMA\_VERSION\_2019**: `"https://json-schema.org/draft/2019-09/schema"` = `"https://json-schema.org/draft/2019-09/schema"`
28
+
29
+ The schema version 2019.
22
30
 
23
31
  ## Methods
24
32
 
@@ -62,7 +70,7 @@ Result containing errors if there are any.
62
70
 
63
71
  ### getPropertyType()
64
72
 
65
- > `static` **getPropertyType**(`schema`, `propertyName`): `undefined` \| `string`
73
+ > `static` **getPropertyType**(`schema`, `propertyName`): `string` \| `undefined`
66
74
 
67
75
  Get the property type from a schema.
68
76
 
@@ -82,7 +90,7 @@ The name of the property to get the type for.
82
90
 
83
91
  #### Returns
84
92
 
85
- `undefined` \| `string`
93
+ `string` \| `undefined`
86
94
 
87
95
  The types of the property.
88
96
 
@@ -100,7 +108,7 @@ Convert an entity schema to JSON schema e.g https://example.com/schemas/.
100
108
 
101
109
  The entity schema to convert.
102
110
 
103
- `undefined` | `IEntitySchema`\<`unknown`\>
111
+ `IEntitySchema`\<`unknown`\> | `undefined`
104
112
 
105
113
  ##### baseDomain?
106
114
 
@@ -4,11 +4,11 @@ Interface describing a type which can handle a specific data type.
4
4
 
5
5
  ## Properties
6
6
 
7
- ### context
7
+ ### namespace
8
8
 
9
- > **context**: `string`
9
+ > **namespace**: `string`
10
10
 
11
- The context for the type.
11
+ The namespace for the type.
12
12
 
13
13
  ***
14
14
 
@@ -20,6 +20,14 @@ The type for the item.
20
20
 
21
21
  ***
22
22
 
23
+ ### jsonLdContext?
24
+
25
+ > `optional` **jsonLdContext**: `string`
26
+
27
+ The JSON LD context for the type.
28
+
29
+ ***
30
+
23
31
  ### defaultValue?
24
32
 
25
33
  > `optional` **defaultValue**: `unknown`
@@ -30,13 +38,13 @@ The default value for the item to use when constructing a new object.
30
38
 
31
39
  ### jsonSchema()?
32
40
 
33
- > `optional` **jsonSchema**(): `Promise`\<`undefined` \| `SchemaObject`\>
41
+ > `optional` **jsonSchema**(): `Promise`\<`SchemaObject` \| `undefined`\>
34
42
 
35
43
  Get the JSON schema for the data type.
36
44
 
37
45
  #### Returns
38
46
 
39
- `Promise`\<`undefined` \| `SchemaObject`\>
47
+ `Promise`\<`SchemaObject` \| `undefined`\>
40
48
 
41
49
  The JSON schema for the data type.
42
50
 
@@ -1,5 +1,5 @@
1
1
  # Type Alias: IJsonSchema
2
2
 
3
- > **IJsonSchema** = `SchemaObject`
3
+ > **IJsonSchema** = `Ajv.SchemaObject`
4
4
 
5
5
  Default schema type.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/data-core",
3
- "version": "0.0.2-next.4",
3
+ "version": "0.0.3-next.10",
4
4
  "description": "Definitions and helpers for using with data and schemas",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,19 +21,17 @@
21
21
  "ajv": "8.17.1",
22
22
  "ajv-formats": "3.0.1"
23
23
  },
24
- "main": "./dist/cjs/index.cjs",
25
- "module": "./dist/esm/index.mjs",
24
+ "main": "./dist/es/index.js",
26
25
  "types": "./dist/types/index.d.ts",
27
26
  "exports": {
28
27
  ".": {
29
28
  "types": "./dist/types/index.d.ts",
30
- "require": "./dist/cjs/index.cjs",
31
- "import": "./dist/esm/index.mjs"
29
+ "import": "./dist/es/index.js",
30
+ "default": "./dist/es/index.js"
32
31
  }
33
32
  },
34
33
  "files": [
35
- "dist/cjs",
36
- "dist/esm",
34
+ "dist/es",
37
35
  "dist/types",
38
36
  "locales",
39
37
  "docs"
@@ -1,282 +0,0 @@
1
- 'use strict';
2
-
3
- var core = require('@twin.org/core');
4
- var web = require('@twin.org/web');
5
- var Ajv = require('ajv/dist/2020.js');
6
- var addFormats = require('ajv-formats');
7
-
8
- // Copyright 2024 IOTA Stiftung.
9
- // SPDX-License-Identifier: Apache-2.0.
10
- /**
11
- * Factory for creating handlers for data types.
12
- */
13
- // eslint-disable-next-line @typescript-eslint/naming-convention
14
- const DataTypeHandlerFactory = core.Factory.createFactory("data-type");
15
-
16
- // Copyright 2024 IOTA Stiftung.
17
- // SPDX-License-Identifier: Apache-2.0.
18
- /**
19
- * Factory for creating handlers for identifiers.
20
- */
21
- // eslint-disable-next-line @typescript-eslint/naming-convention
22
- const IdentifierHandlerFactory = core.Factory.createFactory("namespace", false, (names, uri) => {
23
- core.Urn.guard("IdentifierHandlerFactory", "uri", uri);
24
- const urn = core.Urn.fromValidString(uri);
25
- const urnParts = urn.parts();
26
- for (let i = urnParts.length - 1; i >= 0; i--) {
27
- const wholeNamespace = urnParts.slice(i).join(":");
28
- if (names.includes(wholeNamespace)) {
29
- return wholeNamespace;
30
- }
31
- }
32
- });
33
-
34
- // Copyright 2024 IOTA Stiftung.
35
- // SPDX-License-Identifier: Apache-2.0.
36
- /**
37
- * Validation modes for validating data types.
38
- */
39
- // eslint-disable-next-line @typescript-eslint/naming-convention
40
- const ValidationMode = {
41
- /**
42
- * Use the validation method of the data type.
43
- */
44
- Validate: "validate",
45
- /**
46
- * Use the JSON Schema methods of the data type.
47
- */
48
- JsonSchema: "json-schema",
49
- /**
50
- * Use either validation mode.
51
- */
52
- Either: "either",
53
- /**
54
- * Use both validation modes.
55
- */
56
- Both: "both"
57
- };
58
-
59
- // Copyright 2024 IOTA Stiftung.
60
- // SPDX-License-Identifier: Apache-2.0.
61
- /**
62
- * A helper for JSON schemas.
63
- */
64
- class JsonSchemaHelper {
65
- /**
66
- * The schema version.
67
- */
68
- static SCHEMA_VERSION = "https://json-schema.org/draft/2020-12/schema";
69
- /**
70
- * The class name.
71
- * @internal
72
- */
73
- static CLASS_NAME = "JsonSchemaHelper";
74
- /**
75
- * Validates data against the schema.
76
- * @param schema The schema to validate the data with.
77
- * @param data The data to be validated.
78
- * @param additionalTypes Additional types to add for reference, not already in DataTypeHandlerFactory.
79
- * @returns Result containing errors if there are any.
80
- */
81
- static async validate(schema, data, additionalTypes) {
82
- const ajv = new Ajv({
83
- allowUnionTypes: true,
84
- // Disable strict tuples as it causes issues with the schema validation when
85
- // you have an array with fixed elements e.g. myType: [string, ...string[]]
86
- // https://github.com/ajv-validator/ajv/issues/1417
87
- strictTuples: false,
88
- loadSchema: async (uri) => {
89
- const subTypeHandler = DataTypeHandlerFactory.getIfExists(uri);
90
- if (core.Is.function(subTypeHandler?.jsonSchema)) {
91
- const subSchema = await subTypeHandler.jsonSchema();
92
- if (core.Is.object(subSchema)) {
93
- return subSchema;
94
- }
95
- }
96
- try {
97
- // We don't have the type in our local data types, so we try to fetch it from the web
98
- return web.FetchHelper.fetchJson(JsonSchemaHelper.CLASS_NAME, uri, web.HttpMethod.GET, undefined, {
99
- // Cache for an hour
100
- cacheTtlMs: 3600000
101
- });
102
- }
103
- catch {
104
- // Failed to load remotely so return an empty object
105
- // so the schema validation doesn't completely fail
106
- return {};
107
- }
108
- }
109
- });
110
- addFormats(ajv);
111
- // Add the additional types provided by the user
112
- if (core.Is.objectValue(additionalTypes)) {
113
- for (const key in additionalTypes) {
114
- ajv.addSchema(additionalTypes[key], key);
115
- }
116
- }
117
- const compiled = await ajv.compileAsync(schema);
118
- const result = await compiled(data);
119
- const output = {
120
- result
121
- };
122
- if (!output.result) {
123
- output.error = compiled.errors;
124
- }
125
- return output;
126
- }
127
- /**
128
- * Get the property type from a schema.
129
- * @param schema The schema to extract the types from.
130
- * @param propertyName The name of the property to get the type for.
131
- * @returns The types of the property.
132
- */
133
- static getPropertyType(schema, propertyName) {
134
- if (schema.type === "object" && core.Is.objectValue(schema.properties)) {
135
- const propertySchema = schema.properties[propertyName];
136
- if (core.Is.object(propertySchema)) {
137
- if (core.Is.stringValue(propertySchema.$ref)) {
138
- return propertySchema.$ref;
139
- }
140
- return propertySchema.type;
141
- }
142
- }
143
- }
144
- /**
145
- * Convert an entity schema to JSON schema e.g https://example.com/schemas/.
146
- * @param entitySchema The entity schema to convert.
147
- * @param baseDomain The base domain for local schemas e.g. https://example.com/
148
- * @returns The JSON schema for the entity.
149
- */
150
- static entitySchemaToJsonSchema(entitySchema, baseDomain) {
151
- let domain = core.StringHelper.trimTrailingSlashes(baseDomain ?? "");
152
- if (domain.length > 0) {
153
- domain += "/";
154
- }
155
- const properties = {};
156
- const required = [];
157
- if (core.Is.arrayValue(entitySchema?.properties)) {
158
- for (const propertySchema of entitySchema.properties) {
159
- const jsonPropertySchema = {
160
- type: propertySchema.type,
161
- description: propertySchema.description,
162
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
163
- examples: propertySchema.examples
164
- };
165
- if (core.Is.stringValue(propertySchema.itemType) && propertySchema.type === "array") {
166
- if (propertySchema.itemType === "object") {
167
- jsonPropertySchema.items = {
168
- $ref: propertySchema.itemTypeRef?.startsWith("http")
169
- ? propertySchema.itemTypeRef
170
- : `${domain}${propertySchema.itemTypeRef}`
171
- };
172
- }
173
- else {
174
- jsonPropertySchema.items = {
175
- type: propertySchema.itemType
176
- };
177
- }
178
- }
179
- else if (propertySchema.type === "object") {
180
- delete jsonPropertySchema.type;
181
- jsonPropertySchema.$ref = propertySchema.itemTypeRef?.startsWith("http")
182
- ? propertySchema.itemTypeRef
183
- : `${domain}${propertySchema.itemTypeRef}`;
184
- }
185
- properties[propertySchema.property] = jsonPropertySchema;
186
- if (!propertySchema.optional) {
187
- required.push(propertySchema.property);
188
- }
189
- }
190
- }
191
- return {
192
- $schema: JsonSchemaHelper.SCHEMA_VERSION,
193
- $id: `${domain}${entitySchema?.type}`,
194
- title: entitySchema?.type,
195
- type: entitySchema ? "object" : "null",
196
- description: entitySchema?.options?.description,
197
- required,
198
- properties,
199
- additionalProperties: false
200
- };
201
- }
202
- }
203
-
204
- // Copyright 2024 IOTA Stiftung.
205
- // SPDX-License-Identifier: Apache-2.0.
206
- /**
207
- * Class to help with data types.
208
- */
209
- class DataTypeHelper {
210
- /**
211
- * Validate a data type.
212
- * @param propertyName The name of the property being validated to use in error messages.
213
- * @param dataType The data type to validate.
214
- * @param data The data to validate.
215
- * @param validationFailures The list of validation failures to add to.
216
- * @param options Optional options for validation.
217
- * @param options.failOnMissingType If true, will fail validation if the data type is missing, defaults to false.
218
- * @param options.validationMode The validation mode to use, defaults to either.
219
- * @returns True if the data was valid.
220
- */
221
- static async validate(propertyName, dataType, data, validationFailures, options) {
222
- let isValid = true;
223
- if (core.Is.stringValue(dataType)) {
224
- const handler = DataTypeHandlerFactory.getIfExists(dataType);
225
- if (handler) {
226
- const validationMode = options?.validationMode ?? ValidationMode.Either;
227
- // If we have a validate function use that as it is more specific
228
- // and will produce better error messages
229
- let hasValidated = false;
230
- if ((validationMode === ValidationMode.Validate ||
231
- validationMode === ValidationMode.Both ||
232
- validationMode === ValidationMode.Either) &&
233
- core.Is.function(handler.validate)) {
234
- isValid = await handler.validate(propertyName, data, validationFailures);
235
- hasValidated = true;
236
- }
237
- if ((validationMode === ValidationMode.JsonSchema ||
238
- (validationMode === ValidationMode.Either && !hasValidated) ||
239
- validationMode === ValidationMode.Both) &&
240
- core.Is.function(handler.jsonSchema)) {
241
- // Otherwise use the JSON schema if there is one
242
- const schema = await handler.jsonSchema();
243
- if (core.Is.object(schema)) {
244
- const validationResult = await JsonSchemaHelper.validate(schema, data);
245
- if (core.Is.arrayValue(validationResult.error)) {
246
- validationFailures.push({
247
- property: propertyName,
248
- reason: "validation.schema.failedValidation",
249
- properties: {
250
- value: data,
251
- schemaErrors: validationResult.error,
252
- message: validationResult.error.map(e => e.message).join("\n")
253
- }
254
- });
255
- }
256
- if (!validationResult.result) {
257
- isValid = false;
258
- }
259
- }
260
- }
261
- }
262
- else if (options?.failOnMissingType ?? false) {
263
- // If we don't have a handler for a specific type and we are failing on missing type
264
- validationFailures.push({
265
- property: propertyName,
266
- reason: "validation.schema.missingType",
267
- properties: {
268
- dataType
269
- }
270
- });
271
- isValid = false;
272
- }
273
- }
274
- return isValid;
275
- }
276
- }
277
-
278
- exports.DataTypeHandlerFactory = DataTypeHandlerFactory;
279
- exports.DataTypeHelper = DataTypeHelper;
280
- exports.IdentifierHandlerFactory = IdentifierHandlerFactory;
281
- exports.JsonSchemaHelper = JsonSchemaHelper;
282
- exports.ValidationMode = ValidationMode;