@twin.org/tools-core 0.0.3-next.13 → 0.0.3-next.15

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/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # TWIN Tools Core
2
2
 
3
- Shared components for the tools.
3
+ This package provides shared utilities and models for tooling packages. It centralises common behaviours so command line apps and supporting modules can build on one dependable foundation.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  ```shell
8
- npm install @twin.org/tools-core-models
8
+ npm install @twin.org/tools-core
9
9
  ```
10
10
 
11
11
  ## Examples
@@ -59,6 +59,9 @@ export class JsonSchemaHelper {
59
59
  JsonSchemaHelper.processSchemaArray(schemaObject.allOf);
60
60
  JsonSchemaHelper.processSchemaArray(schemaObject.anyOf);
61
61
  JsonSchemaHelper.processSchemaArray(schemaObject.oneOf);
62
+ if (Is.stringValue(schemaObject.$ref)) {
63
+ schemaObject.$ref = JsonSchemaHelper.normaliseTypeName(schemaObject.$ref);
64
+ }
62
65
  }
63
66
  }
64
67
  /**
@@ -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,MAAM,gBAAgB,CAAC;AAGpC;;GAEG;AACH,MAAM,OAAO,gBAAgB;IAC5B;;OAEG;IACI,MAAM,CAAU,cAAc,GAAG,8CAA8C,CAAC;IAEvF;;;OAGG;IACI,MAAM,CAAC,aAAa,CAAC,YAAyB;QACpD,IAAI,EAAE,CAAC,MAAM,CAAc,YAAY,CAAC,EAAE,CAAC;YAC1C,0FAA0F;YAC1F,8DAA8D;YAC9D,wDAAwD;YACxD,4CAA4C;YAC5C,mCAAmC;YACnC,4DAA4D;YAC5D,kEAAkE;YAClE,IAAI,EAAE,CAAC,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;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AAoB,CAAC,EAAE,CAAC;gBAC5C,gBAAgB,CAAC,iBAAiB,CAAC,UAAU,EAAE,MAAM,CAAC,oBAAoB,EAAE,aAAa,CAAC,CAAC;YAC5F,CAAC;QACF,CAAC;aAAM,IAAI,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACxC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;gBACjC,IAAI,EAAE,CAAC,MAAM,CAAc,IAAI,CAAC,EAAE,CAAC;oBAClC,gBAAgB,CAAC,iBAAiB,CAAC,UAAU,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC;gBACrE,CAAC;YACF,CAAC;QACF,CAAC;aAAM,IAAI,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACxC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;gBACjC,IAAI,EAAE,CAAC,MAAM,CAAc,IAAI,CAAC,EAAE,CAAC;oBAClC,gBAAgB,CAAC,iBAAiB,CAAC,UAAU,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC;gBACrE,CAAC;YACF,CAAC;QACF,CAAC;IACF,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,aAAa,CAAC,YAAoB;QAC/C,OAAO,YAAY,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC;YAChE,CAAC,CAAC,IAAI,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YACvC,CAAC,CAAC,IAAI,MAAM,CAAC,IAAI,YAAY,GAAG,CAAC,CAAC;IACpC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is } from \"@twin.org/core\";\nimport type { IJsonSchema } from \"../models/IJsonSchema.js\";\n\n/**\n * Helper class for JSON Schema processing.\n */\nexport class JsonSchemaHelper {\n\t/**\n\t * The JSON Schema version used.\n\t */\n\tpublic static readonly SCHEMA_VERSION = \"https://json-schema.org/draft/2020-12/schema\";\n\n\t/**\n\t * Process arrays in the schema object.\n\t * @param schemaObject The schema object to process.\n\t */\n\tpublic static processArrays(schemaObject: IJsonSchema): void {\n\t\tif (Is.object<IJsonSchema>(schemaObject)) {\n\t\t\t// new json schema version has prefixItems instead of items for arrays with fixed ordering\n\t\t\t// so convert any old style schemas that use items as an array\n\t\t\t// prefixItems validates fixed positions and is an array\n\t\t\t// items validates the rest and is an object\n\t\t\t// additionalItems no longer exists\n\t\t\t// https://www.learnjsonschema.com/2020-12/applicator/items/\n\t\t\t// https://www.learnjsonschema.com/2020-12/applicator/prefixitems/\n\t\t\tif (Is.array(schemaObject.items)) {\n\t\t\t\tif (Is.object(schemaObject.additionalItems)) {\n\t\t\t\t\t// If the items are an array then this is fixed ordering\n\t\t\t\t\t// so move to prefixItems and then do the same for additionalItems\n\t\t\t\t\tschemaObject.prefixItems = schemaObject.items;\n\t\t\t\t\tdelete schemaObject.items;\n\t\t\t\t\tschemaObject.items = schemaObject.additionalItems;\n\t\t\t\t} else if (\n\t\t\t\t\tIs.integer(schemaObject.minItems) &&\n\t\t\t\t\tIs.integer(schemaObject.maxItems) &&\n\t\t\t\t\tschemaObject.minItems === schemaObject.maxItems &&\n\t\t\t\t\tschemaObject.maxItems === schemaObject.items.length\n\t\t\t\t) {\n\t\t\t\t\t// There is a fixed number of items which matches the items array length\n\t\t\t\t\t// so move construct an allOf to enforce the fixed length\n\t\t\t\t\tschemaObject.items = { allOf: schemaObject.items };\n\t\t\t\t\tdelete schemaObject.minItems;\n\t\t\t\t\tdelete schemaObject.maxItems;\n\t\t\t\t} else {\n\t\t\t\t\t// no additional items so wrap in an anyOf\n\t\t\t\t\tschemaObject.items = { anyOf: schemaObject.items };\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// We can't use additionalItems in the new schema version\n\t\t\t// so just remove it after processing\n\t\t\tdelete schemaObject.additionalItems;\n\n\t\t\t// Since we are generating JSON-LD schemas we need to support\n\t\t\t// additional properties to allow for object expansion\n\t\t\t// but don't change anything that has specific additional properties defined\n\t\t\tif (schemaObject.additionalProperties === false) {\n\t\t\t\tdelete schemaObject.additionalProperties;\n\t\t\t}\n\n\t\t\tJsonSchemaHelper.processSchemaDictionary(schemaObject.properties);\n\t\t\tJsonSchemaHelper.processArrays(schemaObject.additionalProperties);\n\t\t\tJsonSchemaHelper.processSchemaArray(schemaObject.allOf);\n\t\t\tJsonSchemaHelper.processSchemaArray(schemaObject.anyOf);\n\t\t\tJsonSchemaHelper.processSchemaArray(schemaObject.oneOf);\n\t\t}\n\t}\n\n\t/**\n\t * Process arrays in the schema object.\n\t * @param schemaDictionary The schema object to process.\n\t */\n\tpublic static processSchemaDictionary(schemaDictionary?: { [key: string]: IJsonSchema }): void {\n\t\tif (Is.object(schemaDictionary)) {\n\t\t\tfor (const item of Object.values(schemaDictionary)) {\n\t\t\t\tif (Is.object<IJsonSchema>(item)) {\n\t\t\t\t\tJsonSchemaHelper.processArrays(item);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Process arrays in the schema object.\n\t * @param schemaArray The schema object to process.\n\t */\n\tpublic static processSchemaArray(schemaArray?: IJsonSchema[]): void {\n\t\tif (Is.arrayValue(schemaArray)) {\n\t\t\tconst seenSignatures = new Set<string>();\n\t\t\tfor (const item of schemaArray) {\n\t\t\t\tif (Is.object<IJsonSchema>(item)) {\n\t\t\t\t\tconst signature = JSON.stringify(item);\n\t\t\t\t\t// There is a bug in JSON schema generator where spread tuple produce the same output\n\t\t\t\t\t// e.g. [typeA, ...typeB] and [...typeA, typeB] can produce the same items schema\n\t\t\t\t\t// in the legacy shape { items: { typeA }, additionalItems: [typeB] }.\n\t\t\t\t\t// For duplicate signatures we normalize one entry to the alternate approximation\n\t\t\t\t\t// so we don't emit identical duplicated anyOf/oneOf branches.\n\t\t\t\t\tif (\n\t\t\t\t\t\tseenSignatures.has(signature) &&\n\t\t\t\t\t\tIs.array(item.items) &&\n\t\t\t\t\t\tIs.object(item.additionalItems)\n\t\t\t\t\t) {\n\t\t\t\t\t\tconst originalItems = item.items;\n\t\t\t\t\t\titem.items = [item.additionalItems];\n\t\t\t\t\t\titem.additionalItems = originalItems[0];\n\t\t\t\t\t}\n\t\t\t\t\tseenSignatures.add(signature);\n\t\t\t\t\tJsonSchemaHelper.processArrays(item);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Cleanup TypeScript markers from the type name.\n\t * @param typeName The definition string to clean up.\n\t * @returns The cleaned up definition string.\n\t */\n\tpublic static normaliseTypeName(typeName: string): string {\n\t\t// Remove the partial markers\n\t\tlet sTypeName = typeName.replace(/^Partial<I(.*?)>/g, \"$1\");\n\t\tsTypeName = sTypeName.replace(/Partial%3CI(.*?)%3E/g, \"$1\");\n\n\t\t// Remove the omit markers\n\t\tsTypeName = sTypeName.replace(/^Omit<I(.*?),.*>/g, \"$1\");\n\t\tsTypeName = sTypeName.replace(/Omit%3CI(.*?)%2C.*%3E/g, \"$1\");\n\n\t\t// Remove the pick markers\n\t\tsTypeName = sTypeName.replace(/^Pick<I(.*?),.*>/g, \"$1\");\n\t\tsTypeName = sTypeName.replace(/Pick%3CI(.*?)%2C.*%3E/g, \"$1\");\n\n\t\t// Cleanup the generic markers\n\t\tsTypeName = sTypeName.replace(/^(.*?)<I(.*?)>/g, \"$1<$2>\");\n\t\tsTypeName = sTypeName.replace(/(.*?)%3CI(.*?)%3E/g, \"$1<$2>\");\n\n\t\t// Cleanup the unknown markers\n\t\tsTypeName = sTypeName.replace(/<unknown>/g, \"\");\n\t\tsTypeName = sTypeName.replace(/%3Cunknown%3E/g, \"\");\n\n\t\t// Replace the other url markers\n\t\tsTypeName = sTypeName.replace(/%7C/g, \"|\").replace(/%3C/g, \"<\").replace(/%3E/g, \">\");\n\n\t\treturn sTypeName;\n\t}\n\n\t/**\n\t * Extract type from properties definition.\n\t * @param allTypes All the known types.\n\t * @param schema The schema to extract from.\n\t * @param output The output types.\n\t */\n\tpublic static extractTypesFromSchema(\n\t\tallTypes: { [id: string]: IJsonSchema },\n\t\tschema: IJsonSchema,\n\t\toutput: { [id: string]: IJsonSchema }\n\t): void {\n\t\tconst additionalTypes = [];\n\n\t\tif (Is.stringValue(schema.$ref)) {\n\t\t\tadditionalTypes.push(\n\t\t\t\tJsonSchemaHelper.normaliseTypeName(schema.$ref.replace(\"#/definitions/\", \"\"))\n\t\t\t);\n\t\t} else if (Is.object<IJsonSchema>(schema.items)) {\n\t\t\tif (Is.arrayValue<IJsonSchema>(schema.items)) {\n\t\t\t\tfor (const itemSchema of schema.items) {\n\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, itemSchema, output);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, schema.items, output);\n\t\t\t}\n\t\t} else if (Is.object(schema.properties) || Is.object(schema.additionalProperties)) {\n\t\t\tif (Is.object(schema.properties)) {\n\t\t\t\tfor (const prop in schema.properties) {\n\t\t\t\t\tconst p = schema.properties[prop];\n\t\t\t\t\tif (Is.object<IJsonSchema>(p)) {\n\t\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, p, output);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (Is.object(schema.additionalProperties)) {\n\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, schema.additionalProperties, output);\n\t\t\t}\n\t\t} else if (Is.arrayValue(schema.anyOf)) {\n\t\t\tfor (const prop of schema.anyOf) {\n\t\t\t\tif (Is.object<IJsonSchema>(prop)) {\n\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, prop, output);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (Is.arrayValue(schema.oneOf)) {\n\t\t\tfor (const prop of schema.oneOf) {\n\t\t\t\tif (Is.object<IJsonSchema>(prop)) {\n\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, prop, output);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (additionalTypes.length > 0) {\n\t\t\tJsonSchemaHelper.extractTypes(allTypes, additionalTypes, output);\n\t\t}\n\t}\n\n\t/**\n\t * Extract the required types from all the known schemas.\n\t * @param allSchemas All the known schemas.\n\t * @param requiredTypes The required types.\n\t * @param referencedSchemas The references schemas.\n\t */\n\tpublic static extractTypes(\n\t\tallSchemas: { [id: string]: IJsonSchema },\n\t\trequiredTypes: string[],\n\t\treferencedSchemas: { [id: string]: IJsonSchema }\n\t): void {\n\t\tfor (const typeKey of Object.keys(allSchemas)) {\n\t\t\tif (!referencedSchemas[typeKey]) {\n\t\t\t\tfor (const requiredType of requiredTypes) {\n\t\t\t\t\tconst regex = JsonSchemaHelper.stringToRegEx(requiredType);\n\n\t\t\t\t\tif (regex.test(typeKey)) {\n\t\t\t\t\t\treferencedSchemas[typeKey] = allSchemas[typeKey];\n\t\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(\n\t\t\t\t\t\t\tallSchemas,\n\t\t\t\t\t\t\tallSchemas[typeKey],\n\t\t\t\t\t\t\treferencedSchemas\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Expand the types inline.\n\t * @param schemas The schemas to include the expanded types.\n\t * @param expandedTypes The types to expand.\n\t */\n\tpublic static expandTypes(schemas: { [id: string]: IJsonSchema }, expandedTypes: string[]): void {\n\t\tfor (const typeKey of Object.keys(schemas)) {\n\t\t\tconst schema = schemas[typeKey];\n\n\t\t\tJsonSchemaHelper.expandSchemaTypes(schemas, schema, expandedTypes);\n\t\t}\n\t}\n\n\t/**\n\t * Expand the types inline.\n\t * @param allSchemas All the known schemas.\n\t * @param schema The schemas to include the expanded types.\n\t * @param expandedTypes The types to expand.\n\t */\n\tpublic static expandSchemaTypes(\n\t\tallSchemas: { [id: string]: IJsonSchema },\n\t\tschema: IJsonSchema,\n\t\texpandedTypes: string[]\n\t): void {\n\t\tif (Is.stringValue(schema.$ref)) {\n\t\t\tfor (const expandedType of expandedTypes) {\n\t\t\t\tconst typeName = JsonSchemaHelper.normaliseTypeName(\n\t\t\t\t\tschema.$ref.replace(\"#/definitions/\", \"\")\n\t\t\t\t);\n\t\t\t\tconst regex = JsonSchemaHelper.stringToRegEx(expandedType);\n\t\t\t\tif (regex.test(typeName) && allSchemas[typeName]) {\n\t\t\t\t\tdelete schema.$ref;\n\t\t\t\t\tObject.assign(schema, allSchemas[typeName]);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (Is.object<IJsonSchema>(schema.items)) {\n\t\t\tif (Is.arrayValue<IJsonSchema>(schema.items)) {\n\t\t\t\tfor (const itemSchema of schema.items) {\n\t\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, itemSchema, expandedTypes);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, schema.items, expandedTypes);\n\t\t\t}\n\t\t} else if (Is.object(schema.properties) || Is.object(schema.additionalProperties)) {\n\t\t\tif (Is.object(schema.properties)) {\n\t\t\t\tfor (const prop in schema.properties) {\n\t\t\t\t\tconst p = schema.properties[prop];\n\t\t\t\t\tif (Is.object<IJsonSchema>(p)) {\n\t\t\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, p, expandedTypes);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (Is.object(schema.additionalProperties)) {\n\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, schema.additionalProperties, expandedTypes);\n\t\t\t}\n\t\t} else if (Is.arrayValue(schema.anyOf)) {\n\t\t\tfor (const prop of schema.anyOf) {\n\t\t\t\tif (Is.object<IJsonSchema>(prop)) {\n\t\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, prop, expandedTypes);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (Is.arrayValue(schema.oneOf)) {\n\t\t\tfor (const prop of schema.oneOf) {\n\t\t\t\tif (Is.object<IJsonSchema>(prop)) {\n\t\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, prop, expandedTypes);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Convert a string pattern to a regular expression.\n\t * @param matchPattern The pattern to convert.\n\t * @returns The regular expression.\n\t */\n\tpublic static stringToRegEx(matchPattern: string): RegExp {\n\t\treturn matchPattern.startsWith(\"/\") && matchPattern.endsWith(\"/\")\n\t\t\t? new RegExp(matchPattern.slice(1, -1))\n\t\t\t: new RegExp(`^${matchPattern}$`);\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,MAAM,gBAAgB,CAAC;AAGpC;;GAEG;AACH,MAAM,OAAO,gBAAgB;IAC5B;;OAEG;IACI,MAAM,CAAU,cAAc,GAAG,8CAA8C,CAAC;IAEvF;;;OAGG;IACI,MAAM,CAAC,aAAa,CAAC,YAAyB;QACpD,IAAI,EAAE,CAAC,MAAM,CAAc,YAAY,CAAC,EAAE,CAAC;YAC1C,0FAA0F;YAC1F,8DAA8D;YAC9D,wDAAwD;YACxD,4CAA4C;YAC5C,mCAAmC;YACnC,4DAA4D;YAC5D,kEAAkE;YAClE,IAAI,EAAE,CAAC,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;gBAClC,IAAI,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,eAAe,CAAC,EAAE,CAAC;oBAC7C,wDAAwD;oBACxD,kEAAkE;oBAClE,YAAY,CAAC,WAAW,GAAG,YAAY,CAAC,KAAK,CAAC;oBAC9C,OAAO,YAAY,CAAC,KAA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,CAAC;gBAC5C,gBAAgB,CAAC,iBAAiB,CAAC,UAAU,EAAE,MAAM,CAAC,oBAAoB,EAAE,aAAa,CAAC,CAAC;YAC5F,CAAC;QACF,CAAC;aAAM,IAAI,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACxC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;gBACjC,IAAI,EAAE,CAAC,MAAM,CAAc,IAAI,CAAC,EAAE,CAAC;oBAClC,gBAAgB,CAAC,iBAAiB,CAAC,UAAU,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC;gBACrE,CAAC;YACF,CAAC;QACF,CAAC;aAAM,IAAI,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACxC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;gBACjC,IAAI,EAAE,CAAC,MAAM,CAAc,IAAI,CAAC,EAAE,CAAC;oBAClC,gBAAgB,CAAC,iBAAiB,CAAC,UAAU,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC;gBACrE,CAAC;YACF,CAAC;QACF,CAAC;IACF,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,aAAa,CAAC,YAAoB;QAC/C,OAAO,YAAY,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC;YAChE,CAAC,CAAC,IAAI,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YACvC,CAAC,CAAC,IAAI,MAAM,CAAC,IAAI,YAAY,GAAG,CAAC,CAAC;IACpC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is } from \"@twin.org/core\";\nimport type { IJsonSchema } from \"../models/IJsonSchema.js\";\n\n/**\n * Helper class for JSON Schema processing.\n */\nexport class JsonSchemaHelper {\n\t/**\n\t * The JSON Schema version used.\n\t */\n\tpublic static readonly SCHEMA_VERSION = \"https://json-schema.org/draft/2020-12/schema\";\n\n\t/**\n\t * Process arrays in the schema object.\n\t * @param schemaObject The schema object to process.\n\t */\n\tpublic static processArrays(schemaObject: IJsonSchema): void {\n\t\tif (Is.object<IJsonSchema>(schemaObject)) {\n\t\t\t// new json schema version has prefixItems instead of items for arrays with fixed ordering\n\t\t\t// so convert any old style schemas that use items as an array\n\t\t\t// prefixItems validates fixed positions and is an array\n\t\t\t// items validates the rest and is an object\n\t\t\t// additionalItems no longer exists\n\t\t\t// https://www.learnjsonschema.com/2020-12/applicator/items/\n\t\t\t// https://www.learnjsonschema.com/2020-12/applicator/prefixitems/\n\t\t\tif (Is.array(schemaObject.items)) {\n\t\t\t\tif (Is.object(schemaObject.additionalItems)) {\n\t\t\t\t\t// If the items are an array then this is fixed ordering\n\t\t\t\t\t// so move to prefixItems and then do the same for additionalItems\n\t\t\t\t\tschemaObject.prefixItems = schemaObject.items;\n\t\t\t\t\tdelete schemaObject.items;\n\t\t\t\t\tschemaObject.items = schemaObject.additionalItems;\n\t\t\t\t} else if (\n\t\t\t\t\tIs.integer(schemaObject.minItems) &&\n\t\t\t\t\tIs.integer(schemaObject.maxItems) &&\n\t\t\t\t\tschemaObject.minItems === schemaObject.maxItems &&\n\t\t\t\t\tschemaObject.maxItems === schemaObject.items.length\n\t\t\t\t) {\n\t\t\t\t\t// There is a fixed number of items which matches the items array length\n\t\t\t\t\t// so move construct an allOf to enforce the fixed length\n\t\t\t\t\tschemaObject.items = { allOf: schemaObject.items };\n\t\t\t\t\tdelete schemaObject.minItems;\n\t\t\t\t\tdelete schemaObject.maxItems;\n\t\t\t\t} else {\n\t\t\t\t\t// no additional items so wrap in an anyOf\n\t\t\t\t\tschemaObject.items = { anyOf: schemaObject.items };\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// We can't use additionalItems in the new schema version\n\t\t\t// so just remove it after processing\n\t\t\tdelete schemaObject.additionalItems;\n\n\t\t\t// Since we are generating JSON-LD schemas we need to support\n\t\t\t// additional properties to allow for object expansion\n\t\t\t// but don't change anything that has specific additional properties defined\n\t\t\tif (schemaObject.additionalProperties === false) {\n\t\t\t\tdelete schemaObject.additionalProperties;\n\t\t\t}\n\n\t\t\tJsonSchemaHelper.processSchemaDictionary(schemaObject.properties);\n\t\t\tJsonSchemaHelper.processArrays(schemaObject.additionalProperties);\n\t\t\tJsonSchemaHelper.processSchemaArray(schemaObject.allOf);\n\t\t\tJsonSchemaHelper.processSchemaArray(schemaObject.anyOf);\n\t\t\tJsonSchemaHelper.processSchemaArray(schemaObject.oneOf);\n\n\t\t\tif (Is.stringValue(schemaObject.$ref)) {\n\t\t\t\tschemaObject.$ref = JsonSchemaHelper.normaliseTypeName(schemaObject.$ref);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Process arrays in the schema object.\n\t * @param schemaDictionary The schema object to process.\n\t */\n\tpublic static processSchemaDictionary(schemaDictionary?: { [key: string]: IJsonSchema }): void {\n\t\tif (Is.object(schemaDictionary)) {\n\t\t\tfor (const item of Object.values(schemaDictionary)) {\n\t\t\t\tif (Is.object<IJsonSchema>(item)) {\n\t\t\t\t\tJsonSchemaHelper.processArrays(item);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Process arrays in the schema object.\n\t * @param schemaArray The schema object to process.\n\t */\n\tpublic static processSchemaArray(schemaArray?: IJsonSchema[]): void {\n\t\tif (Is.arrayValue(schemaArray)) {\n\t\t\tconst seenSignatures = new Set<string>();\n\t\t\tfor (const item of schemaArray) {\n\t\t\t\tif (Is.object<IJsonSchema>(item)) {\n\t\t\t\t\tconst signature = JSON.stringify(item);\n\t\t\t\t\t// There is a bug in JSON schema generator where spread tuple produce the same output\n\t\t\t\t\t// e.g. [typeA, ...typeB] and [...typeA, typeB] can produce the same items schema\n\t\t\t\t\t// in the legacy shape { items: { typeA }, additionalItems: [typeB] }.\n\t\t\t\t\t// For duplicate signatures we normalize one entry to the alternate approximation\n\t\t\t\t\t// so we don't emit identical duplicated anyOf/oneOf branches.\n\t\t\t\t\tif (\n\t\t\t\t\t\tseenSignatures.has(signature) &&\n\t\t\t\t\t\tIs.array(item.items) &&\n\t\t\t\t\t\tIs.object(item.additionalItems)\n\t\t\t\t\t) {\n\t\t\t\t\t\tconst originalItems = item.items;\n\t\t\t\t\t\titem.items = [item.additionalItems];\n\t\t\t\t\t\titem.additionalItems = originalItems[0];\n\t\t\t\t\t}\n\t\t\t\t\tseenSignatures.add(signature);\n\t\t\t\t\tJsonSchemaHelper.processArrays(item);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Cleanup TypeScript markers from the type name.\n\t * @param typeName The definition string to clean up.\n\t * @returns The cleaned up definition string.\n\t */\n\tpublic static normaliseTypeName(typeName: string): string {\n\t\t// Remove the partial markers\n\t\tlet sTypeName = typeName.replace(/^Partial<I(.*?)>/g, \"$1\");\n\t\tsTypeName = sTypeName.replace(/Partial%3CI(.*?)%3E/g, \"$1\");\n\n\t\t// Remove the omit markers\n\t\tsTypeName = sTypeName.replace(/^Omit<I(.*?),.*>/g, \"$1\");\n\t\tsTypeName = sTypeName.replace(/Omit%3CI(.*?)%2C.*%3E/g, \"$1\");\n\n\t\t// Remove the pick markers\n\t\tsTypeName = sTypeName.replace(/^Pick<I(.*?),.*>/g, \"$1\");\n\t\tsTypeName = sTypeName.replace(/Pick%3CI(.*?)%2C.*%3E/g, \"$1\");\n\n\t\t// Cleanup the generic markers\n\t\tsTypeName = sTypeName.replace(/^(.*?)<I(.*?)>/g, \"$1<$2>\");\n\t\tsTypeName = sTypeName.replace(/(.*?)%3CI(.*?)%3E/g, \"$1<$2>\");\n\n\t\t// Cleanup the unknown markers\n\t\tsTypeName = sTypeName.replace(/<unknown>/g, \"\");\n\t\tsTypeName = sTypeName.replace(/%3Cunknown%3E/g, \"\");\n\n\t\t// Replace the other url markers\n\t\tsTypeName = sTypeName.replace(/%7C/g, \"|\").replace(/%3C/g, \"<\").replace(/%3E/g, \">\");\n\n\t\treturn sTypeName;\n\t}\n\n\t/**\n\t * Extract type from properties definition.\n\t * @param allTypes All the known types.\n\t * @param schema The schema to extract from.\n\t * @param output The output types.\n\t */\n\tpublic static extractTypesFromSchema(\n\t\tallTypes: { [id: string]: IJsonSchema },\n\t\tschema: IJsonSchema,\n\t\toutput: { [id: string]: IJsonSchema }\n\t): void {\n\t\tconst additionalTypes = [];\n\n\t\tif (Is.stringValue(schema.$ref)) {\n\t\t\tadditionalTypes.push(\n\t\t\t\tJsonSchemaHelper.normaliseTypeName(schema.$ref.replace(\"#/definitions/\", \"\"))\n\t\t\t);\n\t\t} else if (Is.object<IJsonSchema>(schema.items)) {\n\t\t\tif (Is.arrayValue<IJsonSchema>(schema.items)) {\n\t\t\t\tfor (const itemSchema of schema.items) {\n\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, itemSchema, output);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, schema.items, output);\n\t\t\t}\n\t\t} else if (Is.object(schema.properties) || Is.object(schema.additionalProperties)) {\n\t\t\tif (Is.object(schema.properties)) {\n\t\t\t\tfor (const prop in schema.properties) {\n\t\t\t\t\tconst p = schema.properties[prop];\n\t\t\t\t\tif (Is.object<IJsonSchema>(p)) {\n\t\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, p, output);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (Is.object(schema.additionalProperties)) {\n\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, schema.additionalProperties, output);\n\t\t\t}\n\t\t} else if (Is.arrayValue(schema.anyOf)) {\n\t\t\tfor (const prop of schema.anyOf) {\n\t\t\t\tif (Is.object<IJsonSchema>(prop)) {\n\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, prop, output);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (Is.arrayValue(schema.oneOf)) {\n\t\t\tfor (const prop of schema.oneOf) {\n\t\t\t\tif (Is.object<IJsonSchema>(prop)) {\n\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, prop, output);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (additionalTypes.length > 0) {\n\t\t\tJsonSchemaHelper.extractTypes(allTypes, additionalTypes, output);\n\t\t}\n\t}\n\n\t/**\n\t * Extract the required types from all the known schemas.\n\t * @param allSchemas All the known schemas.\n\t * @param requiredTypes The required types.\n\t * @param referencedSchemas The references schemas.\n\t */\n\tpublic static extractTypes(\n\t\tallSchemas: { [id: string]: IJsonSchema },\n\t\trequiredTypes: string[],\n\t\treferencedSchemas: { [id: string]: IJsonSchema }\n\t): void {\n\t\tfor (const typeKey of Object.keys(allSchemas)) {\n\t\t\tif (!referencedSchemas[typeKey]) {\n\t\t\t\tfor (const requiredType of requiredTypes) {\n\t\t\t\t\tconst regex = JsonSchemaHelper.stringToRegEx(requiredType);\n\n\t\t\t\t\tif (regex.test(typeKey)) {\n\t\t\t\t\t\treferencedSchemas[typeKey] = allSchemas[typeKey];\n\t\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(\n\t\t\t\t\t\t\tallSchemas,\n\t\t\t\t\t\t\tallSchemas[typeKey],\n\t\t\t\t\t\t\treferencedSchemas\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Expand the types inline.\n\t * @param schemas The schemas to include the expanded types.\n\t * @param expandedTypes The types to expand.\n\t */\n\tpublic static expandTypes(schemas: { [id: string]: IJsonSchema }, expandedTypes: string[]): void {\n\t\tfor (const typeKey of Object.keys(schemas)) {\n\t\t\tconst schema = schemas[typeKey];\n\n\t\t\tJsonSchemaHelper.expandSchemaTypes(schemas, schema, expandedTypes);\n\t\t}\n\t}\n\n\t/**\n\t * Expand the types inline.\n\t * @param allSchemas All the known schemas.\n\t * @param schema The schemas to include the expanded types.\n\t * @param expandedTypes The types to expand.\n\t */\n\tpublic static expandSchemaTypes(\n\t\tallSchemas: { [id: string]: IJsonSchema },\n\t\tschema: IJsonSchema,\n\t\texpandedTypes: string[]\n\t): void {\n\t\tif (Is.stringValue(schema.$ref)) {\n\t\t\tfor (const expandedType of expandedTypes) {\n\t\t\t\tconst typeName = JsonSchemaHelper.normaliseTypeName(\n\t\t\t\t\tschema.$ref.replace(\"#/definitions/\", \"\")\n\t\t\t\t);\n\t\t\t\tconst regex = JsonSchemaHelper.stringToRegEx(expandedType);\n\t\t\t\tif (regex.test(typeName) && allSchemas[typeName]) {\n\t\t\t\t\tdelete schema.$ref;\n\t\t\t\t\tObject.assign(schema, allSchemas[typeName]);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (Is.object<IJsonSchema>(schema.items)) {\n\t\t\tif (Is.arrayValue<IJsonSchema>(schema.items)) {\n\t\t\t\tfor (const itemSchema of schema.items) {\n\t\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, itemSchema, expandedTypes);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, schema.items, expandedTypes);\n\t\t\t}\n\t\t} else if (Is.object(schema.properties) || Is.object(schema.additionalProperties)) {\n\t\t\tif (Is.object(schema.properties)) {\n\t\t\t\tfor (const prop in schema.properties) {\n\t\t\t\t\tconst p = schema.properties[prop];\n\t\t\t\t\tif (Is.object<IJsonSchema>(p)) {\n\t\t\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, p, expandedTypes);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (Is.object(schema.additionalProperties)) {\n\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, schema.additionalProperties, expandedTypes);\n\t\t\t}\n\t\t} else if (Is.arrayValue(schema.anyOf)) {\n\t\t\tfor (const prop of schema.anyOf) {\n\t\t\t\tif (Is.object<IJsonSchema>(prop)) {\n\t\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, prop, expandedTypes);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (Is.arrayValue(schema.oneOf)) {\n\t\t\tfor (const prop of schema.oneOf) {\n\t\t\t\tif (Is.object<IJsonSchema>(prop)) {\n\t\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, prop, expandedTypes);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Convert a string pattern to a regular expression.\n\t * @param matchPattern The pattern to convert.\n\t * @returns The regular expression.\n\t */\n\tpublic static stringToRegEx(matchPattern: string): RegExp {\n\t\treturn matchPattern.startsWith(\"/\") && matchPattern.endsWith(\"/\")\n\t\t\t? new RegExp(matchPattern.slice(1, -1))\n\t\t\t: new RegExp(`^${matchPattern}$`);\n\t}\n}\n"]}
package/docs/changelog.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.0.3-next.15](https://github.com/twinfoundation/tools/compare/tools-core-v0.0.3-next.14...tools-core-v0.0.3-next.15) (2026-03-10)
4
+
5
+
6
+ ### Features
7
+
8
+ * use jsdoc tags for json-ld markup ([1ea872e](https://github.com/twinfoundation/tools/commit/1ea872e07a1cc0e94178158a57383d64008e02e3))
9
+
10
+ ## [0.0.3-next.14](https://github.com/twinfoundation/tools/compare/tools-core-v0.0.3-next.13...tools-core-v0.0.3-next.14) (2026-03-06)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * improve schema output ([9e53cbe](https://github.com/twinfoundation/tools/commit/9e53cbe26e6272323d72c65e277b7d24521fdbba))
16
+
3
17
  ## [0.0.3-next.13](https://github.com/twinfoundation/tools/compare/tools-core-v0.0.3-next.12...tools-core-v0.0.3-next.13) (2026-03-05)
4
18
 
5
19
 
package/docs/examples.md CHANGED
@@ -1 +1,180 @@
1
- # @twin.org/tools-core - Examples
1
+ # Tools Core Examples
2
+
3
+ Use these helpers to normalise generated schemas and keep references consistent before writing documents.
4
+
5
+ ## JsonSchemaHelper
6
+
7
+ ```typescript
8
+ import { JsonSchemaHelper } from '@twin.org/tools-core';
9
+
10
+ console.log(JsonSchemaHelper.SCHEMA_VERSION); // "https://json-schema.org/draft/2020-12/schema"
11
+ ```
12
+
13
+ ```typescript
14
+ import type { IJsonSchema } from '@twin.org/tools-core';
15
+ import { JsonSchemaHelper } from '@twin.org/tools-core';
16
+
17
+ const schemaObject: IJsonSchema = {
18
+ type: 'array',
19
+ items: [{ type: 'string' }, { type: 'number' }],
20
+ additionalItems: { type: 'boolean' }
21
+ };
22
+
23
+ JsonSchemaHelper.processArrays(schemaObject);
24
+
25
+ console.log(schemaObject.prefixItems); // [{ type: "string" }, { type: "number" }]
26
+
27
+ console.log(schemaObject.items); // { type: "boolean" }
28
+ ```
29
+
30
+ ```typescript
31
+ import type { IJsonSchema } from '@twin.org/tools-core';
32
+ import { JsonSchemaHelper } from '@twin.org/tools-core';
33
+
34
+ const schemaDictionary: { [key: string]: IJsonSchema } = {
35
+ id: { $ref: '#/definitions/Partial%3CIOrder%3E' },
36
+ amount: { type: 'number' }
37
+ };
38
+
39
+ JsonSchemaHelper.processSchemaDictionary(schemaDictionary);
40
+
41
+ console.log(schemaDictionary.id.$ref); // "Order"
42
+ ```
43
+
44
+ ```typescript
45
+ import type { IJsonSchema } from '@twin.org/tools-core';
46
+ import { JsonSchemaHelper } from '@twin.org/tools-core';
47
+
48
+ const schemaArray: IJsonSchema[] = [
49
+ {
50
+ items: [{ type: 'string' }],
51
+ additionalItems: { type: 'number' }
52
+ },
53
+ {
54
+ items: [{ type: 'string' }],
55
+ additionalItems: { type: 'number' }
56
+ }
57
+ ];
58
+
59
+ JsonSchemaHelper.processSchemaArray(schemaArray);
60
+
61
+ console.log(schemaArray[0].prefixItems); // [{ type: "string" }]
62
+ ```
63
+
64
+ ```typescript
65
+ import { JsonSchemaHelper } from '@twin.org/tools-core';
66
+
67
+ console.log(JsonSchemaHelper.normaliseTypeName('#/definitions/Pick%3CIOrder%2C%22id%22%3E')); // "#/definitions/Order"
68
+ ```
69
+
70
+ ```typescript
71
+ import type { IJsonSchema } from '@twin.org/tools-core';
72
+ import { JsonSchemaHelper } from '@twin.org/tools-core';
73
+
74
+ const allSchemas: { [id: string]: IJsonSchema } = {
75
+ Order: {
76
+ type: 'object',
77
+ properties: {
78
+ customer: { $ref: '#/definitions/Customer' }
79
+ }
80
+ },
81
+ Customer: {
82
+ type: 'object',
83
+ properties: {
84
+ name: { type: 'string' }
85
+ }
86
+ }
87
+ };
88
+
89
+ const extracted: { [id: string]: IJsonSchema } = {};
90
+
91
+ JsonSchemaHelper.extractTypesFromSchema(allSchemas, allSchemas.Order, extracted);
92
+
93
+ console.log(Object.keys(extracted)); // ["Customer"]
94
+ ```
95
+
96
+ ```typescript
97
+ import type { IJsonSchema } from '@twin.org/tools-core';
98
+ import { JsonSchemaHelper } from '@twin.org/tools-core';
99
+
100
+ const allSchemas: { [id: string]: IJsonSchema } = {
101
+ Order: {
102
+ type: 'object',
103
+ properties: {
104
+ customer: { $ref: '#/definitions/Customer' }
105
+ }
106
+ },
107
+ Customer: {
108
+ type: 'object',
109
+ properties: {
110
+ name: { type: 'string' }
111
+ }
112
+ }
113
+ };
114
+
115
+ const referencedSchemas: { [id: string]: IJsonSchema } = {};
116
+
117
+ JsonSchemaHelper.extractTypes(allSchemas, ['Order'], referencedSchemas);
118
+
119
+ console.log(Object.keys(referencedSchemas)); // ["Order", "Customer"]
120
+ ```
121
+
122
+ ```typescript
123
+ import type { IJsonSchema } from '@twin.org/tools-core';
124
+ import { JsonSchemaHelper } from '@twin.org/tools-core';
125
+
126
+ const schemas: { [id: string]: IJsonSchema } = {
127
+ Order: {
128
+ type: 'object',
129
+ properties: {
130
+ customer: { $ref: '#/definitions/Customer' }
131
+ }
132
+ },
133
+ Customer: {
134
+ type: 'object',
135
+ properties: {
136
+ name: { type: 'string' }
137
+ }
138
+ }
139
+ };
140
+
141
+ JsonSchemaHelper.expandTypes(schemas, ['Customer']);
142
+
143
+ console.log(schemas.Order.properties?.customer); // { type: "object", properties: { name: { type: "string" } } }
144
+ ```
145
+
146
+ ```typescript
147
+ import type { IJsonSchema } from '@twin.org/tools-core';
148
+ import { JsonSchemaHelper } from '@twin.org/tools-core';
149
+
150
+ const schemas: { [id: string]: IJsonSchema } = {
151
+ Customer: {
152
+ type: 'object',
153
+ properties: {
154
+ name: { type: 'string' }
155
+ }
156
+ }
157
+ };
158
+
159
+ const customerRef: IJsonSchema = { $ref: '#/definitions/Customer' };
160
+
161
+ JsonSchemaHelper.expandSchemaTypes(schemas, customerRef, ['Customer']);
162
+
163
+ console.log(customerRef); // { type: "object", properties: { name: { type: "string" } } }
164
+ ```
165
+
166
+ ```typescript
167
+ import { JsonSchemaHelper } from '@twin.org/tools-core';
168
+
169
+ const regex = JsonSchemaHelper.stringToRegEx('/Order.*/');
170
+
171
+ console.log(regex.test('OrderCreated')); // true
172
+ ```
173
+
174
+ ## OpenApiHelper
175
+
176
+ ```typescript
177
+ import { OpenApiHelper } from '@twin.org/tools-core';
178
+
179
+ console.log(OpenApiHelper.API_VERSION); // "3.1.1"
180
+ ```
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@twin.org/tools-core",
3
- "version": "0.0.3-next.13",
4
- "description": "Shared components for the tools",
3
+ "version": "0.0.3-next.15",
4
+ "description": "Shared utilities and models for tooling packages",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/twinfoundation/tools.git",