@twin.org/data-core 0.0.3-next.4 → 0.0.3-next.5

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.
@@ -1 +1 @@
1
- {"version":3,"file":"IDataTypeHandler.js","sourceRoot":"","sources":["../../../src/models/IDataTypeHandler.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IValidationFailure } from \"@twin.org/core\";\nimport type { IJsonSchema } from \"./IJsonSchema.js\";\n\n/**\n * Interface describing a type which can handle a specific data type.\n */\nexport interface IDataTypeHandler {\n\t/**\n\t * The context for the type.\n\t */\n\tcontext: string;\n\n\t/**\n\t * The type for the item.\n\t */\n\ttype: string;\n\n\t/**\n\t * The default value for the item to use when constructing a new object.\n\t */\n\tdefaultValue?: unknown;\n\n\t/**\n\t * Get the JSON schema for the data type.\n\t * @returns The JSON schema for the data type.\n\t */\n\tjsonSchema?(): Promise<IJsonSchema | undefined>;\n\n\t/**\n\t * A method for validating the data type.\n\t * @param propertyName The name of the property being validated.\n\t * @param value The value to validate.\n\t * @param failures List of failures to add to.\n\t * @param container The object which contains this one.\n\t * @returns True if the item is valid.\n\t */\n\tvalidate?(\n\t\tpropertyName: string,\n\t\tvalue: unknown,\n\t\tfailures: IValidationFailure[],\n\t\tcontainer?: unknown\n\t): Promise<boolean>;\n}\n"]}
1
+ {"version":3,"file":"IDataTypeHandler.js","sourceRoot":"","sources":["../../../src/models/IDataTypeHandler.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IValidationFailure } from \"@twin.org/core\";\nimport type { IJsonSchema } from \"./IJsonSchema.js\";\n\n/**\n * Interface describing a type which can handle a specific data type.\n */\nexport interface IDataTypeHandler {\n\t/**\n\t * The namespace for the type.\n\t */\n\tnamespace: string;\n\n\t/**\n\t * The type for the item.\n\t */\n\ttype: string;\n\n\t/**\n\t * The JSON LD context for the type.\n\t */\n\tjsonLdContext?: string;\n\n\t/**\n\t * The default value for the item to use when constructing a new object.\n\t */\n\tdefaultValue?: unknown;\n\n\t/**\n\t * Get the JSON schema for the data type.\n\t * @returns The JSON schema for the data type.\n\t */\n\tjsonSchema?(): Promise<IJsonSchema | undefined>;\n\n\t/**\n\t * A method for validating the data type.\n\t * @param propertyName The name of the property being validated.\n\t * @param value The value to validate.\n\t * @param failures List of failures to add to.\n\t * @param container The object which contains this one.\n\t * @returns True if the item is valid.\n\t */\n\tvalidate?(\n\t\tpropertyName: string,\n\t\tvalue: unknown,\n\t\tfailures: IValidationFailure[],\n\t\tcontainer?: unknown\n\t): Promise<boolean>;\n}\n"]}
@@ -8,6 +8,41 @@ import { ValidationMode } from "../models/validationMode.js";
8
8
  * Class to help with data types.
9
9
  */
