@twin.org/data-core 0.0.3-next.10 → 0.0.3-next.12
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.
|
@@ -65,7 +65,10 @@ export class JsonSchemaHelper {
|
|
|
65
65
|
if (schema.$schema === JsonSchemaHelper.SCHEMA_VERSION_2019) {
|
|
66
66
|
ajv = new Ajv2019.Ajv2019({ strict: false, ...params });
|
|
67
67
|
}
|
|
68
|
-
|
|
68
|
+
// There is an inconsistency in the types of the formats plugin,
|
|
69
|
+
// so we have to cast it to unknown and then to the correct type
|
|
70
|
+
const applyFormats = formatsPlugin.default;
|
|
71
|
+
applyFormats(ajv);
|
|
69
72
|
// Add the additional types provided by the user
|
|
70
73
|
if (Is.objectValue(additionalTypes)) {
|
|
71
74
|
for (const key in 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,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,EAC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,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 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"]}
|
|
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,KAAK;YACnB,UAAU,EAAE,KAAK,EAAE,GAAW,EAAE,EAAE;gBACjC,MAAM,cAAc,GAAG,sBAAsB,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;gBAC/D,MAAM,gBAAgB,GAAG,cAAc,EAAE,UAAU,EAAE,IAAI,CA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C;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\t// There is an inconsistency in the types of the formats plugin,\n\t\t// so we have to cast it to unknown and then to the correct type\n\t\tconst applyFormats = formatsPlugin.default as unknown as (ajvInstance: unknown) => void;\n\t\tapplyFormats(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"]}
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @twin.org/data-core - Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.3-next.12](https://github.com/twinfoundation/data/compare/data-core-v0.0.3-next.11...data-core-v0.0.3-next.12) (2026-02-25)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Miscellaneous Chores
|
|
7
|
+
|
|
8
|
+
* **data-core:** Synchronize repo versions
|
|
9
|
+
|
|
10
|
+
## [0.0.3-next.11](https://github.com/twinfoundation/data/compare/data-core-v0.0.3-next.10...data-core-v0.0.3-next.11) (2026-02-25)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* expand JsonLdHelper.getId with custom properties names ([8ec4dcf](https://github.com/twinfoundation/data/commit/8ec4dcf807a6dc416b2df2a77749f841a60be05f))
|
|
16
|
+
|
|
3
17
|
## [0.0.3-next.10](https://github.com/twinfoundation/data/compare/data-core-v0.0.3-next.9...data-core-v0.0.3-next.10) (2026-02-24)
|
|
4
18
|
|
|
5
19
|
|