@twin.org/data-core 0.0.1-next.8 → 0.0.1

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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  var core = require('@twin.org/core');
4
4
  var web = require('@twin.org/web');
5
- var Ajv = require('ajv');
5
+ var Ajv = require('ajv/dist/2020.js');
6
6
  var addFormats = require('ajv-formats');
7
7
 
8
8
  // Copyright 2024 IOTA Stiftung.
@@ -49,7 +49,11 @@ const ValidationMode = {
49
49
  /**
50
50
  * Use either validation mode.
51
51
  */
52
- Either: "either"
52
+ Either: "either",
53
+ /**
54
+ * Use both validation modes.
55
+ */
56
+ Both: "both"
53
57
  };
54
58
 
55
59
  // Copyright 2024 IOTA Stiftung.
@@ -77,6 +81,10 @@ class JsonSchemaHelper {
77
81
  static async validate(schema, data, additionalTypes) {
78
82
  const ajv = new Ajv({
79
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,
80
88
  loadSchema: async (uri) => {
81
89
  const subTypeHandler = DataTypeHandlerFactory.getIfExists(uri);
82
90
  if (core.Is.function(subTypeHandler?.jsonSchema)) {
@@ -205,23 +213,30 @@ class DataTypeHelper {
205
213
  * @param dataType The data type to validate.
206
214
  * @param data The data to validate.
207
215
  * @param validationFailures The list of validation failures to add to.
208
- * @param validationMode The validation mode to use, defaults to either.
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.
209
219
  * @returns True if the data was valid.
210
220
  */
211
- static async validate(propertyName, dataType, data, validationFailures, validationMode) {
221
+ static async validate(propertyName, dataType, data, validationFailures, options) {
222
+ let isValid = true;
212
223
  if (core.Is.stringValue(dataType)) {
213
224
  const handler = DataTypeHandlerFactory.getIfExists(dataType);
214
225
  if (handler) {
215
- validationMode = validationMode ?? ValidationMode.Either;
226
+ const validationMode = options?.validationMode ?? ValidationMode.Either;
216
227
  // If we have a validate function use that as it is more specific
217
228
  // and will produce better error messages
229
+ let hasValidated = false;
218
230
  if ((validationMode === ValidationMode.Validate ||
231
+ validationMode === ValidationMode.Both ||
219
232
  validationMode === ValidationMode.Either) &&
220
233
  core.Is.function(handler.validate)) {
221
- return handler.validate(propertyName, data, validationFailures);
234
+ isValid = await handler.validate(propertyName, data, validationFailures);
235
+ hasValidated = true;
222
236
  }
223
- else if ((validationMode === ValidationMode.JsonSchema ||
224
- validationMode === ValidationMode.Either) &&
237
+ if ((validationMode === ValidationMode.JsonSchema ||
238
+ (validationMode === ValidationMode.Either && !hasValidated) ||
239
+ validationMode === ValidationMode.Both) &&
225
240
  core.Is.function(handler.jsonSchema)) {
226
241
  // Otherwise use the JSON schema if there is one
227
242
  const schema = await handler.jsonSchema();
@@ -234,17 +249,29 @@ class DataTypeHelper {
234
249
  properties: {
235
250
  value: data,
236
251
  schemaErrors: validationResult.error,
237
- message: validationResult.error.map(e => e.message).join(", ")
252
+ message: validationResult.error.map(e => e.message).join("\n")
238
253
  }
239
254
  });
240
255
  }
241
- return validationResult.result;
256
+ if (!validationResult.result) {
257
+ isValid = false;
258
+ }
242
259
  }
243
260
  }
244
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
+ }
245
273
  }
246
- // Return true by default if no other mechanism for validation is available
247
- return true;
274
+ return isValid;
248
275
  }
249
276
  }
250
277
 
@@ -1,6 +1,6 @@
1
1
  import { Factory, Urn, Is, StringHelper } from '@twin.org/core';
2
2
  import { FetchHelper, HttpMethod } from '@twin.org/web';
3
- import Ajv from 'ajv';
3
+ import Ajv from 'ajv/dist/2020.js';
4
4
  import addFormats from 'ajv-formats';
5
5
 
6
6
  // Copyright 2024 IOTA Stiftung.
@@ -47,7 +47,11 @@ const ValidationMode = {
47
47
  /**
48
48
  * Use either validation mode.
49
49
  */
50
- Either: "either"
50
+ Either: "either",
51
+ /**
52
+ * Use both validation modes.
53
+ */
54
+ Both: "both"
51
55
  };
52
56
 
53
57
  // Copyright 2024 IOTA Stiftung.
@@ -75,6 +79,10 @@ class JsonSchemaHelper {
75
79
  static async validate(schema, data, additionalTypes) {
76
80
  const ajv = new Ajv({
77
81
  allowUnionTypes: true,
82
+ // Disable strict tuples as it causes issues with the schema validation when
83
+ // you have an array with fixed elements e.g. myType: [string, ...string[]]
84
+ // https://github.com/ajv-validator/ajv/issues/1417
85
+ strictTuples: false,
78
86
  loadSchema: async (uri) => {
79
87
  const subTypeHandler = DataTypeHandlerFactory.getIfExists(uri);
80
88
  if (Is.function(subTypeHandler?.jsonSchema)) {
@@ -203,23 +211,30 @@ class DataTypeHelper {
203
211
  * @param dataType The data type to validate.
204
212
  * @param data The data to validate.
205
213
  * @param validationFailures The list of validation failures to add to.
206
- * @param validationMode The validation mode to use, defaults to either.
214
+ * @param options Optional options for validation.
215
+ * @param options.failOnMissingType If true, will fail validation if the data type is missing, defaults to false.
216
+ * @param options.validationMode The validation mode to use, defaults to either.
207
217
  * @returns True if the data was valid.
208
218
  */
209
- static async validate(propertyName, dataType, data, validationFailures, validationMode) {
219
+ static async validate(propertyName, dataType, data, validationFailures, options) {
220
+ let isValid = true;
210
221
  if (Is.stringValue(dataType)) {
211
222
  const handler = DataTypeHandlerFactory.getIfExists(dataType);
212
223
  if (handler) {
213
- validationMode = validationMode ?? ValidationMode.Either;
224
+ const validationMode = options?.validationMode ?? ValidationMode.Either;
214
225
  // If we have a validate function use that as it is more specific
215
226
  // and will produce better error messages
227
+ let hasValidated = false;
216
228
  if ((validationMode === ValidationMode.Validate ||
229
+ validationMode === ValidationMode.Both ||
217
230
  validationMode === ValidationMode.Either) &&
218
231
  Is.function(handler.validate)) {
219
- return handler.validate(propertyName, data, validationFailures);
232
+ isValid = await handler.validate(propertyName, data, validationFailures);
233
+ hasValidated = true;
220
234
  }
221
- else if ((validationMode === ValidationMode.JsonSchema ||
222
- validationMode === ValidationMode.Either) &&
235
+ if ((validationMode === ValidationMode.JsonSchema ||
236
+ (validationMode === ValidationMode.Either && !hasValidated) ||
237
+ validationMode === ValidationMode.Both) &&
223
238
  Is.function(handler.jsonSchema)) {
224
239
  // Otherwise use the JSON schema if there is one
225
240
  const schema = await handler.jsonSchema();
@@ -232,17 +247,29 @@ class DataTypeHelper {
232
247
  properties: {
233
248
  value: data,
234
249
  schemaErrors: validationResult.error,
235
- message: validationResult.error.map(e => e.message).join(", ")
250
+ message: validationResult.error.map(e => e.message).join("\n")
236
251
  }
237
252
  });
238
253
  }
239
- return validationResult.result;
254
+ if (!validationResult.result) {
255
+ isValid = false;
256
+ }
240
257
  }
241
258
  }
242
259
  }
260
+ else if (options?.failOnMissingType ?? false) {
261
+ // If we don't have a handler for a specific type and we are failing on missing type
262
+ validationFailures.push({
263
+ property: propertyName,
264
+ reason: "validation.schema.missingType",
265
+ properties: {
266
+ dataType
267
+ }
268
+ });
269
+ isValid = false;
270
+ }
243
271
  }
244
- // Return true by default if no other mechanism for validation is available
245
- return true;
272
+ return isValid;
246
273
  }
247
274
  }
248
275
 
@@ -2,6 +2,7 @@ export * from "./factories/dataTypeHandlerFactory";
2
2
  export * from "./factories/identifierHandlerFactory";
3
3
  export * from "./models/IDataTypeHandler";
4
4
  export * from "./models/IIdentifierHandler";
5
+ export * from "./models/IJsonSchema";
5
6
  export * from "./models/ISchemaValidationError";
6
7
  export * from "./models/ISchemaValidationResult";
7
8
  export * from "./models/validationMode";
@@ -1,22 +1,26 @@
1
1
  import type { IValidationFailure } from "@twin.org/core";
2
- import type { JSONSchema7 } from "json-schema";
2
+ import type { IJsonSchema } from "./IJsonSchema";
3
3
  /**
4
4
  * Interface describing a type which can handle a specific data type.
5
5
  */
6
6
  export interface IDataTypeHandler {
7
+ /**
8
+ * The context for the type.
9
+ */
10
+ context: string;
7
11
  /**
8
12
  * The type for the item.
9
13
  */
10
14
  type: string;
11
15
  /**
12
- * The default value for the item.
16
+ * The default value for the item to use when constructing a new object.
13
17
  */
14
18
  defaultValue?: unknown;
15
19
  /**
16
20
  * Get the JSON schema for the data type.
17
21
  * @returns The JSON schema for the data type.
18
22
  */
19
- jsonSchema?(): Promise<JSONSchema7 | undefined>;
23
+ jsonSchema?(): Promise<IJsonSchema | undefined>;
20
24
  /**
21
25
  * A method for validating the data type.
22
26
  * @param propertyName The name of the property being validated.
@@ -0,0 +1,5 @@
1
+ import type { SchemaObject } from "ajv/dist/2020";
2
+ /**
3
+ * Default schema type.
4
+ */
5
+ export type IJsonSchema = SchemaObject;
@@ -14,6 +14,10 @@ export declare const ValidationMode: {
14
14
  * Use either validation mode.
15
15
  */
16
16
  readonly Either: "either";
17
+ /**
18
+ * Use both validation modes.
19
+ */
20
+ readonly Both: "both";
17
21
  };
18
22
  /**
19
23
  * Validation modes for validating data types.
@@ -10,8 +10,13 @@ export declare class DataTypeHelper {
10
10
  * @param dataType The data type to validate.
11
11
  * @param data The data to validate.
12
12
  * @param validationFailures The list of validation failures to add to.
13
- * @param validationMode The validation mode to use, defaults to either.
13
+ * @param options Optional options for validation.
14
+ * @param options.failOnMissingType If true, will fail validation if the data type is missing, defaults to false.
15
+ * @param options.validationMode The validation mode to use, defaults to either.
14
16
  * @returns True if the data was valid.
15
17
  */
16
- static validate(propertyName: string, dataType: string | undefined, data: unknown, validationFailures: IValidationFailure[], validationMode?: ValidationMode): Promise<boolean>;
18
+ static validate(propertyName: string, dataType: string | undefined, data: unknown, validationFailures: IValidationFailure[], options?: {
19
+ validationMode?: ValidationMode;
20
+ failOnMissingType?: boolean;
21
+ }): Promise<boolean>;
17
22
  }
@@ -1,5 +1,5 @@
1
1
  import type { IEntitySchema } from "@twin.org/entity";
2
- import type { JSONSchema7 } from "json-schema";
2
+ import type { IJsonSchema } from "../models/IJsonSchema";
3
3
  import type { ISchemaValidationResult } from "../models/ISchemaValidationResult";
4
4
  /**
5
5
  * A helper for JSON schemas.
@@ -16,8 +16,8 @@ export declare class JsonSchemaHelper {
16
16
  * @param additionalTypes Additional types to add for reference, not already in DataTypeHandlerFactory.
17
17
  * @returns Result containing errors if there are any.
18
18
  */
19
- static validate<T = unknown>(schema: JSONSchema7, data: T, additionalTypes?: {
20
- [id: string]: JSONSchema7;
19
+ static validate<T = unknown>(schema: IJsonSchema, data: T, additionalTypes?: {
20
+ [id: string]: IJsonSchema;
21
21
  }): Promise<ISchemaValidationResult>;
22
22
  /**
23
23
  * Get the property type from a schema.
@@ -25,12 +25,12 @@ export declare class JsonSchemaHelper {
25
25
  * @param propertyName The name of the property to get the type for.
26
26
  * @returns The types of the property.
27
27
  */
28
- static getPropertyType(schema: JSONSchema7, propertyName: string): string | undefined;
28
+ static getPropertyType(schema: IJsonSchema, propertyName: string): string | undefined;
29
29
  /**
30
30
  * Convert an entity schema to JSON schema e.g https://example.com/schemas/.
31
31
  * @param entitySchema The entity schema to convert.
32
32
  * @param baseDomain The base domain for local schemas e.g. https://example.com/
33
33
  * @returns The JSON schema for the entity.
34
34
  */
35
- static entitySchemaToJsonSchema(entitySchema: IEntitySchema | undefined, baseDomain?: string): JSONSchema7;
35
+ static entitySchemaToJsonSchema(entitySchema: IEntitySchema | undefined, baseDomain?: string): IJsonSchema;
36
36
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,97 @@
1
1
  # @twin.org/data-core - Changelog
2
2
 
3
- ## v0.0.1-next.8
3
+ ## 0.0.1 (2025-07-03)
4
+
5
+
6
+ ### Features
7
+
8
+ * add document cache access methods ([dbf1e36](https://github.com/twinfoundation/data/commit/dbf1e36d176c5f428f8c52628fb5a1ff7a6a174a))
9
+ * add fail on missing type option and both mode ([e8b9702](https://github.com/twinfoundation/data/commit/e8b97029a04b646497ff0e55b9610291e58ae92a))
10
+ * expand Json LD Keyword ([70632d1](https://github.com/twinfoundation/data/commit/70632d1e11ad85cf3c57e118476b125a673f1681))
11
+ * use fully qualified names for data type lookups ([b7b5c74](https://github.com/twinfoundation/data/commit/b7b5c746b0180a87baa976f6a7a76cedd53d8ff7))
12
+ * use shared store mechanism ([#3](https://github.com/twinfoundation/data/issues/3)) ([33eb221](https://github.com/twinfoundation/data/commit/33eb221ccec2b4a79549c06e9a04225009b93a46))
13
+ * use updated JSON schema specs ([465223a](https://github.com/twinfoundation/data/commit/465223a9e9c24af546480ef084327a78fa366eaa))
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * interface name ([6e49322](https://github.com/twinfoundation/data/commit/6e49322ec1797417220ec9e529bb124f4717f489))
19
+ * remove undici reference ([d77721e](https://github.com/twinfoundation/data/commit/d77721e21d23c7a6750c2f5cac8104851dfaa6d7))
20
+ * tests using context ([577b3bb](https://github.com/twinfoundation/data/commit/577b3bbb661eafbf6d3fd157133c106732e8eb3d))
21
+
22
+ ## [0.0.1-next.37](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.36...data-core-v0.0.1-next.37) (2025-06-11)
23
+
24
+
25
+ ### Features
26
+
27
+ * use updated JSON schema specs ([465223a](https://github.com/twinfoundation/data/commit/465223a9e9c24af546480ef084327a78fa366eaa))
28
+
29
+
30
+ ### Bug Fixes
31
+
32
+ * remove undici reference ([d77721e](https://github.com/twinfoundation/data/commit/d77721e21d23c7a6750c2f5cac8104851dfaa6d7))
33
+
34
+ ## [0.0.1-next.36](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.35...data-core-v0.0.1-next.36) (2025-06-10)
35
+
36
+
37
+ ### Features
38
+
39
+ * expand Json LD Keyword ([70632d1](https://github.com/twinfoundation/data/commit/70632d1e11ad85cf3c57e118476b125a673f1681))
40
+
41
+ ## [0.0.1-next.35](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.34...data-core-v0.0.1-next.35) (2025-06-03)
42
+
43
+
44
+ ### Miscellaneous Chores
45
+
46
+ * **data-core:** Synchronize repo versions
47
+
48
+ ## [0.0.1-next.34](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.33...data-core-v0.0.1-next.34) (2025-06-02)
49
+
50
+
51
+ ### Miscellaneous Chores
52
+
53
+ * **data-core:** Synchronize repo versions
54
+
55
+ ## [0.0.1-next.33](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.32...data-core-v0.0.1-next.33) (2025-06-02)
56
+
57
+
58
+ ### Features
59
+
60
+ * add fail on missing type option and both mode ([e8b9702](https://github.com/twinfoundation/data/commit/e8b97029a04b646497ff0e55b9610291e58ae92a))
61
+
62
+
63
+ ### Bug Fixes
64
+
65
+ * tests using context ([577b3bb](https://github.com/twinfoundation/data/commit/577b3bbb661eafbf6d3fd157133c106732e8eb3d))
66
+
67
+ ## [0.0.1-next.32](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.31...data-core-v0.0.1-next.32) (2025-05-28)
68
+
69
+
70
+ ### Features
71
+
72
+ * use fully qualified names for data type lookups ([b7b5c74](https://github.com/twinfoundation/data/commit/b7b5c746b0180a87baa976f6a7a76cedd53d8ff7))
73
+
74
+ ## [0.0.1-next.31](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.30...data-core-v0.0.1-next.31) (2025-05-08)
75
+
76
+
77
+ ### Miscellaneous Chores
78
+
79
+ * **data-core:** Synchronize repo versions
80
+
81
+ ## [0.0.1-next.30](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.29...data-core-v0.0.1-next.30) (2025-04-17)
82
+
83
+
84
+ ### Features
85
+
86
+ * use shared store mechanism ([#3](https://github.com/twinfoundation/data/issues/3)) ([33eb221](https://github.com/twinfoundation/data/commit/33eb221ccec2b4a79549c06e9a04225009b93a46))
87
+
88
+ ## [0.0.1-next.29](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.28...data-core-v0.0.1-next.29) (2025-03-28)
89
+
90
+
91
+ ### Features
92
+
93
+ * add document cache access methods ([dbf1e36](https://github.com/twinfoundation/data/commit/dbf1e36d176c5f428f8c52628fb5a1ff7a6a174a))
94
+
95
+ ## v0.0.1-next.28
4
96
 
5
97
  - Initial Release
@@ -4,44 +4,64 @@ Class to help with data types.
4
4
 
5
5
  ## Constructors
6
6
 
7
- ### new DataTypeHelper()
7
+ ### Constructor
8
8
 
9
- > **new DataTypeHelper**(): [`DataTypeHelper`](DataTypeHelper.md)
9
+ > **new DataTypeHelper**(): `DataTypeHelper`
10
10
 
11
11
  #### Returns
12
12
 
13
- [`DataTypeHelper`](DataTypeHelper.md)
13
+ `DataTypeHelper`
14
14
 
15
15
  ## Methods
16
16
 
17
17
  ### validate()
18
18
 
19
- > `static` **validate**(`propertyName`, `dataType`, `data`, `validationFailures`, `validationMode`?): `Promise`\<`boolean`\>
19
+ > `static` **validate**(`propertyName`, `dataType`, `data`, `validationFailures`, `options?`): `Promise`\<`boolean`\>
20
20
 
21
21
  Validate a data type.
22
22
 
23
23
  #### Parameters
24
24
 
25
- **propertyName**: `string`
25
+ ##### propertyName
26
+
27
+ `string`
26
28
 
27
29
  The name of the property being validated to use in error messages.
28
30
 
29
- **dataType**: `undefined` \| `string`
31
+ ##### dataType
30
32
 
31
33
  The data type to validate.
32
34
 
33
- **data**: `unknown`
35
+ `undefined` | `string`
36
+
37
+ ##### data
38
+
39
+ `unknown`
34
40
 
35
41
  The data to validate.
36
42
 
37
- **validationFailures**: `IValidationFailure`[]
43
+ ##### validationFailures
44
+
45
+ `IValidationFailure`[]
38
46
 
39
47
  The list of validation failures to add to.
40
48
 
41
- **validationMode?**: [`ValidationMode`](../type-aliases/ValidationMode.md)
49
+ ##### options?
50
+
51
+ Optional options for validation.
52
+
53
+ ###### validationMode?
54
+
55
+ [`ValidationMode`](../type-aliases/ValidationMode.md)
42
56
 
43
57
  The validation mode to use, defaults to either.
44
58
 
59
+ ###### failOnMissingType?
60
+
61
+ `boolean`
62
+
63
+ If true, will fail validation if the data type is missing, defaults to false.
64
+
45
65
  #### Returns
46
66
 
47
67
  `Promise`\<`boolean`\>
@@ -4,13 +4,13 @@ A helper for JSON schemas.
4
4
 
5
5
  ## Constructors
6
6
 
7
- ### new JsonSchemaHelper()
7
+ ### Constructor
8
8
 
9
- > **new JsonSchemaHelper**(): [`JsonSchemaHelper`](JsonSchemaHelper.md)
9
+ > **new JsonSchemaHelper**(): `JsonSchemaHelper`
10
10
 
11
11
  #### Returns
12
12
 
13
- [`JsonSchemaHelper`](JsonSchemaHelper.md)
13
+ `JsonSchemaHelper`
14
14
 
15
15
  ## Properties
16
16
 
@@ -24,25 +24,31 @@ The schema version.
24
24
 
25
25
  ### validate()
26
26
 
27
- > `static` **validate**\<`T`\>(`schema`, `data`, `additionalTypes`?): `Promise`\<[`ISchemaValidationResult`](../interfaces/ISchemaValidationResult.md)\>
27
+ > `static` **validate**\<`T`\>(`schema`, `data`, `additionalTypes?`): `Promise`\<[`ISchemaValidationResult`](../interfaces/ISchemaValidationResult.md)\>
28
28
 
29
29
  Validates data against the schema.
30
30
 
31
31
  #### Type Parameters
32
32
 
33
- **T** = `unknown`
33
+ ##### T
34
+
35
+ `T` = `unknown`
34
36
 
35
37
  #### Parameters
36
38
 
37
- **schema**: `JSONSchema7`
39
+ ##### schema
40
+
41
+ `SchemaObject`
38
42
 
39
43
  The schema to validate the data with.
40
44
 
41
- **data**: `T`
45
+ ##### data
46
+
47
+ `T`
42
48
 
43
49
  The data to be validated.
44
50
 
45
- **additionalTypes?**
51
+ ##### additionalTypes?
46
52
 
47
53
  Additional types to add for reference, not already in DataTypeHandlerFactory.
48
54
 
@@ -62,11 +68,15 @@ Get the property type from a schema.
62
68
 
63
69
  #### Parameters
64
70
 
65
- **schema**: `JSONSchema7`
71
+ ##### schema
72
+
73
+ `SchemaObject`
66
74
 
67
75
  The schema to extract the types from.
68
76
 
69
- **propertyName**: `string`
77
+ ##### propertyName
78
+
79
+ `string`
70
80
 
71
81
  The name of the property to get the type for.
72
82
 
@@ -80,22 +90,26 @@ The types of the property.
80
90
 
81
91
  ### entitySchemaToJsonSchema()
82
92
 
83
- > `static` **entitySchemaToJsonSchema**(`entitySchema`, `baseDomain`?): `JSONSchema7`
93
+ > `static` **entitySchemaToJsonSchema**(`entitySchema`, `baseDomain?`): `SchemaObject`
84
94
 
85
95
  Convert an entity schema to JSON schema e.g https://example.com/schemas/.
86
96
 
87
97
  #### Parameters
88
98
 
89
- **entitySchema**: `undefined` \| `IEntitySchema`\<`unknown`\>
99
+ ##### entitySchema
90
100
 
91
101
  The entity schema to convert.
92
102
 
93
- **baseDomain?**: `string`
103
+ `undefined` | `IEntitySchema`\<`unknown`\>
104
+
105
+ ##### baseDomain?
106
+
107
+ `string`
94
108
 
95
109
  The base domain for local schemas e.g. https://example.com/
96
110
 
97
111
  #### Returns
98
112
 
99
- `JSONSchema7`
113
+ `SchemaObject`
100
114
 
101
115
  The JSON schema for the entity.
@@ -13,6 +13,7 @@
13
13
 
14
14
  ## Type Aliases
15
15
 
16
+ - [IJsonSchema](type-aliases/IJsonSchema.md)
16
17
  - [ISchemaValidationError](type-aliases/ISchemaValidationError.md)
17
18
  - [ValidationMode](type-aliases/ValidationMode.md)
18
19
 
@@ -4,6 +4,14 @@ Interface describing a type which can handle a specific data type.
4
4
 
5
5
  ## Properties
6
6
 
7
+ ### context
8
+
9
+ > **context**: `string`
10
+
11
+ The context for the type.
12
+
13
+ ***
14
+
7
15
  ### type
8
16
 
9
17
  > **type**: `string`
@@ -16,19 +24,19 @@ The type for the item.
16
24
 
17
25
  > `optional` **defaultValue**: `unknown`
18
26
 
19
- The default value for the item.
27
+ The default value for the item to use when constructing a new object.
20
28
 
21
29
  ## Methods
22
30
 
23
31
  ### jsonSchema()?
24
32
 
25
- > `optional` **jsonSchema**(): `Promise`\<`undefined` \| `JSONSchema7`\>
33
+ > `optional` **jsonSchema**(): `Promise`\<`undefined` \| `SchemaObject`\>
26
34
 
27
35
  Get the JSON schema for the data type.
28
36
 
29
37
  #### Returns
30
38
 
31
- `Promise`\<`undefined` \| `JSONSchema7`\>
39
+ `Promise`\<`undefined` \| `SchemaObject`\>
32
40
 
33
41
  The JSON schema for the data type.
34
42
 
@@ -36,25 +44,33 @@ The JSON schema for the data type.
36
44
 
37
45
  ### validate()?
38
46
 
39
- > `optional` **validate**(`propertyName`, `value`, `failures`, `container`?): `Promise`\<`boolean`\>
47
+ > `optional` **validate**(`propertyName`, `value`, `failures`, `container?`): `Promise`\<`boolean`\>
40
48
 
41
49
  A method for validating the data type.
42
50
 
43
51
  #### Parameters
44
52
 
45
- **propertyName**: `string`
53
+ ##### propertyName
54
+
55
+ `string`
46
56
 
47
57
  The name of the property being validated.
48
58
 
49
- **value**: `unknown`
59
+ ##### value
60
+
61
+ `unknown`
50
62
 
51
63
  The value to validate.
52
64
 
53
- **failures**: `IValidationFailure`[]
65
+ ##### failures
66
+
67
+ `IValidationFailure`[]
54
68
 
55
69
  List of failures to add to.
56
70
 
57
- **container?**: `unknown`
71
+ ##### container?
72
+
73
+ `unknown`
58
74
 
59
75
  The object which contains this one.
60
76
 
@@ -20,15 +20,21 @@ A method for validating the identifier.
20
20
 
21
21
  #### Parameters
22
22
 
23
- **propertyName**: `string`
23
+ ##### propertyName
24
+
25
+ `string`
24
26
 
25
27
  The name of the property being validated.
26
28
 
27
- **value**: `unknown`
29
+ ##### value
30
+
31
+ `unknown`
28
32
 
29
33
  The value to validate.
30
34
 
31
- **failures**: `IValidationFailure`[]
35
+ ##### failures
36
+
37
+ `IValidationFailure`[]
32
38
 
33
39
  List of failures to add to.
34
40
 
@@ -0,0 +1,5 @@
1
+ # Type Alias: IJsonSchema
2
+
3
+ > **IJsonSchema** = `SchemaObject`
4
+
5
+ Default schema type.
@@ -1,5 +1,5 @@
1
1
  # Type Alias: ISchemaValidationError
2
2
 
3
- > **ISchemaValidationError**: `ErrorObject`[]
3
+ > **ISchemaValidationError** = `ErrorObject`[]
4
4
 
5
5
  Schema validation error.
@@ -1,5 +1,5 @@
1
1
  # Type Alias: ValidationMode
2
2
 
3
- > **ValidationMode**: *typeof* [`ValidationMode`](../variables/ValidationMode.md)\[keyof *typeof* [`ValidationMode`](../variables/ValidationMode.md)\]
3
+ > **ValidationMode** = *typeof* [`ValidationMode`](../variables/ValidationMode.md)\[keyof *typeof* [`ValidationMode`](../variables/ValidationMode.md)\]
4
4
 
5
5
  Validation modes for validating data types.
@@ -23,3 +23,9 @@ Use the JSON Schema methods of the data type.
23
23
  > `readonly` **Either**: `"either"` = `"either"`
24
24
 
25
25
  Use either validation mode.
26
+
27
+ ### Both
28
+
29
+ > `readonly` **Both**: `"both"` = `"both"`
30
+
31
+ Use both validation modes.
package/locales/en.json CHANGED
@@ -5,7 +5,8 @@
5
5
  "keyAlreadyExists": "The key already exists"
6
6
  },
7
7
  "schema": {
8
- "failedValidation": "The JSON schema failed validation, {message}"
8
+ "failedValidation": "The JSON schema failed validation, {message}",
9
+ "missingType": "Failed to validate as there is no handler for type \"{dataType}\""
9
10
  }
10
11
  }
11
12
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/data-core",
3
- "version": "0.0.1-next.8",
3
+ "version": "0.0.1",
4
4
  "description": "Definitions and helpers for using with data and schemas",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,11 +14,10 @@
14
14
  "node": ">=20.0.0"
15
15
  },
16
16
  "dependencies": {
17
- "@twin.org/core": "next",
18
- "@twin.org/entity": "next",
19
- "@twin.org/nameof": "next",
20
- "@twin.org/web": "next",
21
- "@types/json-schema": "7.0.15",
17
+ "@twin.org/core": "^0.0.1",
18
+ "@twin.org/entity": "^0.0.1",
19
+ "@twin.org/nameof": "^0.0.1",
20
+ "@twin.org/web": "^0.0.1",
22
21
  "ajv": "8.17.1",
23
22
  "ajv-formats": "3.0.1"
24
23
  },
@@ -27,9 +26,9 @@
27
26
  "types": "./dist/types/index.d.ts",
28
27
  "exports": {
29
28
  ".": {
29
+ "types": "./dist/types/index.d.ts",
30
30
  "require": "./dist/cjs/index.cjs",
31
- "import": "./dist/esm/index.mjs",
32
- "types": "./dist/types/index.d.ts"
31
+ "import": "./dist/esm/index.mjs"
33
32
  }
34
33
  },
35
34
  "files": [