@twin.org/data-core 0.0.3-next.3 → 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.
- package/dist/es/models/IDataTypeHandler.js.map +1 -1
- package/dist/es/utils/dataTypeHelper.js +35 -0
- package/dist/es/utils/dataTypeHelper.js.map +1 -1
- package/dist/es/utils/jsonSchemaHelper.js +13 -4
- package/dist/es/utils/jsonSchemaHelper.js.map +1 -1
- package/dist/types/models/IDataTypeHandler.d.ts +6 -2
- package/dist/types/utils/dataTypeHelper.d.ts +25 -0
- package/dist/types/utils/jsonSchemaHelper.d.ts +5 -1
- package/docs/changelog.md +14 -0
- package/docs/reference/classes/DataTypeHelper.md +92 -0
- package/docs/reference/classes/JsonSchemaHelper.md +9 -1
- package/docs/reference/interfaces/IDataTypeHandler.md +11 -3
- package/package.json +1 -1
|
@@ -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
|
|
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,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AC,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;iCAC9D;6BACD,CAAC,CAAC;wBACJ,CAAC;wBACD,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,CAAC;4BAC9B,OAAO,GAAG,KAAK,CAAC;wBACjB,CAAC;oBACF,CAAC;gBACF,CAAC;YACF,CAAC;iBAAM,IAAI,OAAO,EAAE,iBAAiB,IAAI,KAAK,EAAE,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,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BAAkB,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 * 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"]}
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0.
|
|
3
3
|
import { Is, StringHelper } from "@twin.org/core";
|
|
4
4
|
import { FetchHelper, HttpMethod } from "@twin.org/web";
|
|
5
|
-
import
|
|
5
|
+
import Ajv2019 from "ajv/dist/2019.js";
|
|
6
|
+
import Ajv2020 from "ajv/dist/2020.js";
|
|
6
7
|
import formatsPlugin from "ajv-formats";
|
|
7
8
|
import { DataTypeHandlerFactory } from "../factories/dataTypeHandlerFactory.js";
|
|
8
9
|
/**
|
|
@@ -10,9 +11,13 @@ import { DataTypeHandlerFactory } from "../factories/dataTypeHandlerFactory.js";
|
|
|
10
11
|
*/
|
|
11
12
|
export class JsonSchemaHelper {
|
|
12
13
|
/**
|
|
13
|
-
* The schema version.
|
|
14
|
+
* The schema version 2020 (default).
|
|
14
15
|
*/
|
|
15
16
|
static SCHEMA_VERSION = "https://json-schema.org/draft/2020-12/schema";
|
|
17
|
+
/**
|
|
18
|
+
* The schema version 2019.
|
|
19
|
+
*/
|
|
20
|
+
static SCHEMA_VERSION_2019 = "https://json-schema.org/draft/2019-09/schema";
|
|
16
21
|
/**
|
|
17
22
|
* The class name.
|
|
18
23
|
* @internal
|
|
@@ -26,7 +31,7 @@ export class JsonSchemaHelper {
|
|
|
26
31
|
* @returns Result containing errors if there are any.
|
|
27
32
|
*/
|
|
28
33
|
static async validate(schema, data, additionalTypes) {
|
|
29
|
-
const
|
|
34
|
+
const params = {
|
|
30
35
|
allowUnionTypes: true,
|
|
31
36
|
// Disable strict tuples as it causes issues with the schema validation when
|
|
32
37
|
// you have an array with fixed elements e.g. myType: [string, ...string[]]
|
|
@@ -55,7 +60,11 @@ export class JsonSchemaHelper {
|
|
|
55
60
|
return {};
|
|
56
61
|
}
|
|
57
62
|
}
|
|
58
|
-
}
|
|
63
|
+
};
|
|
64
|
+
let ajv = new Ajv2020.Ajv2020(params);
|
|
65
|
+
if (schema.$schema === JsonSchemaHelper.SCHEMA_VERSION_2019) {
|
|
66
|
+
ajv = new Ajv2019.Ajv2019({ strict: false, ...params });
|
|
67
|
+
}
|
|
59
68
|
formatsPlugin.default(ajv);
|
|
60
69
|
// Add the additional types provided by the user
|
|
61
70
|
if (Is.objectValue(additionalTypes)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsonSchemaHelper.js","sourceRoot":"","sources":["../../../src/utils/jsonSchemaHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAGlD,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,GAAG,MAAM,kBAAkB,CAAC;AACnC,OAAO,aAAa,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,sBAAsB,EAAE,MAAM,wCAAwC,CAAC;AAKhF;;GAEG;AACH,MAAM,OAAO,gBAAgB;IAC5B;;OAEG;IACI,MAAM,CAAU,cAAc,GAAG,8CAA8C,CAAC;IAEvF;;;OAGG;IACI,MAAM,CAAU,UAAU,sBAA8B;IAE/D;;;;;;OAMG;IACI,MAAM,CAAC,KAAK,CAAC,QAAQ,CAC3B,MAAmB,EACnB,IAAO,EACP,eAA+C;QAE/C,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC;YAC3B,eAAe,EAAE,IAAI;YACrB,4EAA4E;YAC5E,2EAA2E;YAC3E,mDAAmD;YACnD,YAAY,EAAE,KAAK;YACnB,UAAU,EAAE,KAAK,EAAC,GAAG,EAAC,EAAE;gBACvB,MAAM,cAAc,GAAG,sBAAsB,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;gBAC/D,MAAM,gBAAgB,GAAG,cAAc,EAAE,UAAU,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC;gBAC1E,IAAI,EAAE,CAAC,QAAQ,CAAC,gBAAgB,CAAC,EAAE,CAAC;oBACnC,MAAM,SAAS,GAAG,MAAM,gBAAgB,EAAE,CAAC;oBAC3C,IAAI,EAAE,CAAC,MAAM,CAAc,SAAS,CAAC,EAAE,CAAC;wBACvC,OA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C,GAAG,MAAM,GAAG,cAAc,CAAC,WAAW,EAAE;yBAC3C,CAAC;oBACH,CAAC;yBAAM,CAAC;wBACP,kBAAkB,CAAC,KAAK,GAAG;4BAC1B,IAAI,EAAE,cAAc,CAAC,QAAQ;yBAC7B,CAAC;oBACH,CAAC;gBACF,CAAC;qBAAM,IAAI,cAAc,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;oBAC7C,OAAO,kBAAkB,CAAC,IAAI,CAAC;oBAC/B,kBAAkB,CAAC,IAAI,GAAG,cAAc,CAAC,WAAW,EAAE,UAAU,CAAC,MAAM,CAAC;wBACvE,CAAC,CAAC,cAAc,CAAC,WAAW;wBAC5B,CAAC,CAAC,GAAG,MAAM,GAAG,cAAc,CAAC,WAAW,EAAE,CAAC;gBAC7C,CAAC;gBAED,UAAU,CAAC,cAAc,CAAC,QAAQ,CAAC,GAAG,kBAAkB,CAAC;gBAEzD,IAAI,CAAC,cAAc,CAAC,QAAQ,EAAE,CAAC;oBAC9B,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;gBACxC,CAAC;YACF,CAAC;QACF,CAAC;QAED,OAAO;YACN,OAAO,EAAE,gBAAgB,CAAC,cAAc;YACxC,GAAG,EAAE,GAAG,MAAM,GAAG,YAAY,EAAE,IAAI,EAAE;YACrC,KAAK,EAAE,YAAY,EAAE,IAAI;YACzB,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM;YACtC,WAAW,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW;YAC/C,QAAQ;YACR,UAAU;YACV,oBAAoB,EAAE,KAAK;SAC3B,CAAC;IACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is, StringHelper } from \"@twin.org/core\";\nimport type { IEntitySchema } from \"@twin.org/entity\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { FetchHelper, HttpMethod } from \"@twin.org/web\";\nimport Ajv from \"ajv/dist/2020.js\";\nimport formatsPlugin from \"ajv-formats\";\nimport { DataTypeHandlerFactory } from \"../factories/dataTypeHandlerFactory.js\";\nimport type { IJsonSchema } from \"../models/IJsonSchema.js\";\nimport type { ISchemaValidationError } from \"../models/ISchemaValidationError.js\";\nimport type { ISchemaValidationResult } from \"../models/ISchemaValidationResult.js\";\n\n/**\n * A helper for JSON schemas.\n */\nexport class JsonSchemaHelper {\n\t/**\n\t * The schema version.\n\t */\n\tpublic static readonly SCHEMA_VERSION = \"https://json-schema.org/draft/2020-12/schema\";\n\n\t/**\n\t * The class name.\n\t * @internal\n\t */\n\tpublic static readonly CLASS_NAME = nameof<JsonSchemaHelper>();\n\n\t/**\n\t * Validates data against the schema.\n\t * @param schema The schema to validate the data with.\n\t * @param data The data to be validated.\n\t * @param additionalTypes Additional types to add for reference, not already in DataTypeHandlerFactory.\n\t * @returns Result containing errors if there are any.\n\t */\n\tpublic static async validate<T = unknown>(\n\t\tschema: IJsonSchema,\n\t\tdata: T,\n\t\tadditionalTypes?: { [id: string]: IJsonSchema }\n\t): Promise<ISchemaValidationResult> {\n\t\tconst ajv = new Ajv.Ajv2020({\n\t\t\tallowUnionTypes: true,\n\t\t\t// Disable strict tuples as it causes issues with the schema validation when\n\t\t\t// you have an array with fixed elements e.g. myType: [string, ...string[]]\n\t\t\t// https://github.com/ajv-validator/ajv/issues/1417\n\t\t\tstrictTuples: false,\n\t\t\tloadSchema: async uri => {\n\t\t\t\tconst subTypeHandler = DataTypeHandlerFactory.getIfExists(uri);\n\t\t\t\tconst jsonSchemaMethod = subTypeHandler?.jsonSchema?.bind(subTypeHandler);\n\t\t\t\tif (Is.function(jsonSchemaMethod)) {\n\t\t\t\t\tconst subSchema = await jsonSchemaMethod();\n\t\t\t\t\tif (Is.object<IJsonSchema>(subSchema)) {\n\t\t\t\t\t\treturn subSchema;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\t// We don't have the type in our local data types, so we try to fetch it from the web\n\t\t\t\t\tconst result = await FetchHelper.fetchJson<never, IJsonSchema>(\n\t\t\t\t\t\tJsonSchemaHelper.CLASS_NAME,\n\t\t\t\t\t\turi,\n\t\t\t\t\t\tHttpMethod.GET,\n\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t// Cache for an hour\n\t\t\t\t\t\t\tcacheTtlMs: 3600000\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\t\t\t\t\treturn result;\n\t\t\t\t} catch {\n\t\t\t\t\t// Failed to load remotely so return an empty object\n\t\t\t\t\t// so the schema validation doesn't completely fail\n\t\t\t\t\treturn {};\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tformatsPlugin.default(ajv);\n\n\t\t// Add the additional types provided by the user\n\t\tif (Is.objectValue(additionalTypes)) {\n\t\t\tfor (const key in additionalTypes) {\n\t\t\t\tajv.addSchema(additionalTypes[key], key);\n\t\t\t}\n\t\t}\n\n\t\tconst compiled = await ajv.compileAsync(schema);\n\t\tconst result = compiled(data);\n\n\t\tconst output: ISchemaValidationResult = {\n\t\t\tresult\n\t\t};\n\n\t\tif (!output.result) {\n\t\t\toutput.error = compiled.errors as ISchemaValidationError;\n\t\t}\n\n\t\treturn output;\n\t}\n\n\t/**\n\t * Get the property type from a schema.\n\t * @param schema The schema to extract the types from.\n\t * @param propertyName The name of the property to get the type for.\n\t * @returns The types of the property.\n\t */\n\tpublic static getPropertyType(schema: IJsonSchema, propertyName: string): string | undefined {\n\t\tif (schema.type === \"object\" && Is.objectValue(schema.properties)) {\n\t\t\tconst propertySchema = schema.properties[propertyName];\n\t\t\tif (Is.object<IJsonSchema>(propertySchema)) {\n\t\t\t\tif (Is.stringValue(propertySchema.$ref)) {\n\t\t\t\t\treturn propertySchema.$ref;\n\t\t\t\t}\n\t\t\t\treturn propertySchema.type as string;\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Convert an entity schema to JSON schema e.g https://example.com/schemas/.\n\t * @param entitySchema The entity schema to convert.\n\t * @param baseDomain The base domain for local schemas e.g. https://example.com/\n\t * @returns The JSON schema for the entity.\n\t */\n\tpublic static entitySchemaToJsonSchema(\n\t\tentitySchema: IEntitySchema | undefined,\n\t\tbaseDomain?: string\n\t): IJsonSchema {\n\t\tlet domain = StringHelper.trimTrailingSlashes(baseDomain ?? \"\");\n\t\tif (domain.length > 0) {\n\t\t\tdomain += \"/\";\n\t\t}\n\n\t\tconst properties: {\n\t\t\t[key: string]: IJsonSchema;\n\t\t} = {};\n\n\t\tconst required: string[] = [];\n\n\t\tif (Is.arrayValue(entitySchema?.properties)) {\n\t\t\tfor (const propertySchema of entitySchema.properties) {\n\t\t\t\tconst jsonPropertySchema: IJsonSchema = {\n\t\t\t\t\ttype: propertySchema.type,\n\t\t\t\t\tdescription: propertySchema.description,\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\t\t\t\texamples: propertySchema.examples as unknown as any\n\t\t\t\t};\n\n\t\t\t\tif (Is.stringValue(propertySchema.itemType) && propertySchema.type === \"array\") {\n\t\t\t\t\tif (propertySchema.itemType === \"object\") {\n\t\t\t\t\t\tjsonPropertySchema.items = {\n\t\t\t\t\t\t\t$ref: propertySchema.itemTypeRef?.startsWith(\"http\")\n\t\t\t\t\t\t\t\t? propertySchema.itemTypeRef\n\t\t\t\t\t\t\t\t: `${domain}${propertySchema.itemTypeRef}`\n\t\t\t\t\t\t};\n\t\t\t\t\t} else {\n\t\t\t\t\t\tjsonPropertySchema.items = {\n\t\t\t\t\t\t\ttype: propertySchema.itemType\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t} else if (propertySchema.type === \"object\") {\n\t\t\t\t\tdelete jsonPropertySchema.type;\n\t\t\t\t\tjsonPropertySchema.$ref = propertySchema.itemTypeRef?.startsWith(\"http\")\n\t\t\t\t\t\t? propertySchema.itemTypeRef\n\t\t\t\t\t\t: `${domain}${propertySchema.itemTypeRef}`;\n\t\t\t\t}\n\n\t\t\t\tproperties[propertySchema.property] = jsonPropertySchema;\n\n\t\t\t\tif (!propertySchema.optional) {\n\t\t\t\t\trequired.push(propertySchema.property);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\t$schema: JsonSchemaHelper.SCHEMA_VERSION,\n\t\t\t$id: `${domain}${entitySchema?.type}`,\n\t\t\ttitle: entitySchema?.type,\n\t\t\ttype: entitySchema ? \"object\" : \"null\",\n\t\t\tdescription: entitySchema?.options?.description,\n\t\t\trequired,\n\t\t\tproperties,\n\t\t\tadditionalProperties: false\n\t\t};\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"jsonSchemaHelper.js","sourceRoot":"","sources":["../../../src/utils/jsonSchemaHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAGlD,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,OAAO,MAAM,kBAAkB,CAAC;AACvC,OAAO,OAAO,MAAM,kBAAkB,CAAC;AACvC,OAAO,aAAa,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,sBAAsB,EAAE,MAAM,wCAAwC,CAAC;AAKhF;;GAEG;AACH,MAAM,OAAO,gBAAgB;IAC5B;;OAEG;IACI,MAAM,CAAU,cAAc,GAAG,8CAA8C,CAAC;IAEvF;;OAEG;IACI,MAAM,CAAU,mBAAmB,GAAG,8CAA8C,CAAC;IAE5F;;;OAGG;IACI,MAAM,CAAU,UAAU,sBAA8B;IAE/D;;;;;;OAMG;IACI,MAAM,CAAC,KAAK,CAAC,QAAQ,CAC3B,MAAmB,EACnB,IAAO,EACP,eAA+C;QAE/C,MAAM,MAAM,GAAG;YACd,eAAe,EAAE,IAAI;YACrB,4EAA4E;YAC5E,2EAA2E;YAC3E,mDAAmD;YACnD,YAAY,EAAE,KAA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AAc,CAAC,QAAQ,EAAE,CAAC;oBAC9B,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;gBACxC,CAAC;YACF,CAAC;QACF,CAAC;QAED,OAAO;YACN,OAAO,EAAE,gBAAgB,CAAC,cAAc;YACxC,GAAG,EAAE,GAAG,MAAM,GAAG,YAAY,EAAE,IAAI,EAAE;YACrC,KAAK,EAAE,YAAY,EAAE,IAAI;YACzB,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM;YACtC,WAAW,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW;YAC/C,QAAQ;YACR,UAAU;YACV,oBAAoB,EAAE,KAAK;SAC3B,CAAC;IACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is, StringHelper } from \"@twin.org/core\";\nimport type { IEntitySchema } from \"@twin.org/entity\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { FetchHelper, HttpMethod } from \"@twin.org/web\";\nimport Ajv2019 from \"ajv/dist/2019.js\";\nimport Ajv2020 from \"ajv/dist/2020.js\";\nimport formatsPlugin from \"ajv-formats\";\nimport { DataTypeHandlerFactory } from \"../factories/dataTypeHandlerFactory.js\";\nimport type { IJsonSchema } from \"../models/IJsonSchema.js\";\nimport type { ISchemaValidationError } from \"../models/ISchemaValidationError.js\";\nimport type { ISchemaValidationResult } from \"../models/ISchemaValidationResult.js\";\n\n/**\n * A helper for JSON schemas.\n */\nexport class JsonSchemaHelper {\n\t/**\n\t * The schema version 2020 (default).\n\t */\n\tpublic static readonly SCHEMA_VERSION = \"https://json-schema.org/draft/2020-12/schema\";\n\n\t/**\n\t * The schema version 2019.\n\t */\n\tpublic static readonly SCHEMA_VERSION_2019 = \"https://json-schema.org/draft/2019-09/schema\";\n\n\t/**\n\t * The class name.\n\t * @internal\n\t */\n\tpublic static readonly CLASS_NAME = nameof<JsonSchemaHelper>();\n\n\t/**\n\t * Validates data against the schema.\n\t * @param schema The schema to validate the data with.\n\t * @param data The data to be validated.\n\t * @param additionalTypes Additional types to add for reference, not already in DataTypeHandlerFactory.\n\t * @returns Result containing errors if there are any.\n\t */\n\tpublic static async validate<T = unknown>(\n\t\tschema: IJsonSchema,\n\t\tdata: T,\n\t\tadditionalTypes?: { [id: string]: IJsonSchema }\n\t): Promise<ISchemaValidationResult> {\n\t\tconst params = {\n\t\t\tallowUnionTypes: true,\n\t\t\t// Disable strict tuples as it causes issues with the schema validation when\n\t\t\t// you have an array with fixed elements e.g. myType: [string, ...string[]]\n\t\t\t// https://github.com/ajv-validator/ajv/issues/1417\n\t\t\tstrictTuples: false,\n\t\t\tloadSchema: async (uri: string) => {\n\t\t\t\tconst subTypeHandler = DataTypeHandlerFactory.getIfExists(uri);\n\t\t\t\tconst jsonSchemaMethod = subTypeHandler?.jsonSchema?.bind(subTypeHandler);\n\t\t\t\tif (Is.function(jsonSchemaMethod)) {\n\t\t\t\t\tconst subSchema = await jsonSchemaMethod();\n\t\t\t\t\tif (Is.object<IJsonSchema>(subSchema)) {\n\t\t\t\t\t\treturn subSchema;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\t// We don't have the type in our local data types, so we try to fetch it from the web\n\t\t\t\t\tconst result = await FetchHelper.fetchJson<never, IJsonSchema>(\n\t\t\t\t\t\tJsonSchemaHelper.CLASS_NAME,\n\t\t\t\t\t\turi,\n\t\t\t\t\t\tHttpMethod.GET,\n\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t// Cache for an hour\n\t\t\t\t\t\t\tcacheTtlMs: 3600000\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\t\t\t\t\treturn result;\n\t\t\t\t} catch {\n\t\t\t\t\t// Failed to load remotely so return an empty object\n\t\t\t\t\t// so the schema validation doesn't completely fail\n\t\t\t\t\treturn {};\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\n\t\tlet ajv = new Ajv2020.Ajv2020(params);\n\t\tif (schema.$schema === JsonSchemaHelper.SCHEMA_VERSION_2019) {\n\t\t\tajv = new Ajv2019.Ajv2019({ strict: false, ...params });\n\t\t}\n\n\t\tformatsPlugin.default(ajv);\n\n\t\t// Add the additional types provided by the user\n\t\tif (Is.objectValue(additionalTypes)) {\n\t\t\tfor (const key in additionalTypes) {\n\t\t\t\tajv.addSchema(additionalTypes[key], key);\n\t\t\t}\n\t\t}\n\n\t\tconst compiled = await ajv.compileAsync(schema);\n\t\tconst result = compiled(data);\n\n\t\tconst output: ISchemaValidationResult = {\n\t\t\tresult\n\t\t};\n\n\t\tif (!output.result) {\n\t\t\toutput.error = compiled.errors as ISchemaValidationError;\n\t\t}\n\n\t\treturn output;\n\t}\n\n\t/**\n\t * Get the property type from a schema.\n\t * @param schema The schema to extract the types from.\n\t * @param propertyName The name of the property to get the type for.\n\t * @returns The types of the property.\n\t */\n\tpublic static getPropertyType(schema: IJsonSchema, propertyName: string): string | undefined {\n\t\tif (schema.type === \"object\" && Is.objectValue(schema.properties)) {\n\t\t\tconst propertySchema = schema.properties[propertyName];\n\t\t\tif (Is.object<IJsonSchema>(propertySchema)) {\n\t\t\t\tif (Is.stringValue(propertySchema.$ref)) {\n\t\t\t\t\treturn propertySchema.$ref;\n\t\t\t\t}\n\t\t\t\treturn propertySchema.type as string;\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Convert an entity schema to JSON schema e.g https://example.com/schemas/.\n\t * @param entitySchema The entity schema to convert.\n\t * @param baseDomain The base domain for local schemas e.g. https://example.com/\n\t * @returns The JSON schema for the entity.\n\t */\n\tpublic static entitySchemaToJsonSchema(\n\t\tentitySchema: IEntitySchema | undefined,\n\t\tbaseDomain?: string\n\t): IJsonSchema {\n\t\tlet domain = StringHelper.trimTrailingSlashes(baseDomain ?? \"\");\n\t\tif (domain.length > 0) {\n\t\t\tdomain += \"/\";\n\t\t}\n\n\t\tconst properties: {\n\t\t\t[key: string]: IJsonSchema;\n\t\t} = {};\n\n\t\tconst required: string[] = [];\n\n\t\tif (Is.arrayValue(entitySchema?.properties)) {\n\t\t\tfor (const propertySchema of entitySchema.properties) {\n\t\t\t\tconst jsonPropertySchema: IJsonSchema = {\n\t\t\t\t\ttype: propertySchema.type,\n\t\t\t\t\tdescription: propertySchema.description,\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\t\t\t\texamples: propertySchema.examples as unknown as any\n\t\t\t\t};\n\n\t\t\t\tif (Is.stringValue(propertySchema.itemType) && propertySchema.type === \"array\") {\n\t\t\t\t\tif (propertySchema.itemType === \"object\") {\n\t\t\t\t\t\tjsonPropertySchema.items = {\n\t\t\t\t\t\t\t$ref: propertySchema.itemTypeRef?.startsWith(\"http\")\n\t\t\t\t\t\t\t\t? propertySchema.itemTypeRef\n\t\t\t\t\t\t\t\t: `${domain}${propertySchema.itemTypeRef}`\n\t\t\t\t\t\t};\n\t\t\t\t\t} else {\n\t\t\t\t\t\tjsonPropertySchema.items = {\n\t\t\t\t\t\t\ttype: propertySchema.itemType\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t} else if (propertySchema.type === \"object\") {\n\t\t\t\t\tdelete jsonPropertySchema.type;\n\t\t\t\t\tjsonPropertySchema.$ref = propertySchema.itemTypeRef?.startsWith(\"http\")\n\t\t\t\t\t\t? propertySchema.itemTypeRef\n\t\t\t\t\t\t: `${domain}${propertySchema.itemTypeRef}`;\n\t\t\t\t}\n\n\t\t\t\tproperties[propertySchema.property] = jsonPropertySchema;\n\n\t\t\t\tif (!propertySchema.optional) {\n\t\t\t\t\trequired.push(propertySchema.property);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\t$schema: JsonSchemaHelper.SCHEMA_VERSION,\n\t\t\t$id: `${domain}${entitySchema?.type}`,\n\t\t\ttitle: entitySchema?.type,\n\t\t\ttype: entitySchema ? \"object\" : \"null\",\n\t\t\tdescription: entitySchema?.options?.description,\n\t\t\trequired,\n\t\t\tproperties,\n\t\t\tadditionalProperties: false\n\t\t};\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
|
|
8
|
+
* The namespace for the type.
|
|
9
9
|
*/
|
|
10
|
-
|
|
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.
|
|
@@ -6,9 +6,13 @@ import type { ISchemaValidationResult } from "../models/ISchemaValidationResult.
|
|
|
6
6
|
*/
|
|
7
7
|
export declare class JsonSchemaHelper {
|
|
8
8
|
/**
|
|
9
|
-
* The schema version.
|
|
9
|
+
* The schema version 2020 (default).
|
|
10
10
|
*/
|
|
11
11
|
static readonly SCHEMA_VERSION = "https://json-schema.org/draft/2020-12/schema";
|
|
12
|
+
/**
|
|
13
|
+
* The schema version 2019.
|
|
14
|
+
*/
|
|
15
|
+
static readonly SCHEMA_VERSION_2019 = "https://json-schema.org/draft/2019-09/schema";
|
|
12
16
|
/**
|
|
13
17
|
* Validates data against the schema.
|
|
14
18
|
* @param schema The schema to validate the data with.
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
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
|
+
|
|
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)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* support JSON Schema 2019 ([#31](https://github.com/twinfoundation/data/issues/31)) ([f798f72](https://github.com/twinfoundation/data/commit/f798f721c998cf50b8ba2318bec574069aad02ae))
|
|
16
|
+
|
|
3
17
|
## [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)
|
|
4
18
|
|
|
5
19
|
|
|
@@ -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`\>
|
|
@@ -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
|
|
|
@@ -4,11 +4,11 @@ Interface describing a type which can handle a specific data type.
|
|
|
4
4
|
|
|
5
5
|
## Properties
|
|
6
6
|
|
|
7
|
-
###
|
|
7
|
+
### namespace
|
|
8
8
|
|
|
9
|
-
> **
|
|
9
|
+
> **namespace**: `string`
|
|
10
10
|
|
|
11
|
-
The
|
|
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`
|