10
10
  export class DataTypeHelper {
11
+ /**
12
+ * Register a data type.
13
+ * @param namespace The namespace for the type.
14
+ * @param type The type for the item.
15
+ * @param jsonLdContext The JSON LD context for the type.
16
+ * @param schema The JSON schema for the type.
17
+ */
18
+ static registerType(namespace, type, jsonLdContext, schema) {
19
+ DataTypeHandlerFactory.register(`${namespace}${type}`, () => ({
20
+ namespace,
21
+ jsonLdContext,
22
+ type,
23
+ jsonSchema: async () => schema
24
+ }));
25
+ }
26
+ /**
27
+ * Register a list of types.
28
+ * @param namespace The namespace for the types.
29
+ * @param jsonLdContext The JSON LD context for the types.
30
+ * @param typeDefinition The type definitions to register.
31
+ */
32
+ static registerTypes(namespace, jsonLdContext, typeDefinition) {
33
+ for (const typeDef of typeDefinition) {
34
+ DataTypeHelper.registerType(namespace, typeDef.type, jsonLdContext, typeDef.schema);
35
+ }
36
+ }
37
+ /**
38
+ * Get the JSON schema for a data type.
39
+ * @param dataType The data type to get the schema for.
40
+ * @returns The JSON schema for the data type or undefined if not found.
41
+ */
42
+ static getSchemaForType(dataType) {
43
+ const handler = DataTypeHandlerFactory.getIfExists(dataType);
44
+ return handler?.jsonSchema ? handler.jsonSchema() : undefined;
45
+ }
11
46
  /**
12
47
  * Validate a data type.
13
48
  * @param propertyName The name of the property being validated to use in error messages.
@@ -1 +1 @@
1
- {"version":3,"file":"dataTypeHelper.js","sourceRoot":"","sources":["../../../src/utils/dataTypeHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAA2B,MAAM,gBAAgB,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,sBAAsB,EAAE,MAAM,wCAAwC,CAAC;AAEhF,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAE7D;;GAEG;AACH,MAAM,OAAO,cAAc;IAC1B;;;;;;;;;;OAUG;IACI,MAAM,CAAC,KAAK,CAAC,QAAQ,CAC3B,YAAoB,EACpB,QAA4B,EAC5B,IAAa,EACb,kBAAwC,EACxC,OAGC;QAED,IAAI,OAAO,GAAG,IAAI,CAAC;QAEnB,IAAI,EAAE,CAAC,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC9B,MAAM,OAAO,GAAG,sBAAsB,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;YAE7D,IAAI,OAAO,EAAE,CAAC;gBACb,MAAM,cAAc,GAAG,OAAO,EAAE,cAAc,IAAI,cAAc,CAAC,MAAM,CAAC;gBAExE,iEAAiE;gBACjE,yCAAyC;gBACzC,IAAI,YAAY,GAAG,KAAK,CAAC;gBACzB,MAAM,cAAc,GAAG,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;gBACvD,IACC,CAAC,cAAc,KAAK,cAAc,CAAC,QAAQ;oBAC1C,cAAc,KAAK,cAAc,CAAC,IAAI;oBACtC,cAAc,KAAK,cAAc,CAAC,MAAM,CAAC;oBAC1C,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC,EAC1B,CAAC;oBACF,OAAO,GAAG,MAA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CAAC;gBAChD,oFAAoF;gBACpF,kBAAkB,CAAC,IAAI,CAAC;oBACvB,QAAQ,EAAE,YAAY;oBACtB,MAAM,EAAE,+BAA+B;oBACvC,UAAU,EAAE;wBACX,QAAQ;qBACR;iBACD,CAAC,CAAC;gBACH,OAAO,GAAG,KAAK,CAAC;YACjB,CAAC;QACF,CAAC;QAED,OAAO,OAAO,CAAC;IAChB,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is, type IValidationFailure } from \"@twin.org/core\";\nimport { JsonSchemaHelper } from \"./jsonSchemaHelper.js\";\nimport { DataTypeHandlerFactory } from \"../factories/dataTypeHandlerFactory.js\";\nimport type { IJsonSchema } from \"../models/IJsonSchema.js\";\nimport { ValidationMode } from \"../models/validationMode.js\";\n\n/**\n * Class to help with data types.\n */\nexport class DataTypeHelper {\n\t/**\n\t * Validate a data type.\n\t * @param propertyName The name of the property being validated to use in error messages.\n\t * @param dataType The data type to validate.\n\t * @param data The data to validate.\n\t * @param validationFailures The list of validation failures to add to.\n\t * @param options Optional options for validation.\n\t * @param options.failOnMissingType If true, will fail validation if the data type is missing, defaults to false.\n\t * @param options.validationMode The validation mode to use, defaults to either.\n\t * @returns True if the data was valid.\n\t */\n\tpublic static async validate(\n\t\tpropertyName: string,\n\t\tdataType: string | undefined,\n\t\tdata: unknown,\n\t\tvalidationFailures: IValidationFailure[],\n\t\toptions?: {\n\t\t\tvalidationMode?: ValidationMode;\n\t\t\tfailOnMissingType?: boolean;\n\t\t}\n\t): Promise<boolean> {\n\t\tlet isValid = true;\n\n\t\tif (Is.stringValue(dataType)) {\n\t\t\tconst handler = DataTypeHandlerFactory.getIfExists(dataType);\n\n\t\t\tif (handler) {\n\t\t\t\tconst validationMode = options?.validationMode ?? ValidationMode.Either;\n\n\t\t\t\t// If we have a validate function use that as it is more specific\n\t\t\t\t// and will produce better error messages\n\t\t\t\tlet hasValidated = false;\n\t\t\t\tconst validateMethod = handler.validate?.bind(handler);\n\t\t\t\tif (\n\t\t\t\t\t(validationMode === ValidationMode.Validate ||\n\t\t\t\t\t\tvalidationMode === ValidationMode.Both ||\n\t\t\t\t\t\tvalidationMode === ValidationMode.Either) &&\n\t\t\t\t\tIs.function(validateMethod)\n\t\t\t\t) {\n\t\t\t\t\tisValid = await validateMethod(propertyName, data, validationFailures);\n\t\t\t\t\thasValidated = true;\n\t\t\t\t}\n\n\t\t\t\tconst jsonSchemaMethod = handler.jsonSchema?.bind(handler);\n\t\t\t\tif (\n\t\t\t\t\t(validationMode === ValidationMode.JsonSchema ||\n\t\t\t\t\t\t(validationMode === ValidationMode.Either && !hasValidated) ||\n\t\t\t\t\t\tvalidationMode === ValidationMode.Both) &&\n\t\t\t\t\tIs.function(jsonSchemaMethod)\n\t\t\t\t) {\n\t\t\t\t\t// Otherwise use the JSON schema if there is one\n\t\t\t\t\tconst schema = await jsonSchemaMethod();\n\n\t\t\t\t\tif (Is.object<IJsonSchema>(schema)) {\n\t\t\t\t\t\tconst validationResult = await JsonSchemaHelper.validate(schema, data);\n\t\t\t\t\t\tif (Is.arrayValue(validationResult.error)) {\n\t\t\t\t\t\t\tvalidationFailures.push({\n\t\t\t\t\t\t\t\tproperty: propertyName,\n\t\t\t\t\t\t\t\treason: \"validation.schema.failedValidation\",\n\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\tvalue: data,\n\t\t\t\t\t\t\t\t\tschemaErrors: validationResult.error,\n\t\t\t\t\t\t\t\t\tmessage: validationResult.error.map(e => e.message).join(\"\\n\")\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!validationResult.result) {\n\t\t\t\t\t\t\tisValid = false;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (options?.failOnMissingType ?? false) {\n\t\t\t\t// If we don't have a handler for a specific type and we are failing on missing type\n\t\t\t\tvalidationFailures.push({\n\t\t\t\t\tproperty: propertyName,\n\t\t\t\t\treason: \"validation.schema.missingType\",\n\t\t\t\t\tproperties: {\n\t\t\t\t\t\tdataType\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\tisValid = false;\n\t\t\t}\n\t\t}\n\n\t\treturn isValid;\n\t}\n}\n"]}
1
+ {"version":3,"file":"dataTypeHelper.js","sourceRoot":"","sources":["../../../src/utils/dataTypeHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAA2B,MAAM,gBAAgB,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,sBAAsB,EAAE,MAAM,wCAAwC,CAAC;AAEhF,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAE7D;;GAEG;AACH,MAAM,OAAO,cAAc;IAC1B;;;;;;OAMG;IACI,MAAM,CAAC,YAAY,CACzB,SAAiB,EACjB,IAAY,EACZ,aAAiC,EACjC,MAA0C;QAE1C,sBAAsB,CAAC,QAAQ,CAAC,GAAG,SAAS,GAAG,IAAI,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;YAC7D,SAAS;YACT,aAAa;YACb,IAAI;YACJ,UAAU,EAAE,KAAK,IAAI,EAAE,CAAC,MAAM;SAC9B,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,aAAa,CAC1B,SAAiB,EACjB,aAAiC,EACjC,cAGG;QAEH,KAAK,MAAM,OAAO,IAAI,cAAc,EAAE,CAAC;YACtC,cAAc,CAAC,YAAY,CAAC,SAAS,EAAE,OAAO,CAAC,IAAI,EAAE,aAAa,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;QACrF,CAAC;IACF,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,gBAAgB,CAAC,QAAgB;QAC9C,MAAM,OAAO,GAAG,sBAAsB,CAAC,WAAW,CA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ailure } from \"@twin.org/core\";\nimport { JsonSchemaHelper } from \"./jsonSchemaHelper.js\";\nimport { DataTypeHandlerFactory } from \"../factories/dataTypeHandlerFactory.js\";\nimport type { IJsonSchema } from \"../models/IJsonSchema.js\";\nimport { ValidationMode } from \"../models/validationMode.js\";\n\n/**\n * Class to help with data types.\n */\nexport class DataTypeHelper {\n\t/**\n\t * Register a data type.\n\t * @param namespace The namespace for the type.\n\t * @param type The type for the item.\n\t * @param jsonLdContext The JSON LD context for the type.\n\t * @param schema The JSON schema for the type.\n\t */\n\tpublic static registerType(\n\t\tnamespace: string,\n\t\ttype: string,\n\t\tjsonLdContext: string | undefined,\n\t\tschema: IJsonSchema | Promise<IJsonSchema>\n\t): void {\n\t\tDataTypeHandlerFactory.register(`${namespace}${type}`, () => ({\n\t\t\tnamespace,\n\t\t\tjsonLdContext,\n\t\t\ttype,\n\t\t\tjsonSchema: async () => schema\n\t\t}));\n\t}\n\n\t/**\n\t * Register a list of types.\n\t * @param namespace The namespace for the types.\n\t * @param jsonLdContext The JSON LD context for the types.\n\t * @param typeDefinition The type definitions to register.\n\t */\n\tpublic static registerTypes(\n\t\tnamespace: string,\n\t\tjsonLdContext: string | undefined,\n\t\ttypeDefinition: {\n\t\t\ttype: string;\n\t\t\tschema: IJsonSchema | Promise<IJsonSchema>;\n\t\t}[]\n\t): void {\n\t\tfor (const typeDef of typeDefinition) {\n\t\t\tDataTypeHelper.registerType(namespace, typeDef.type, jsonLdContext, typeDef.schema);\n\t\t}\n\t}\n\n\t/**\n\t * Get the JSON schema for a data type.\n\t * @param dataType The data type to get the schema for.\n\t * @returns The JSON schema for the data type or undefined if not found.\n\t */\n\tpublic static getSchemaForType(dataType: string): IJsonSchema | undefined {\n\t\tconst handler = DataTypeHandlerFactory.getIfExists(dataType);\n\t\treturn handler?.jsonSchema ? handler.jsonSchema() : undefined;\n\t}\n\n\t/**\n\t * Validate a data type.\n\t * @param propertyName The name of the property being validated to use in error messages.\n\t * @param dataType The data type to validate.\n\t * @param data The data to validate.\n\t * @param validationFailures The list of validation failures to add to.\n\t * @param options Optional options for validation.\n\t * @param options.failOnMissingType If true, will fail validation if the data type is missing, defaults to false.\n\t * @param options.validationMode The validation mode to use, defaults to either.\n\t * @returns True if the data was valid.\n\t */\n\tpublic static async validate(\n\t\tpropertyName: string,\n\t\tdataType: string | undefined,\n\t\tdata: unknown,\n\t\tvalidationFailures: IValidationFailure[],\n\t\toptions?: {\n\t\t\tvalidationMode?: ValidationMode;\n\t\t\tfailOnMissingType?: boolean;\n\t\t}\n\t): Promise<boolean> {\n\t\tlet isValid = true;\n\n\t\tif (Is.stringValue(dataType)) {\n\t\t\tconst handler = DataTypeHandlerFactory.getIfExists(dataType);\n\n\t\t\tif (handler) {\n\t\t\t\tconst validationMode = options?.validationMode ?? ValidationMode.Either;\n\n\t\t\t\t// If we have a validate function use that as it is more specific\n\t\t\t\t// and will produce better error messages\n\t\t\t\tlet hasValidated = false;\n\t\t\t\tconst validateMethod = handler.validate?.bind(handler);\n\t\t\t\tif (\n\t\t\t\t\t(validationMode === ValidationMode.Validate ||\n\t\t\t\t\t\tvalidationMode === ValidationMode.Both ||\n\t\t\t\t\t\tvalidationMode === ValidationMode.Either) &&\n\t\t\t\t\tIs.function(validateMethod)\n\t\t\t\t) {\n\t\t\t\t\tisValid = await validateMethod(propertyName, data, validationFailures);\n\t\t\t\t\thasValidated = true;\n\t\t\t\t}\n\n\t\t\t\tconst jsonSchemaMethod = handler.jsonSchema?.bind(handler);\n\t\t\t\tif (\n\t\t\t\t\t(validationMode === ValidationMode.JsonSchema ||\n\t\t\t\t\t\t(validationMode === ValidationMode.Either && !hasValidated) ||\n\t\t\t\t\t\tvalidationMode === ValidationMode.Both) &&\n\t\t\t\t\tIs.function(jsonSchemaMethod)\n\t\t\t\t) {\n\t\t\t\t\t// Otherwise use the JSON schema if there is one\n\t\t\t\t\tconst schema = await jsonSchemaMethod();\n\n\t\t\t\t\tif (Is.object<IJsonSchema>(schema)) {\n\t\t\t\t\t\tconst validationResult = await JsonSchemaHelper.validate(schema, data);\n\t\t\t\t\t\tif (Is.arrayValue(validationResult.error)) {\n\t\t\t\t\t\t\tvalidationFailures.push({\n\t\t\t\t\t\t\t\tproperty: propertyName,\n\t\t\t\t\t\t\t\treason: \"validation.schema.failedValidation\",\n\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\tvalue: data,\n\t\t\t\t\t\t\t\t\tschemaErrors: validationResult.error,\n\t\t\t\t\t\t\t\t\tmessage: validationResult.error.map(e => e.message).join(\"\\n\")\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!validationResult.result) {\n\t\t\t\t\t\t\tisValid = false;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (options?.failOnMissingType ?? false) {\n\t\t\t\t// If we don't have a handler for a specific type and we are failing on missing type\n\t\t\t\tvalidationFailures.push({\n\t\t\t\t\tproperty: propertyName,\n\t\t\t\t\treason: \"validation.schema.missingType\",\n\t\t\t\t\tproperties: {\n\t\t\t\t\t\tdataType\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\tisValid = false;\n\t\t\t}\n\t\t}\n\n\t\treturn isValid;\n\t}\n}\n"]}
@@ -5,13 +5,17 @@ import type { IJsonSchema } from "./IJsonSchema.js";
5
5
  */
6
6
  export interface IDataTypeHandler {
7
7
  /**
8
- * The context for the type.
8
+ * The namespace for the type.
9
9
  */
10
- context: string;
10
+ namespace: string;
11
11
  /**
12
12
  * The type for the item.
13
13
  */
14
14
  type: string;
15
+ /**
16
+ * The JSON LD context for the type.
17
+ */
18
+ jsonLdContext?: string;
15
19
  /**
16
20
  * The default value for the item to use when constructing a new object.
17
21
  */
@@ -1,9 +1,34 @@
1
1
  import { type IValidationFailure } from "@twin.org/core";
2
+ import type { IJsonSchema } from "../models/IJsonSchema.js";
2
3
  import { ValidationMode } from "../models/validationMode.js";
3
4
  /**
4
5
  * Class to help with data types.
5
6
  */
6
7
  export declare class DataTypeHelper {
8
+ /**
9
+ * Register a data type.
10
+ * @param namespace The namespace for the type.
11
+ * @param type The type for the item.
12
+ * @param jsonLdContext The JSON LD context for the type.
13
+ * @param schema The JSON schema for the type.
14
+ */
15
+ static registerType(namespace: string, type: string, jsonLdContext: string | undefined, schema: IJsonSchema | Promise<IJsonSchema>): void;
16
+ /**
17
+ * Register a list of types.
18
+ * @param namespace The namespace for the types.
19
+ * @param jsonLdContext The JSON LD context for the types.
20
+ * @param typeDefinition The type definitions to register.
21
+ */
22
+ static registerTypes(namespace: string, jsonLdContext: string | undefined, typeDefinition: {
23
+ type: string;
24
+ schema: IJsonSchema | Promise<IJsonSchema>;
25
+ }[]): void;
26
+ /**
27
+ * Get the JSON schema for a data type.
28
+ * @param dataType The data type to get the schema for.
29
+ * @returns The JSON schema for the data type or undefined if not found.
30
+ */
31
+ static getSchemaForType(dataType: string): IJsonSchema | undefined;
7
32
  /**
8
33
  * Validate a data type.
9
34
  * @param propertyName The name of the property being validated to use in error messages.
package/docs/changelog.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @twin.org/data-core - Changelog
2
2
 
3
+ ## [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)
4
+
5
+
6
+ ### Features
7
+
8
+ * improve data type registration ([#34](https://github.com/twinfoundation/data/issues/34)) ([855d110](https://github.com/twinfoundation/data/commit/855d11046a4d85317b77a5c4e0f4a7b1b6d1a767))
9
+
3
10
  ## [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)
4
11
 
5
12
 
@@ -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`): `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
+ `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`\>
@@ -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`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/data-core",
3
- "version": "0.0.3-next.4",
3
+ "version": "0.0.3-next.5",
4
4
  "description": "Definitions and helpers for using with data and schemas",
5
5
  "repository": {
6
6
  "type": "git",