@powerlines/schema 0.11.25 → 0.11.27
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/bundle.mjs.map +1 -1
- package/dist/codegen.cjs +49 -6
- package/dist/codegen.d.cts +21 -3
- package/dist/codegen.d.cts.map +1 -1
- package/dist/codegen.d.mts +21 -3
- package/dist/codegen.d.mts.map +1 -1
- package/dist/codegen.mjs +44 -3
- package/dist/codegen.mjs.map +1 -1
- package/dist/constants.cjs +19 -0
- package/dist/constants.d.cts +17 -0
- package/dist/constants.d.cts.map +1 -0
- package/dist/constants.d.mts +17 -0
- package/dist/constants.d.mts.map +1 -0
- package/dist/constants.mjs +18 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/extract.cjs +23 -4
- package/dist/extract.d.cts.map +1 -1
- package/dist/extract.d.mts.map +1 -1
- package/dist/extract.mjs +23 -4
- package/dist/extract.mjs.map +1 -1
- package/dist/helpers.cjs +94 -0
- package/dist/helpers.d.cts +44 -0
- package/dist/helpers.d.cts.map +1 -0
- package/dist/helpers.d.mts +44 -0
- package/dist/helpers.d.mts.map +1 -0
- package/dist/helpers.mjs +90 -0
- package/dist/helpers.mjs.map +1 -0
- package/dist/index.cjs +19 -2
- package/dist/index.d.cts +7 -4
- package/dist/index.d.mts +7 -4
- package/dist/index.mjs +6 -3
- package/dist/jtd.mjs.map +1 -1
- package/dist/persistence.cjs +76 -0
- package/dist/persistence.d.cts +47 -0
- package/dist/persistence.d.cts.map +1 -0
- package/dist/persistence.d.mts +47 -0
- package/dist/persistence.d.mts.map +1 -0
- package/dist/persistence.mjs +71 -0
- package/dist/persistence.mjs.map +1 -0
- package/dist/reflection.cjs +13 -6
- package/dist/reflection.d.cts.map +1 -1
- package/dist/reflection.d.mts.map +1 -1
- package/dist/reflection.mjs +12 -6
- package/dist/reflection.mjs.map +1 -1
- package/dist/resolve.mjs.map +1 -1
- package/dist/type-checks.cjs +21 -2
- package/dist/type-checks.d.cts +16 -2
- package/dist/type-checks.d.cts.map +1 -1
- package/dist/type-checks.d.mts +16 -2
- package/dist/type-checks.d.mts.map +1 -1
- package/dist/type-checks.mjs +21 -4
- package/dist/type-checks.mjs.map +1 -1
- package/dist/types.d.cts +42 -12
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +42 -12
- package/dist/types.d.mts.map +1 -1
- package/package.json +19 -7
package/dist/extract.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extract.mjs","names":["isSetObject","extractJsonSchemaZod"],"sources":["../src/extract.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { Context } from \"@powerlines/core\";\nimport { isTypeDefinition } from \"@powerlines/core\";\nimport { esbuildPlugin } from \"@powerlines/deepkit/esbuild-plugin\";\nimport { isType, stringifyType, Type } from \"@powerlines/deepkit/vendor/type\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { murmurhash } from \"@stryke/hash\";\nimport {\n isJsonSchemaObjectType,\n isStandardJsonSchema,\n JsonSchemaType\n} from \"@stryke/json\";\nimport { isSetString } from \"@stryke/type-checks\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport {\n extractJsonSchema as extractJsonSchemaZod,\n isZod3Type\n} from \"@stryke/zod\";\nimport defu from \"defu\";\nimport type { BuildOptions } from \"esbuild\";\nimport * as z3 from \"zod/v3\";\nimport { jsonSchemaToJtd } from \"./jtd\";\nimport { reflectionToJsonSchema } from \"./reflection\";\nimport { resolve } from \"./resolve\";\nimport {\n isExtractedSchema,\n isJTDSchema,\n isSchema,\n isUntypedInput,\n isUntypedSchema\n} from \"./type-checks\";\nimport {\n ExtractedSchema,\n JsonSchemaLike,\n JTDSchemaType,\n Schema,\n SchemaInput,\n SchemaInputVariant,\n SchemaMetadata,\n SchemaSource,\n SchemaSourceInput,\n SchemaSourceVariant,\n TypeDefinitionReference,\n UntypedInputObject,\n UntypedSchema\n} from \"./types\";\n\n/**\n * Creates a hash string for a given schema definition input. The function checks the type of the input and generates a hash based on its content. If the input is a Zod schema, it hashes the JSON representation of its internal definition. If the input is a Standard JSON Schema, it hashes the JSON representation of its internal standard schema. If the input is already a JSON Schema object, it hashes its JSON representation directly. If the input is a reflected Deepkit Type object, it hashes its JSON representation. The resulting hash string can be used for caching or comparison purposes.\n */\nexport function extractHash(\n variant: SchemaInputVariant,\n input: SchemaInput\n): string {\n if (isSetString(input)) {\n return murmurhash({ variant, input });\n } else if (isSetObject(input)) {\n if (isZod3Type(input)) {\n return murmurhash({ variant, input: input._def });\n } else if (isStandardJsonSchema(input)) {\n return murmurhash({ variant, input: input[\"~standard\"] });\n } else if (isJsonSchemaObjectType(input)) {\n return murmurhash({ variant, input });\n } else if (isType(input)) {\n return murmurhash({ variant, input: stringifyType(input) });\n }\n }\n\n throw new Error(\n `Failed to create an input hash for the provided schema definition input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object.`\n );\n}\n\n/**\n * Converts a reflected Deepkit {@link @powerlines/deepkit/vendor/type#Type} into a JSON Schema (draft-07) representation.\n *\n * @remarks\n * This function delegates to an internal recursive walker that handles the full set of Deepkit reflection kinds.\n *\n * @param reflection - The reflected Deepkit Type to convert.\n * @returns A JSON Schema (draft-07) fragment representing the type, or `undefined` when no schema could be produced.\n */\nexport function extractReflection<\n TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>\n>(reflection: Type): JTDSchemaType<TMetadata> | undefined {\n if (!isType(reflection)) {\n return undefined;\n }\n\n return reflectionToJsonSchema<TMetadata>(reflection);\n}\n\n/**\n * Extracts a JSON Type Definition (RFC 8927) schema from a given schema definition, if possible.\n *\n * @remarks\n * This function checks if the provided input is a Zod schema, a Standard JSON Schema, or already a JSON Schema object, extracts a JSON Schema fragment via the appropriate adapter (Zod and Standard Schema produce draft-07 fragments), and then converts that fragment into a valid JTD form suitable for AJV's JTD validator.\n *\n * @param schema - The schema definition to extract from. This can be a Zod schema, a Standard JSON Schema, or a JSON Schema object.\n * @returns The extracted JTD schema if successful, otherwise undefined.\n */\nexport function extractJsonSchema<\n TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>\n>(schema: unknown): JTDSchemaType<TMetadata> | undefined {\n if (\n isSetObject(schema) &&\n (isZod3Type(schema) ||\n isStandardJsonSchema(schema) ||\n isJsonSchemaObjectType(schema) ||\n isUntypedInput(schema) ||\n isUntypedSchema(schema))\n ) {\n let jsonSchema: unknown;\n if (isZod3Type(schema)) {\n jsonSchema = extractJsonSchemaZod(schema);\n } else if (isStandardJsonSchema(schema)) {\n jsonSchema = schema[\"~standard\"].jsonSchema.input({\n target: \"draft-2020-12\"\n });\n } else if (isUntypedInput(schema)) {\n jsonSchema = schema.$schema;\n } else {\n jsonSchema = schema;\n }\n\n const jtd = jsonSchemaToJtd<TMetadata>(jsonSchema as JsonSchemaLike);\n if (jtd) {\n return jtd;\n }\n }\n\n return undefined;\n}\n\n/**\n * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.\n *\n * @param input - The input object to extract the schema definition from.\n * @returns A `Schema` containing the extracted schema and its variant if successful.\n * @throws An error if the input does not contain a valid schema definition.\n */\nexport function extractResolvedVariant(\n input: SchemaSourceInput\n): SchemaSourceVariant {\n if (isSetObject(input)) {\n if (isZod3Type(input)) {\n return \"zod3\";\n } else if (isStandardJsonSchema(input)) {\n return \"standard-schema\";\n } else if (isJTDSchema(input)) {\n return \"jtd-schema\";\n } else if (isJsonSchemaObjectType(input)) {\n return \"json-schema\";\n } else if (isType(input)) {\n return \"reflection\";\n } else if (isUntypedInput(input) || isUntypedSchema(input)) {\n return \"untyped\";\n }\n }\n\n throw new Error(\n `Failed to determine the variant of the provided schema definition input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a reflected Deepkit Type object, or an Untyped schema.`\n );\n}\n\n/**\n * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a reflected Deepkit Type object, or an Untyped schema. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.\n *\n * @param input - The input object to extract the schema definition from.\n * @returns A `Schema` containing the extracted schema and its variant if successful.\n * @throws An error if the input does not contain a valid schema definition.\n */\nexport function extractVariant(input: SchemaInput): SchemaInputVariant {\n if (isSetString(input) || isTypeDefinition(input)) {\n return \"type-definition\";\n }\n\n return extractResolvedVariant(input as SchemaSourceInput);\n}\n\n/**\n * Extracts a JSON Schema object from a given schema definition input. The input can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding JSON Schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns the extracted JSON Schema if successful; otherwise, it throws an error.\n *\n * @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.\n * @param variant - The variant of the schema definition to extract.\n * @returns The extracted JSON Schema if successful.\n * @throws An error if the input does not contain a valid schema definition.\n */\nexport async function extractSchemaSchema<\n TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>\n>(\n input: SchemaSourceInput,\n variant?: SchemaInputVariant\n): Promise<JTDSchemaType<TMetadata>> {\n if (isExtractedSchema<TMetadata>(input)) {\n return input.schema;\n }\n\n const resolvedVariant = variant ?? extractResolvedVariant(input);\n\n let schema: JTDSchemaType<TMetadata> | undefined;\n if (\n resolvedVariant === \"zod3\" ||\n resolvedVariant === \"json-schema\" ||\n resolvedVariant === \"standard-schema\" ||\n resolvedVariant === \"untyped\"\n ) {\n const jsonSchema = extractJsonSchema<TMetadata>(input);\n if (jsonSchema) {\n schema = jsonSchemaToJtd<TMetadata>(jsonSchema);\n }\n } else if (resolvedVariant === \"reflection\") {\n schema = extractReflection<TMetadata>(input as Type);\n } else if (resolvedVariant === \"jtd-schema\") {\n schema = input as JTDSchemaType<TMetadata>;\n }\n\n if (schema) {\n return schema;\n }\n\n throw new Error(\n `Failed to extract a valid schema from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object.`\n );\n}\n\n/**\n * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.\n *\n * @param variant - The variant of the schema definition to extract.\n * @param input - The input object to extract the schema definition from.\n * @returns A `Schema` containing the extracted schema and its variant if successful.\n * @throws An error if the input does not contain a valid schema definition.\n */\nexport function extractSource<\n TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>\n>(variant: SchemaSourceVariant, input: SchemaSourceInput): SchemaSource {\n if (variant === \"zod3\") {\n return {\n hash: extractHash(variant, input),\n variant: \"zod3\",\n schema: input as z3.ZodTypeAny\n };\n } else if (variant === \"untyped\") {\n return {\n hash: extractHash(variant, input),\n variant: \"untyped\",\n schema: input as UntypedInputObject | UntypedSchema\n };\n } else if (variant === \"standard-schema\") {\n return {\n hash: extractHash(variant, input),\n variant: \"standard-schema\",\n schema: input as StandardJSONSchemaV1\n };\n } else if (variant === \"json-schema\") {\n return {\n hash: extractHash(variant, input),\n variant: \"json-schema\",\n schema: input as JsonSchemaType\n };\n } else if (variant === \"reflection\") {\n return {\n hash: extractHash(variant, input),\n variant: \"reflection\",\n schema: input as Type\n };\n } else if (variant === \"jtd-schema\") {\n return {\n hash: extractHash(variant, input),\n variant: \"jtd-schema\",\n schema: input as JTDSchemaType<TMetadata>\n };\n }\n\n throw new Error(\n `Failed to extract source information from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object.`\n );\n}\n\n/**\n * Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.\n *\n * @example\n * ```ts\n * // Resolve a schema definition from a file path\n * const schema1 = await extractSchema(context, \"./schemas.ts#MySchema\");\n * // Resolve a schema definition from a JSON Schema object\n * const schema2 = await extractSchema(context, schemaObject);\n * // Resolve a schema definition from a Zod schema\n * const schema3 = await extractSchema(context, zodSchema);\n * // Resolve a schema definition from a reflected Deepkit Type object\n * const schema4 = await extractSchema(context, reflectionType);\n * ```\n *\n * @param context - The plugin context used for resolving the schema definition input.\n * @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.\n * @param options - Optional overrides for the ESBuild configuration used during resolution.\n * @returns A promise that resolves to a {@link ExtractedSchema} containing the extracted JSON Schema and its variant, or the bytecode if JSON Schema extraction is not possible.\n */\nexport async function extractSchema<\n TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>,\n TContext extends Context = Context\n>(\n context: TContext,\n input: SchemaInput,\n options: Partial<BuildOptions> = {}\n): Promise<ExtractedSchema<TMetadata>> {\n if (isExtractedSchema<TMetadata>(input)) {\n return input;\n }\n\n if (isSchema<TMetadata>(input)) {\n return {\n ...input,\n source: {\n hash: extractHash(\"jtd-schema\", input.schema),\n variant: \"jtd-schema\",\n schema: input.schema\n }\n };\n }\n\n let source: SchemaSource;\n\n const variant = extractVariant(input);\n if (variant === \"type-definition\") {\n const resolved = await resolve<SchemaSourceInput>(\n context,\n input as TypeDefinitionReference,\n defu(options, {\n plugins: [\n esbuildPlugin(context, {\n reflection: \"default\",\n level: \"all\"\n })\n ]\n })\n );\n\n source = extractSource(extractResolvedVariant(resolved), resolved);\n } else if (\n [\n \"json-schema\",\n \"jtd-schema\",\n \"standard-schema\",\n \"zod3\",\n \"untyped\",\n \"reflection\"\n ].includes(variant)\n ) {\n source = extractSource(variant, input as SchemaSourceInput);\n } else {\n throw new Error(\n `Invalid schema definition input \"${\n variant\n }\". The variant must be one of \"type-definition\", \"json-schema\", \"jtd-schema\", \"standard-schema\", \"zod3\", \"untyped\", or \"reflection\".`\n );\n }\n\n return {\n variant,\n source,\n schema: await extractSchemaSchema<TMetadata>(source.schema, source.variant),\n hash: extractHash(variant, input)\n };\n}\n\n/**\n * Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.\n *\n * @example\n * ```ts\n * // Resolve a schema definition from a file path\n * const schema1 = await extract(context, \"./schemas.ts#MySchema\");\n * // Resolve a schema definition from a JSON Schema object\n * const schema2 = await extract(context, schemaObject);\n * // Resolve a schema definition from a Zod schema\n * const schema3 = await extract(context, zodSchema);\n * // Resolve a schema definition from a reflected Deepkit Type object\n * const schema4 = await extract(context, reflectionType);\n * ```\n *\n * @see https://github.com/colinhacks/zod\n * @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec\n * @see https://json-schema.org/\n * @see https://ajv.js.org/json-type-definition.html\n * @see https://deepkit.io/en/documentation/runtime-types/reflection\n * @see https://github.com/unjs/untyped\n * @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html\n *\n * @param context - The {@link Context | context} used for resolving the {@link Schema | schema} definition input.\n * @param input - The input object or string to extract the {@link Schema | schema} from. This can be {@link TypeDefinitionReference | a string that references a Typescript module}, a [Zod v3 schema](https://github.com/colinhacks/zod), any type that adheres to [the Standard JSON Schema specification](https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec), a [JSON Schema object](https://json-schema.org/), a [JTD schema object](https://ajv.js.org/json-type-definition.html), an [untyped schema](https://github.com/unjs/untyped), or a [TypeScript type reflection](https://deepkit.io/en/documentation/runtime-types/reflection).\n * @param options - Optional overrides for the [ESBuild configuration](https://esbuild.github.io/api/#general-options) used during resolution.\n * @returns A promise that resolves to a {@link Schema | schema} object parsed from the input.\n */\nexport async function extract<\n TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>,\n TContext extends Context = Context\n>(\n context: TContext,\n input: SchemaInput,\n options: Partial<BuildOptions> = {}\n): Promise<Schema<TMetadata>> {\n const result = await extractSchema<TMetadata>(context, input, options);\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmEA,SAAgB,YACd,SACA,OACQ;AACR,KAAI,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,QAAO;EACL,GAAG;EACH,QAAQ;GACN,MAAM,YAAY,cAAc,MAAM,OAAO;GAC7C,SAAS;GACT,QAAQ,MAAM;GACf;EACF;CAGH,IAAI;CAEJ,MAAM,UAAU,eAAe,MAAM;AACrC,KAAI,YAAY,mBAAmB;EACjC,MAAM,WAAW,MAAM,QACrB,SACA,OACA,KAAK,SAAS,EACZ,SAAS,CACP,cAAc,SAAS;GACrB,YAAY;GACZ,OAAO;GACR,CAAC,CACH,EACF,CAAC,CACH;AAED,WAAS,cAAc,uBAAuB,SAAS,EAAE,SAAS;YAElE;EACE;EACA;EACA;EACA;EACA;EACA;EACD,CAAC,SAAS,QAAQ,CAEnB,UAAS,cAAc,SAAS,MAA2B;KAE3D,OAAM,IAAI,MACR,oCACE,QACD,sIACF;AAGH,QAAO;EACL;EACA;EACA,QAAQ,MAAM,oBAA+B,OAAO,QAAQ,OAAO,QAAQ;EAC3E,MAAM,YAAY,SAAS,MAAM;EAClC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BH,eAAsB,QAIpB,SACA,OACA,UAAiC,EAAE,EACP;AAG5B,QAAO,MAFc,cAAyB,SAAS,OAAO,QAAQ"}
|
|
1
|
+
{"version":3,"file":"extract.mjs","names":["isSetObject","extractJsonSchemaZod"],"sources":["../src/extract.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { Context } from \"@powerlines/core\";\nimport { isTypeDefinition } from \"@powerlines/core\";\nimport { esbuildPlugin } from \"@powerlines/deepkit/esbuild-plugin\";\nimport { isType, stringifyType, Type } from \"@powerlines/deepkit/vendor/type\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { murmurhash } from \"@stryke/hash\";\nimport {\n isJsonSchemaObjectType,\n isStandardJsonSchema,\n JsonSchemaType\n} from \"@stryke/json\";\nimport { joinPaths } from \"@stryke/path/join\";\nimport { isSetString } from \"@stryke/type-checks\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport {\n extractJsonSchema as extractJsonSchemaZod,\n isZod3Type\n} from \"@stryke/zod\";\nimport defu from \"defu\";\nimport type { BuildOptions } from \"esbuild\";\nimport * as z3 from \"zod/v3\";\nimport { jsonSchemaToJtd } from \"./jtd\";\nimport { getCacheDirectory, writeSchema } from \"./persistence\";\nimport { reflectionToJsonSchema } from \"./reflection\";\nimport { resolve } from \"./resolve\";\nimport {\n isExtractedSchema,\n isJTDSchema,\n isSchema,\n isUntypedInput,\n isUntypedSchema\n} from \"./type-checks\";\nimport {\n ExtractedSchema,\n JsonSchemaLike,\n JTDSchemaType,\n Schema,\n SchemaInput,\n SchemaInputVariant,\n SchemaMetadata,\n SchemaSource,\n SchemaSourceInput,\n SchemaSourceVariant,\n TypeDefinitionReference,\n UntypedInputObject,\n UntypedSchema\n} from \"./types\";\n\n/**\n * Creates a hash string for a given schema definition input. The function checks the type of the input and generates a hash based on its content. If the input is a Zod schema, it hashes the JSON representation of its internal definition. If the input is a Standard JSON Schema, it hashes the JSON representation of its internal standard schema. If the input is already a JSON Schema object, it hashes its JSON representation directly. If the input is a reflected Deepkit Type object, it hashes its JSON representation. The resulting hash string can be used for caching or comparison purposes.\n */\nexport function extractHash(\n variant: SchemaInputVariant,\n input: SchemaInput\n): string {\n if (isSetString(input)) {\n return murmurhash({ variant, input });\n } else if (isSetObject(input)) {\n if (isZod3Type(input)) {\n return murmurhash({ variant, input: input._def });\n } else if (isStandardJsonSchema(input)) {\n return murmurhash({ variant, input: input[\"~standard\"] });\n } else if (isJsonSchemaObjectType(input)) {\n return murmurhash({ variant, input });\n } else if (isType(input)) {\n return murmurhash({ variant, input: stringifyType(input) });\n }\n }\n\n throw new Error(\n `Failed to create an input hash for the provided schema definition input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object.`\n );\n}\n\n/**\n * Converts a reflected Deepkit {@link @powerlines/deepkit/vendor/type#Type} into a JSON Schema (draft-07) representation.\n *\n * @remarks\n * This function delegates to an internal recursive walker that handles the full set of Deepkit reflection kinds.\n *\n * @param reflection - The reflected Deepkit Type to convert.\n * @returns A JSON Schema (draft-07) fragment representing the type, or `undefined` when no schema could be produced.\n */\nexport function extractReflection<\n TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>\n>(reflection: Type): JTDSchemaType<TMetadata> | undefined {\n if (!isType(reflection)) {\n return undefined;\n }\n\n return reflectionToJsonSchema<TMetadata>(reflection);\n}\n\n/**\n * Extracts a JSON Type Definition (RFC 8927) schema from a given schema definition, if possible.\n *\n * @remarks\n * This function checks if the provided input is a Zod schema, a Standard JSON Schema, or already a JSON Schema object, extracts a JSON Schema fragment via the appropriate adapter (Zod and Standard Schema produce draft-07 fragments), and then converts that fragment into a valid JTD form suitable for AJV's JTD validator.\n *\n * @param schema - The schema definition to extract from. This can be a Zod schema, a Standard JSON Schema, or a JSON Schema object.\n * @returns The extracted JTD schema if successful, otherwise undefined.\n */\nexport function extractJsonSchema<\n TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>\n>(schema: unknown): JTDSchemaType<TMetadata> | undefined {\n if (\n isSetObject(schema) &&\n (isZod3Type(schema) ||\n isStandardJsonSchema(schema) ||\n isJsonSchemaObjectType(schema) ||\n isUntypedInput(schema) ||\n isUntypedSchema(schema))\n ) {\n let jsonSchema: unknown;\n if (isZod3Type(schema)) {\n jsonSchema = extractJsonSchemaZod(schema);\n } else if (isStandardJsonSchema(schema)) {\n jsonSchema = schema[\"~standard\"].jsonSchema.input({\n target: \"draft-2020-12\"\n });\n } else if (isUntypedInput(schema)) {\n jsonSchema = schema.$schema;\n } else {\n jsonSchema = schema;\n }\n\n const jtd = jsonSchemaToJtd<TMetadata>(jsonSchema as JsonSchemaLike);\n if (jtd) {\n return jtd;\n }\n }\n\n return undefined;\n}\n\n/**\n * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.\n *\n * @param input - The input object to extract the schema definition from.\n * @returns A `Schema` containing the extracted schema and its variant if successful.\n * @throws An error if the input does not contain a valid schema definition.\n */\nexport function extractResolvedVariant(\n input: SchemaSourceInput\n): SchemaSourceVariant {\n if (isSetObject(input)) {\n if (isZod3Type(input)) {\n return \"zod3\";\n } else if (isStandardJsonSchema(input)) {\n return \"standard-schema\";\n } else if (isJTDSchema(input)) {\n return \"jtd-schema\";\n } else if (isJsonSchemaObjectType(input)) {\n return \"json-schema\";\n } else if (isType(input)) {\n return \"reflection\";\n } else if (isUntypedInput(input) || isUntypedSchema(input)) {\n return \"untyped\";\n }\n }\n\n throw new Error(\n `Failed to determine the variant of the provided schema definition input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a reflected Deepkit Type object, or an Untyped schema.`\n );\n}\n\n/**\n * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a reflected Deepkit Type object, or an Untyped schema. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.\n *\n * @param input - The input object to extract the schema definition from.\n * @returns A `Schema` containing the extracted schema and its variant if successful.\n * @throws An error if the input does not contain a valid schema definition.\n */\nexport function extractVariant(input: SchemaInput): SchemaInputVariant {\n if (isSetString(input) || isTypeDefinition(input)) {\n return \"type-definition\";\n }\n\n return extractResolvedVariant(input as SchemaSourceInput);\n}\n\n/**\n * Extracts a JSON Schema object from a given schema definition input. The input can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding JSON Schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns the extracted JSON Schema if successful; otherwise, it throws an error.\n *\n * @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.\n * @param variant - The variant of the schema definition to extract.\n * @returns The extracted JSON Schema if successful.\n * @throws An error if the input does not contain a valid schema definition.\n */\nexport async function extractSchemaSchema<\n TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>\n>(\n input: SchemaSourceInput,\n variant?: SchemaInputVariant\n): Promise<JTDSchemaType<TMetadata>> {\n if (isExtractedSchema<TMetadata>(input)) {\n return input.schema;\n }\n\n const resolvedVariant = variant ?? extractResolvedVariant(input);\n\n let schema: JTDSchemaType<TMetadata> | undefined;\n if (\n resolvedVariant === \"zod3\" ||\n resolvedVariant === \"json-schema\" ||\n resolvedVariant === \"standard-schema\" ||\n resolvedVariant === \"untyped\"\n ) {\n const jsonSchema = extractJsonSchema<TMetadata>(input);\n if (jsonSchema) {\n schema = jsonSchemaToJtd<TMetadata>(jsonSchema);\n }\n } else if (resolvedVariant === \"reflection\") {\n schema = extractReflection<TMetadata>(input as Type);\n } else if (resolvedVariant === \"jtd-schema\") {\n schema = input as JTDSchemaType<TMetadata>;\n }\n\n if (schema) {\n return schema;\n }\n\n throw new Error(\n `Failed to extract a valid schema from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object.`\n );\n}\n\n/**\n * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.\n *\n * @param variant - The variant of the schema definition to extract.\n * @param input - The input object to extract the schema definition from.\n * @returns A `Schema` containing the extracted schema and its variant if successful.\n * @throws An error if the input does not contain a valid schema definition.\n */\nexport function extractSource<\n TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>\n>(variant: SchemaSourceVariant, input: SchemaSourceInput): SchemaSource {\n if (variant === \"zod3\") {\n return {\n hash: extractHash(variant, input),\n variant: \"zod3\",\n schema: input as z3.ZodTypeAny\n };\n } else if (variant === \"untyped\") {\n return {\n hash: extractHash(variant, input),\n variant: \"untyped\",\n schema: input as UntypedInputObject | UntypedSchema\n };\n } else if (variant === \"standard-schema\") {\n return {\n hash: extractHash(variant, input),\n variant: \"standard-schema\",\n schema: input as StandardJSONSchemaV1\n };\n } else if (variant === \"json-schema\") {\n return {\n hash: extractHash(variant, input),\n variant: \"json-schema\",\n schema: input as JsonSchemaType\n };\n } else if (variant === \"reflection\") {\n return {\n hash: extractHash(variant, input),\n variant: \"reflection\",\n schema: input as Type\n };\n } else if (variant === \"jtd-schema\") {\n return {\n hash: extractHash(variant, input),\n variant: \"jtd-schema\",\n schema: input as JTDSchemaType<TMetadata>\n };\n }\n\n throw new Error(\n `Failed to extract source information from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object.`\n );\n}\n\n/**\n * Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.\n *\n * @example\n * ```ts\n * // Resolve a schema definition from a file path\n * const schema1 = await extractSchema(context, \"./schemas.ts#MySchema\");\n * // Resolve a schema definition from a JSON Schema object\n * const schema2 = await extractSchema(context, schemaObject);\n * // Resolve a schema definition from a Zod schema\n * const schema3 = await extractSchema(context, zodSchema);\n * // Resolve a schema definition from a reflected Deepkit Type object\n * const schema4 = await extractSchema(context, reflectionType);\n * ```\n *\n * @param context - The plugin context used for resolving the schema definition input.\n * @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.\n * @param options - Optional overrides for the ESBuild configuration used during resolution.\n * @returns A promise that resolves to a {@link ExtractedSchema} containing the extracted JSON Schema and its variant, or the bytecode if JSON Schema extraction is not possible.\n */\nexport async function extractSchema<\n TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>,\n TContext extends Context = Context\n>(\n context: TContext,\n input: SchemaInput,\n options: Partial<BuildOptions> = {}\n): Promise<ExtractedSchema<TMetadata>> {\n if (isExtractedSchema<TMetadata>(input)) {\n return input;\n }\n\n if (isSchema<TMetadata>(input)) {\n return {\n ...input,\n source: {\n hash: extractHash(\"jtd-schema\", input.schema),\n variant: \"jtd-schema\",\n schema: input.schema\n }\n };\n }\n\n let source: SchemaSource;\n\n const variant = extractVariant(input);\n const hash = extractHash(variant, input);\n\n if (variant === \"type-definition\") {\n const resolved = await resolve<SchemaSourceInput>(\n context,\n input as TypeDefinitionReference,\n defu(options, {\n plugins: [\n esbuildPlugin(context, {\n reflection: \"default\",\n level: \"all\"\n })\n ]\n })\n );\n\n source = extractSource(extractResolvedVariant(resolved), resolved);\n } else if (\n [\n \"json-schema\",\n \"jtd-schema\",\n \"standard-schema\",\n \"zod3\",\n \"untyped\",\n \"reflection\"\n ].includes(variant)\n ) {\n source = extractSource(variant, input as SchemaSourceInput);\n } else {\n throw new Error(\n `Invalid schema definition input \"${\n variant\n }\". The variant must be one of \"type-definition\", \"json-schema\", \"jtd-schema\", \"standard-schema\", \"zod3\", \"untyped\", or \"reflection\".`\n );\n }\n\n return {\n variant,\n source,\n schema: await extractSchemaSchema<TMetadata>(source.schema, source.variant),\n hash\n };\n}\n\n/**\n * Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.\n *\n * @example\n * ```ts\n * // Resolve a schema definition from a file path\n * const schema1 = await extract(context, \"./schemas.ts#MySchema\");\n * // Resolve a schema definition from a JSON Schema object\n * const schema2 = await extract(context, schemaObject);\n * // Resolve a schema definition from a Zod schema\n * const schema3 = await extract(context, zodSchema);\n * // Resolve a schema definition from a reflected Deepkit Type object\n * const schema4 = await extract(context, reflectionType);\n * ```\n *\n * @see https://github.com/colinhacks/zod\n * @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec\n * @see https://json-schema.org/\n * @see https://ajv.js.org/json-type-definition.html\n * @see https://deepkit.io/en/documentation/runtime-types/reflection\n * @see https://github.com/unjs/untyped\n * @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html\n *\n * @param context - The {@link Context | context} used for resolving the {@link Schema | schema} definition input.\n * @param input - The input object or string to extract the {@link Schema | schema} from. This can be {@link TypeDefinitionReference | a string that references a Typescript module}, a [Zod v3 schema](https://github.com/colinhacks/zod), any type that adheres to [the Standard JSON Schema specification](https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec), a [JSON Schema object](https://json-schema.org/), a [JTD schema object](https://ajv.js.org/json-type-definition.html), an [untyped schema](https://github.com/unjs/untyped), or a [TypeScript type reflection](https://deepkit.io/en/documentation/runtime-types/reflection).\n * @param options - Optional overrides for the [ESBuild configuration](https://esbuild.github.io/api/#general-options) used during resolution.\n * @returns A promise that resolves to a {@link Schema | schema} object parsed from the input.\n */\nexport async function extract<\n TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>,\n TContext extends Context = Context\n>(\n context: TContext,\n input: SchemaInput,\n options: Partial<BuildOptions> = {}\n): Promise<Schema<TMetadata>> {\n if (isExtractedSchema<TMetadata>(input) || isSchema<TMetadata>(input)) {\n return input;\n }\n\n let result: Schema<TMetadata> | undefined;\n\n const variant = extractVariant(input);\n const hash = extractHash(variant, input);\n\n const cacheFilePath = joinPaths(getCacheDirectory(context), `${hash}.json`);\n if (\n context.config.skipCache !== true &&\n context.fs.existsSync(cacheFilePath)\n ) {\n const schema = await context.fs.read(cacheFilePath);\n if (schema) {\n result = {\n variant,\n hash,\n schema: JSON.parse(schema) as JTDSchemaType<TMetadata>\n };\n }\n }\n\n result ??= await extractSchema<TMetadata>(context, input, options);\n if (!result?.schema) {\n throw new Error(\n `Failed to extract a valid schema from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object.`\n );\n }\n\n if (context.config.skipCache !== true) {\n await writeSchema(context, result);\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAqEA,SAAgB,YACd,SACA,OACQ;CACR,IAAI,YAAY,KAAK,GACnB,OAAO,WAAW;EAAE;EAAS;CAAM,CAAC;MAC/B,IAAIA,cAAY,KAAK,GAC1B;MAAI,WAAW,KAAK,GAClB,OAAO,WAAW;GAAE;GAAS,OAAO,MAAM;EAAK,CAAC;OAC3C,IAAI,qBAAqB,KAAK,GACnC,OAAO,WAAW;GAAE;GAAS,O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cjs
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
3
|
+
const require_type_checks = require('./type-checks.cjs');
|
|
4
|
+
let defu = require("defu");
|
|
5
|
+
let _stryke_type_checks = require("@stryke/type-checks");
|
|
6
|
+
|
|
7
|
+
//#region src/helpers.ts
|
|
8
|
+
/**
|
|
9
|
+
* A helper function to extract the properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns a record of its properties, where each key is the property name and the value is the corresponding JTD schema type along with an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function checks both `properties` and `optionalProperties` of the JTD schema to construct the resulting record.
|
|
10
|
+
*
|
|
11
|
+
* @param obj - The {@link ObjectSchema} from which to extract the properties.
|
|
12
|
+
* @returns A record of the properties defined in the JTD object schema, where each key is the property name and the value is an object containing the JTD schema type and an `optional` flag.
|
|
13
|
+
*/
|
|
14
|
+
function getProperties(obj) {
|
|
15
|
+
const properties = {};
|
|
16
|
+
const schema = require_type_checks.isObjectSchema(obj) ? obj.schema : obj;
|
|
17
|
+
if ("optionalProperties" in schema && (0, _stryke_type_checks.isSetObject)(schema.optionalProperties)) for (const [key, value] of Object.entries(schema.optionalProperties)) properties[key] = {
|
|
18
|
+
...value,
|
|
19
|
+
name: key,
|
|
20
|
+
optional: true
|
|
21
|
+
};
|
|
22
|
+
if ("properties" in schema && (0, _stryke_type_checks.isSetObject)(schema.properties)) for (const [key, value] of Object.entries(schema.properties)) properties[key] = {
|
|
23
|
+
...value,
|
|
24
|
+
name: key,
|
|
25
|
+
optional: false
|
|
26
|
+
};
|
|
27
|
+
return properties;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A helper function to get a list of properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns an array of its properties, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function utilizes the `getProperties` helper function to first retrieve the properties as a record and then converts that record into an array format for easier iteration and access.
|
|
31
|
+
*
|
|
32
|
+
* @param obj - The {@link ObjectSchema} from which to extract the properties.
|
|
33
|
+
* @returns An array of properties defined in the JTD object schema, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag.
|
|
34
|
+
*/
|
|
35
|
+
function getPropertiesList(obj) {
|
|
36
|
+
return Object.values(getProperties(obj));
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A helper function to add a property to a JTD object schema. This function takes a JTD object schema (either {@link JTDSchemaObjectType} or {@link ObjectSchema}), a property name, and a JTD schema type as input. It adds the specified property to the appropriate section of the JTD schema based on whether the property is optional or required. If the property is optional (i.e., has `nullable: true`), it is added to the `optionalProperties` section of the schema; otherwise, it is added to the `properties` section. The function also ensures that the property's metadata includes its name for easier identification and access in future operations.
|
|
40
|
+
*
|
|
41
|
+
* @param obj - The JTD object schema to which the property should be added.
|
|
42
|
+
* @param name - The name of the property to add.
|
|
43
|
+
* @param property - The JTD schema type of the property to add, which may include a `nullable` flag indicating whether the property is optional.
|
|
44
|
+
*/
|
|
45
|
+
function addProperty(obj, name, property) {
|
|
46
|
+
const schema = require_type_checks.isObjectSchema(obj) ? obj.schema : obj;
|
|
47
|
+
if (property.nullable) {
|
|
48
|
+
schema.optionalProperties ??= {};
|
|
49
|
+
schema.optionalProperties[name] = {
|
|
50
|
+
...property,
|
|
51
|
+
metadata: {
|
|
52
|
+
...property.metadata,
|
|
53
|
+
name
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
} else {
|
|
57
|
+
schema.properties ??= {};
|
|
58
|
+
schema.properties[name] = {
|
|
59
|
+
...property,
|
|
60
|
+
metadata: {
|
|
61
|
+
...property.metadata,
|
|
62
|
+
name
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Merges multiple JTD object schemas into a single schema. This function takes an array of JTD object schemas (either {@link JTDSchemaObjectType} or {@link ObjectSchema}) and combines their properties into a single JTD object schema. The resulting schema will contain all properties from the input schemas, with optional properties marked accordingly. If there are overlapping properties between the input schemas, the function will merge them using a deep merge strategy (via `defu`) if they are both JTD object schemas; otherwise, the property from the first schema in the input array will take precedence.
|
|
69
|
+
*
|
|
70
|
+
* @remarks
|
|
71
|
+
* This function attempts to mimic {@link defu}'s behavior for merging objects, but with special handling for JTD schemas. When merging properties, if a property exists in multiple input schemas and is defined as a JTD object schema in all of them, the function will perform a deep merge of those schemas. If the property is not a JTD object schema in any of the input schemas, the function will simply take the first definition it encounters.
|
|
72
|
+
*
|
|
73
|
+
* @param schemas - An array of JTD object schemas to merge.
|
|
74
|
+
* @returns A single JTD object schema that combines the properties of all input schemas.
|
|
75
|
+
*/
|
|
76
|
+
function mergeSchemas(...schemas) {
|
|
77
|
+
const mergedSchema = {
|
|
78
|
+
properties: {},
|
|
79
|
+
optionalProperties: {},
|
|
80
|
+
additionalProperties: false
|
|
81
|
+
};
|
|
82
|
+
for (const schema of schemas.reverse()) {
|
|
83
|
+
const properties = getProperties(schema);
|
|
84
|
+
for (const [key, value] of Object.entries(properties)) if (value.optional) mergedSchema.optionalProperties[key] = mergedSchema.optionalProperties[key] && require_type_checks.isJTDSchemaObject(mergedSchema.optionalProperties[key]) && require_type_checks.isJTDSchemaObject(value) ? (0, defu.defu)(mergedSchema.optionalProperties[key], value) : value;
|
|
85
|
+
else mergedSchema.properties[key] = mergedSchema.properties[key] && require_type_checks.isJTDSchemaObject(mergedSchema.properties[key]) && require_type_checks.isJTDSchemaObject(value) ? (0, defu.defu)(mergedSchema.properties[key], value) : value;
|
|
86
|
+
}
|
|
87
|
+
return mergedSchema;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
//#endregion
|
|
91
|
+
exports.addProperty = addProperty;
|
|
92
|
+
exports.getProperties = getProperties;
|
|
93
|
+
exports.getPropertiesList = getPropertiesList;
|
|
94
|
+
exports.mergeSchemas = mergeSchemas;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { JTDSchemaObjectType, JTDSchemaType, ObjectSchema, SchemaMetadata } from "./types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/helpers.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A helper function to extract the properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns a record of its properties, where each key is the property name and the value is the corresponding JTD schema type along with an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function checks both `properties` and `optionalProperties` of the JTD schema to construct the resulting record.
|
|
6
|
+
*
|
|
7
|
+
* @param obj - The {@link ObjectSchema} from which to extract the properties.
|
|
8
|
+
* @returns A record of the properties defined in the JTD object schema, where each key is the property name and the value is an object containing the JTD schema type and an `optional` flag.
|
|
9
|
+
*/
|
|
10
|
+
declare function getProperties<TMetadata extends SchemaMetadata>(obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>): Record<string, JTDSchemaType<TMetadata> & {
|
|
11
|
+
name: string;
|
|
12
|
+
optional: boolean;
|
|
13
|
+
}>;
|
|
14
|
+
/**
|
|
15
|
+
* A helper function to get a list of properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns an array of its properties, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function utilizes the `getProperties` helper function to first retrieve the properties as a record and then converts that record into an array format for easier iteration and access.
|
|
16
|
+
*
|
|
17
|
+
* @param obj - The {@link ObjectSchema} from which to extract the properties.
|
|
18
|
+
* @returns An array of properties defined in the JTD object schema, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag.
|
|
19
|
+
*/
|
|
20
|
+
declare function getPropertiesList<TMetadata extends SchemaMetadata>(obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>): Array<JTDSchemaType<TMetadata> & {
|
|
21
|
+
name: string;
|
|
22
|
+
optional: boolean;
|
|
23
|
+
}>;
|
|
24
|
+
/**
|
|
25
|
+
* A helper function to add a property to a JTD object schema. This function takes a JTD object schema (either {@link JTDSchemaObjectType} or {@link ObjectSchema}), a property name, and a JTD schema type as input. It adds the specified property to the appropriate section of the JTD schema based on whether the property is optional or required. If the property is optional (i.e., has `nullable: true`), it is added to the `optionalProperties` section of the schema; otherwise, it is added to the `properties` section. The function also ensures that the property's metadata includes its name for easier identification and access in future operations.
|
|
26
|
+
*
|
|
27
|
+
* @param obj - The JTD object schema to which the property should be added.
|
|
28
|
+
* @param name - The name of the property to add.
|
|
29
|
+
* @param property - The JTD schema type of the property to add, which may include a `nullable` flag indicating whether the property is optional.
|
|
30
|
+
*/
|
|
31
|
+
declare function addProperty<TMetadata extends SchemaMetadata>(obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>, name: string, property: JTDSchemaType<TMetadata>): void;
|
|
32
|
+
/**
|
|
33
|
+
* Merges multiple JTD object schemas into a single schema. This function takes an array of JTD object schemas (either {@link JTDSchemaObjectType} or {@link ObjectSchema}) and combines their properties into a single JTD object schema. The resulting schema will contain all properties from the input schemas, with optional properties marked accordingly. If there are overlapping properties between the input schemas, the function will merge them using a deep merge strategy (via `defu`) if they are both JTD object schemas; otherwise, the property from the first schema in the input array will take precedence.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* This function attempts to mimic {@link defu}'s behavior for merging objects, but with special handling for JTD schemas. When merging properties, if a property exists in multiple input schemas and is defined as a JTD object schema in all of them, the function will perform a deep merge of those schemas. If the property is not a JTD object schema in any of the input schemas, the function will simply take the first definition it encounters.
|
|
37
|
+
*
|
|
38
|
+
* @param schemas - An array of JTD object schemas to merge.
|
|
39
|
+
* @returns A single JTD object schema that combines the properties of all input schemas.
|
|
40
|
+
*/
|
|
41
|
+
declare function mergeSchemas<TMetadata extends SchemaMetadata>(...schemas: (JTDSchemaObjectType<TMetadata> | ObjectSchema<TMetadata>)[]): JTDSchemaObjectType<TMetadata>;
|
|
42
|
+
//#endregion
|
|
43
|
+
export { addProperty, getProperties, getPropertiesList, mergeSchemas };
|
|
44
|
+
//# sourceMappingURL=helpers.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.cts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;AAkCA;;;;iBAAgB,aAAA,mBAAgC,cAAA,CAAA,CAC9C,GAAA,EAAK,YAAA,CAAa,SAAA,IAAa,mBAAA,CAAoB,SAAA,IAClD,MAAA,SAED,aAAA,CAAc,SAAA;EAAe,IAAA;EAAc,QAAA;AAAA;;;;;;;iBAgC7B,iBAAA,mBAAoC,cAAA,CAAA,CAClD,GAAA,EAAK,YAAA,CAAa,SAAA,IAAa,mBAAA,CAAoB,SAAA,IAClD,KAAA,CAAM,aAAA,CAAc,SAAA;EAAe,IAAA;EAAc,QAAA;AAAA;;;;;;;;iBAWpC,WAAA,mBAA8B,cAAA,CAAA,CAC5C,GAAA,EAAK,YAAA,CAAa,SAAA,IAAa,mBAAA,CAAoB,SAAA,GACnD,IAAA,UACA,QAAA,EAAU,aAAA,CAAc,SAAA;AAhD2B;AAgCrD;;;;;;;;AAhCqD,iBA2ErC,YAAA,mBAA+B,cAAA,CAAA,CAAA,GAC1C,OAAA,GAAU,mBAAA,CAAoB,SAAA,IAAa,YAAA,CAAa,SAAA,OAC1D,mBAAA,CAAoB,SAAA"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { JTDSchemaObjectType, JTDSchemaType, ObjectSchema, SchemaMetadata } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/helpers.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A helper function to extract the properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns a record of its properties, where each key is the property name and the value is the corresponding JTD schema type along with an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function checks both `properties` and `optionalProperties` of the JTD schema to construct the resulting record.
|
|
6
|
+
*
|
|
7
|
+
* @param obj - The {@link ObjectSchema} from which to extract the properties.
|
|
8
|
+
* @returns A record of the properties defined in the JTD object schema, where each key is the property name and the value is an object containing the JTD schema type and an `optional` flag.
|
|
9
|
+
*/
|
|
10
|
+
declare function getProperties<TMetadata extends SchemaMetadata>(obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>): Record<string, JTDSchemaType<TMetadata> & {
|
|
11
|
+
name: string;
|
|
12
|
+
optional: boolean;
|
|
13
|
+
}>;
|
|
14
|
+
/**
|
|
15
|
+
* A helper function to get a list of properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns an array of its properties, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function utilizes the `getProperties` helper function to first retrieve the properties as a record and then converts that record into an array format for easier iteration and access.
|
|
16
|
+
*
|
|
17
|
+
* @param obj - The {@link ObjectSchema} from which to extract the properties.
|
|
18
|
+
* @returns An array of properties defined in the JTD object schema, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag.
|
|
19
|
+
*/
|
|
20
|
+
declare function getPropertiesList<TMetadata extends SchemaMetadata>(obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>): Array<JTDSchemaType<TMetadata> & {
|
|
21
|
+
name: string;
|
|
22
|
+
optional: boolean;
|
|
23
|
+
}>;
|
|
24
|
+
/**
|
|
25
|
+
* A helper function to add a property to a JTD object schema. This function takes a JTD object schema (either {@link JTDSchemaObjectType} or {@link ObjectSchema}), a property name, and a JTD schema type as input. It adds the specified property to the appropriate section of the JTD schema based on whether the property is optional or required. If the property is optional (i.e., has `nullable: true`), it is added to the `optionalProperties` section of the schema; otherwise, it is added to the `properties` section. The function also ensures that the property's metadata includes its name for easier identification and access in future operations.
|
|
26
|
+
*
|
|
27
|
+
* @param obj - The JTD object schema to which the property should be added.
|
|
28
|
+
* @param name - The name of the property to add.
|
|
29
|
+
* @param property - The JTD schema type of the property to add, which may include a `nullable` flag indicating whether the property is optional.
|
|
30
|
+
*/
|
|
31
|
+
declare function addProperty<TMetadata extends SchemaMetadata>(obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>, name: string, property: JTDSchemaType<TMetadata>): void;
|
|
32
|
+
/**
|
|
33
|
+
* Merges multiple JTD object schemas into a single schema. This function takes an array of JTD object schemas (either {@link JTDSchemaObjectType} or {@link ObjectSchema}) and combines their properties into a single JTD object schema. The resulting schema will contain all properties from the input schemas, with optional properties marked accordingly. If there are overlapping properties between the input schemas, the function will merge them using a deep merge strategy (via `defu`) if they are both JTD object schemas; otherwise, the property from the first schema in the input array will take precedence.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* This function attempts to mimic {@link defu}'s behavior for merging objects, but with special handling for JTD schemas. When merging properties, if a property exists in multiple input schemas and is defined as a JTD object schema in all of them, the function will perform a deep merge of those schemas. If the property is not a JTD object schema in any of the input schemas, the function will simply take the first definition it encounters.
|
|
37
|
+
*
|
|
38
|
+
* @param schemas - An array of JTD object schemas to merge.
|
|
39
|
+
* @returns A single JTD object schema that combines the properties of all input schemas.
|
|
40
|
+
*/
|
|
41
|
+
declare function mergeSchemas<TMetadata extends SchemaMetadata>(...schemas: (JTDSchemaObjectType<TMetadata> | ObjectSchema<TMetadata>)[]): JTDSchemaObjectType<TMetadata>;
|
|
42
|
+
//#endregion
|
|
43
|
+
export { addProperty, getProperties, getPropertiesList, mergeSchemas };
|
|
44
|
+
//# sourceMappingURL=helpers.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.mts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;AAkCA;;;;iBAAgB,aAAA,mBAAgC,cAAA,CAAA,CAC9C,GAAA,EAAK,YAAA,CAAa,SAAA,IAAa,mBAAA,CAAoB,SAAA,IAClD,MAAA,SAED,aAAA,CAAc,SAAA;EAAe,IAAA;EAAc,QAAA;AAAA;;;;;;;iBAgC7B,iBAAA,mBAAoC,cAAA,CAAA,CAClD,GAAA,EAAK,YAAA,CAAa,SAAA,IAAa,mBAAA,CAAoB,SAAA,IAClD,KAAA,CAAM,aAAA,CAAc,SAAA;EAAe,IAAA;EAAc,QAAA;AAAA;;;;;;;;iBAWpC,WAAA,mBAA8B,cAAA,CAAA,CAC5C,GAAA,EAAK,YAAA,CAAa,SAAA,IAAa,mBAAA,CAAoB,SAAA,GACnD,IAAA,UACA,QAAA,EAAU,aAAA,CAAc,SAAA;AAhD2B;AAgCrD;;;;;;;;AAhCqD,iBA2ErC,YAAA,mBAA+B,cAAA,CAAA,CAAA,GAC1C,OAAA,GAAU,mBAAA,CAAoB,SAAA,IAAa,YAAA,CAAa,SAAA,OAC1D,mBAAA,CAAoB,SAAA"}
|
package/dist/helpers.mjs
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { isJTDSchemaObject, isObjectSchema } from "./type-checks.mjs";
|
|
2
|
+
import { defu as defu$1 } from "defu";
|
|
3
|
+
import { isSetObject } from "@stryke/type-checks";
|
|
4
|
+
|
|
5
|
+
//#region src/helpers.ts
|
|
6
|
+
/**
|
|
7
|
+
* A helper function to extract the properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns a record of its properties, where each key is the property name and the value is the corresponding JTD schema type along with an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function checks both `properties` and `optionalProperties` of the JTD schema to construct the resulting record.
|
|
8
|
+
*
|
|
9
|
+
* @param obj - The {@link ObjectSchema} from which to extract the properties.
|
|
10
|
+
* @returns A record of the properties defined in the JTD object schema, where each key is the property name and the value is an object containing the JTD schema type and an `optional` flag.
|
|
11
|
+
*/
|
|
12
|
+
function getProperties(obj) {
|
|
13
|
+
const properties = {};
|
|
14
|
+
const schema = isObjectSchema(obj) ? obj.schema : obj;
|
|
15
|
+
if ("optionalProperties" in schema && isSetObject(schema.optionalProperties)) for (const [key, value] of Object.entries(schema.optionalProperties)) properties[key] = {
|
|
16
|
+
...value,
|
|
17
|
+
name: key,
|
|
18
|
+
optional: true
|
|
19
|
+
};
|
|
20
|
+
if ("properties" in schema && isSetObject(schema.properties)) for (const [key, value] of Object.entries(schema.properties)) properties[key] = {
|
|
21
|
+
...value,
|
|
22
|
+
name: key,
|
|
23
|
+
optional: false
|
|
24
|
+
};
|
|
25
|
+
return properties;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A helper function to get a list of properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns an array of its properties, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function utilizes the `getProperties` helper function to first retrieve the properties as a record and then converts that record into an array format for easier iteration and access.
|
|
29
|
+
*
|
|
30
|
+
* @param obj - The {@link ObjectSchema} from which to extract the properties.
|
|
31
|
+
* @returns An array of properties defined in the JTD object schema, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag.
|
|
32
|
+
*/
|
|
33
|
+
function getPropertiesList(obj) {
|
|
34
|
+
return Object.values(getProperties(obj));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A helper function to add a property to a JTD object schema. This function takes a JTD object schema (either {@link JTDSchemaObjectType} or {@link ObjectSchema}), a property name, and a JTD schema type as input. It adds the specified property to the appropriate section of the JTD schema based on whether the property is optional or required. If the property is optional (i.e., has `nullable: true`), it is added to the `optionalProperties` section of the schema; otherwise, it is added to the `properties` section. The function also ensures that the property's metadata includes its name for easier identification and access in future operations.
|
|
38
|
+
*
|
|
39
|
+
* @param obj - The JTD object schema to which the property should be added.
|
|
40
|
+
* @param name - The name of the property to add.
|
|
41
|
+
* @param property - The JTD schema type of the property to add, which may include a `nullable` flag indicating whether the property is optional.
|
|
42
|
+
*/
|
|
43
|
+
function addProperty(obj, name, property) {
|
|
44
|
+
const schema = isObjectSchema(obj) ? obj.schema : obj;
|
|
45
|
+
if (property.nullable) {
|
|
46
|
+
schema.optionalProperties ??= {};
|
|
47
|
+
schema.optionalProperties[name] = {
|
|
48
|
+
...property,
|
|
49
|
+
metadata: {
|
|
50
|
+
...property.metadata,
|
|
51
|
+
name
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
} else {
|
|
55
|
+
schema.properties ??= {};
|
|
56
|
+
schema.properties[name] = {
|
|
57
|
+
...property,
|
|
58
|
+
metadata: {
|
|
59
|
+
...property.metadata,
|
|
60
|
+
name
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Merges multiple JTD object schemas into a single schema. This function takes an array of JTD object schemas (either {@link JTDSchemaObjectType} or {@link ObjectSchema}) and combines their properties into a single JTD object schema. The resulting schema will contain all properties from the input schemas, with optional properties marked accordingly. If there are overlapping properties between the input schemas, the function will merge them using a deep merge strategy (via `defu`) if they are both JTD object schemas; otherwise, the property from the first schema in the input array will take precedence.
|
|
67
|
+
*
|
|
68
|
+
* @remarks
|
|
69
|
+
* This function attempts to mimic {@link defu}'s behavior for merging objects, but with special handling for JTD schemas. When merging properties, if a property exists in multiple input schemas and is defined as a JTD object schema in all of them, the function will perform a deep merge of those schemas. If the property is not a JTD object schema in any of the input schemas, the function will simply take the first definition it encounters.
|
|
70
|
+
*
|
|
71
|
+
* @param schemas - An array of JTD object schemas to merge.
|
|
72
|
+
* @returns A single JTD object schema that combines the properties of all input schemas.
|
|
73
|
+
*/
|
|
74
|
+
function mergeSchemas(...schemas) {
|
|
75
|
+
const mergedSchema = {
|
|
76
|
+
properties: {},
|
|
77
|
+
optionalProperties: {},
|
|
78
|
+
additionalProperties: false
|
|
79
|
+
};
|
|
80
|
+
for (const schema of schemas.reverse()) {
|
|
81
|
+
const properties = getProperties(schema);
|
|
82
|
+
for (const [key, value] of Object.entries(properties)) if (value.optional) mergedSchema.optionalProperties[key] = mergedSchema.optionalProperties[key] && isJTDSchemaObject(mergedSchema.optionalProperties[key]) && isJTDSchemaObject(value) ? defu$1(mergedSchema.optionalProperties[key], value) : value;
|
|
83
|
+
else mergedSchema.properties[key] = mergedSchema.properties[key] && isJTDSchemaObject(mergedSchema.properties[key]) && isJTDSchemaObject(value) ? defu$1(mergedSchema.properties[key], value) : value;
|
|
84
|
+
}
|
|
85
|
+
return mergedSchema;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
export { addProperty, getProperties, getPropertiesList, mergeSchemas };
|
|
90
|
+
//# sourceMappingURL=helpers.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.mjs","names":["defu"],"sources":["../src/helpers.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isSetObject } from \"@stryke/type-checks\";\nimport { defu } from \"defu\";\nimport { isJTDSchemaObject, isObjectSchema } from \"./type-checks\";\nimport {\n JTDSchemaObjectType,\n JTDSchemaType,\n ObjectSchema,\n SchemaMetadata\n} from \"./types\";\n\n/**\n * A helper function to extract the properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns a record of its properties, where each key is the property name and the value is the corresponding JTD schema type along with an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function checks both `properties` and `optionalProperties` of the JTD schema to construct the resulting record.\n *\n * @param obj - The {@link ObjectSchema} from which to extract the properties.\n * @returns A record of the properties defined in the JTD object schema, where each key is the property name and the value is an object containing the JTD schema type and an `optional` flag.\n */\nexport function getProperties<TMetadata extends SchemaMetadata>(\n obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>\n): Record<\n string,\n JTDSchemaType<TMetadata> & { name: string; optional: boolean }\n> {\n const properties: Record<\n string,\n JTDSchemaType<TMetadata> & { name: string; optional: boolean }\n > = {};\n\n const schema = isObjectSchema(obj) ? obj.schema : obj;\n if (\n \"optionalProperties\" in schema &&\n isSetObject(schema.optionalProperties)\n ) {\n for (const [key, value] of Object.entries(schema.optionalProperties)) {\n properties[key] = { ...value, name: key, optional: true };\n }\n }\n\n if (\"properties\" in schema && isSetObject(schema.properties)) {\n for (const [key, value] of Object.entries(schema.properties)) {\n properties[key] = { ...value, name: key, optional: false };\n }\n }\n\n return properties;\n}\n\n/**\n * A helper function to get a list of properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns an array of its properties, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function utilizes the `getProperties` helper function to first retrieve the properties as a record and then converts that record into an array format for easier iteration and access.\n *\n * @param obj - The {@link ObjectSchema} from which to extract the properties.\n * @returns An array of properties defined in the JTD object schema, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag.\n */\nexport function getPropertiesList<TMetadata extends SchemaMetadata>(\n obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>\n): Array<JTDSchemaType<TMetadata> & { name: string; optional: boolean }> {\n return Object.values(getProperties(obj));\n}\n\n/**\n * A helper function to add a property to a JTD object schema. This function takes a JTD object schema (either {@link JTDSchemaObjectType} or {@link ObjectSchema}), a property name, and a JTD schema type as input. It adds the specified property to the appropriate section of the JTD schema based on whether the property is optional or required. If the property is optional (i.e., has `nullable: true`), it is added to the `optionalProperties` section of the schema; otherwise, it is added to the `properties` section. The function also ensures that the property's metadata includes its name for easier identification and access in future operations.\n *\n * @param obj - The JTD object schema to which the property should be added.\n * @param name - The name of the property to add.\n * @param property - The JTD schema type of the property to add, which may include a `nullable` flag indicating whether the property is optional.\n */\nexport function addProperty<TMetadata extends SchemaMetadata>(\n obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>,\n name: string,\n property: JTDSchemaType<TMetadata>\n) {\n const schema = isObjectSchema(obj) ? obj.schema : obj;\n if (property.nullable) {\n schema.optionalProperties ??= {};\n schema.optionalProperties[name] = {\n ...property,\n metadata: { ...property.metadata, name } as TMetadata\n };\n } else {\n schema.properties ??= {};\n schema.properties[name] = {\n ...property,\n metadata: { ...property.metadata, name } as TMetadata\n };\n }\n}\n\n/**\n * Merges multiple JTD object schemas into a single schema. This function takes an array of JTD object schemas (either {@link JTDSchemaObjectType} or {@link ObjectSchema}) and combines their properties into a single JTD object schema. The resulting schema will contain all properties from the input schemas, with optional properties marked accordingly. If there are overlapping properties between the input schemas, the function will merge them using a deep merge strategy (via `defu`) if they are both JTD object schemas; otherwise, the property from the first schema in the input array will take precedence.\n *\n * @remarks\n * This function attempts to mimic {@link defu}'s behavior for merging objects, but with special handling for JTD schemas. When merging properties, if a property exists in multiple input schemas and is defined as a JTD object schema in all of them, the function will perform a deep merge of those schemas. If the property is not a JTD object schema in any of the input schemas, the function will simply take the first definition it encounters.\n *\n * @param schemas - An array of JTD object schemas to merge.\n * @returns A single JTD object schema that combines the properties of all input schemas.\n */\nexport function mergeSchemas<TMetadata extends SchemaMetadata>(\n ...schemas: (JTDSchemaObjectType<TMetadata> | ObjectSchema<TMetadata>)[]\n): JTDSchemaObjectType<TMetadata> {\n const mergedSchema = {\n properties: {},\n optionalProperties: {},\n additionalProperties: false\n } as JTDSchemaObjectType<TMetadata>;\n for (const schema of schemas.reverse()) {\n const properties = getProperties(schema);\n for (const [key, value] of Object.entries(properties)) {\n if (value.optional) {\n mergedSchema.optionalProperties![key] = (\n mergedSchema.optionalProperties![key] &&\n isJTDSchemaObject(mergedSchema.optionalProperties![key]) &&\n isJTDSchemaObject(value)\n ? defu(mergedSchema.optionalProperties![key], value)\n : value\n ) as JTDSchemaType<TMetadata>;\n } else {\n mergedSchema.properties![key] = (\n mergedSchema.properties![key] &&\n isJTDSchemaObject(mergedSchema.properties![key]) &&\n isJTDSchemaObject(value)\n ? defu(mergedSchema.properties![key], value)\n : value\n ) as JTDSchemaType<TMetadata>;\n }\n }\n }\n\n return mergedSchema;\n}\n"],"mappings":";;;;;;;;;;;AAkCA,SAAgB,cACd,KAIA;CACA,MAAM,aAGF,CAAC;CAEL,MAAM,SAAS,eAAe,GAAG,IAAI,IAAI,SAAS;CAClD,IACE,wBAAwB,UACxB,YAAY,OAAO,kBAAkB,GAErC,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,kBAAkB,GACjE,WAAW,OAAO;EAAE,GAAG;EAAO,MAAM;EAAK,UAAU;CAAK;CAI5D,IAAI,gBAAgB,UAAU,YAAY,OAAO,UAAU,GACzD,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,UAAU,GACzD,WAAW,OAAO;EAAE,GAAG;EAAO,MAAM;EAAK,UAAU;CAAM;CAI7D,OAAO;AACT;;;;;;;AAQA,SAAgB,kBACd,KACuE;CACvE,OAAO,OAAO,OAAO,cAAc,GAAG,CAAC;AACzC;;;;;;;;AASA,SAAgB,YACd,KACA,MACA,UACA;CACA,MAAM,SAAS,eAAe,GAAG,IAAI,IAAI,SAAS;CAClD,IAAI,SAAS,UAAU;EACrB,OAAO,uBAAuB,CAAC;EAC/B,OAAO,mBAAmB,QAAQ;GAChC,GAAG;GACH,UAAU;IAAE,GAAG,SAAS;IAAU;GAAK;EACzC;CACF,OAAO;EACL,OAAO,eAAe,CAAC;EACvB,OAAO,WAAW,QAAQ;GACxB,GAAG;GACH,UAAU;IAAE,GAAG,SAAS;IAAU;GAAK;EACzC;CACF;AACF;;;;;;;;;;AAWA,SAAgB,aACd,GAAG,SAC6B;CAChC,MAAM,eAAe;EACnB,YAAY,CAAC;EACb,oBAAoB,CAAC;EACrB,sBAAsB;CACxB;CACA,KAAK,MAAM,UAAU,QAAQ,QAAQ,GAAG;EACtC,MAAM,aAAa,cAAc,MAAM;EACvC,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,UAAU,GAClD,IAAI,MAAM,UACR,aAAa,mBAAoB,OAC/B,aAAa,mBAAoB,QACjC,kBAAkB,aAAa,mBAAoB,IAAI,KACvD,kBAAkB,KAAK,IACnBA,OAAK,aAAa,mBAAoB,MAAM,KAAK,IACjD;OAGN,aAAa,WAAY,OACvB,aAAa,WAAY,QACzB,kBAAkB,aAAa,WAAY,IAAI,KAC/C,kBAAkB,KAAK,IACnBA,OAAK,aAAa,WAAY,MAAM,KAAK,IACzC;CAIZ;CAEA,OAAO;AACT"}
|
package/dist/index.cjs
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_bundle = require('./bundle.cjs');
|
|
3
|
+
const require_type_checks = require('./type-checks.cjs');
|
|
4
|
+
const require_helpers = require('./helpers.cjs');
|
|
3
5
|
const require_codegen = require('./codegen.cjs');
|
|
6
|
+
const require_constants = require('./constants.cjs');
|
|
4
7
|
const require_jtd = require('./jtd.cjs');
|
|
8
|
+
const require_persistence = require('./persistence.cjs');
|
|
5
9
|
const require_reflection = require('./reflection.cjs');
|
|
6
10
|
const require_resolve = require('./resolve.cjs');
|
|
7
|
-
const require_type_checks = require('./type-checks.cjs');
|
|
8
11
|
const require_extract = require('./extract.cjs');
|
|
9
12
|
|
|
13
|
+
exports.JTDTypes = require_constants.JTDTypes;
|
|
14
|
+
exports.addProperty = require_helpers.addProperty;
|
|
10
15
|
exports.bundle = require_bundle.bundle;
|
|
11
16
|
exports.extract = require_extract.extract;
|
|
12
17
|
exports.extractHash = require_extract.extractHash;
|
|
@@ -18,13 +23,25 @@ exports.extractSchemaSchema = require_extract.extractSchemaSchema;
|
|
|
18
23
|
exports.extractSource = require_extract.extractSource;
|
|
19
24
|
exports.extractVariant = require_extract.extractVariant;
|
|
20
25
|
exports.generateCode = require_codegen.generateCode;
|
|
26
|
+
exports.getCacheDirectory = require_persistence.getCacheDirectory;
|
|
27
|
+
exports.getCacheFilePath = require_persistence.getCacheFilePath;
|
|
28
|
+
exports.getProperties = require_helpers.getProperties;
|
|
29
|
+
exports.getPropertiesList = require_helpers.getPropertiesList;
|
|
21
30
|
exports.isExtractedSchema = require_type_checks.isExtractedSchema;
|
|
22
31
|
exports.isJTDSchema = require_type_checks.isJTDSchema;
|
|
32
|
+
exports.isJTDSchemaObject = require_type_checks.isJTDSchemaObject;
|
|
33
|
+
exports.isObjectSchema = require_type_checks.isObjectSchema;
|
|
23
34
|
exports.isSchema = require_type_checks.isSchema;
|
|
24
35
|
exports.isUntypedInput = require_type_checks.isUntypedInput;
|
|
25
36
|
exports.isUntypedSchema = require_type_checks.isUntypedSchema;
|
|
26
37
|
exports.jsonSchemaToJtd = require_jtd.jsonSchemaToJtd;
|
|
38
|
+
exports.mergeSchemas = require_helpers.mergeSchemas;
|
|
39
|
+
exports.readSchema = require_persistence.readSchema;
|
|
40
|
+
exports.readSchemaSafe = require_persistence.readSchemaSafe;
|
|
27
41
|
exports.reflectionToJsonSchema = require_reflection.reflectionToJsonSchema;
|
|
28
42
|
exports.resolve = require_resolve.resolve;
|
|
29
43
|
exports.resolveModule = require_resolve.resolveModule;
|
|
30
|
-
exports.resolveReflection = require_resolve.resolveReflection;
|
|
44
|
+
exports.resolveReflection = require_resolve.resolveReflection;
|
|
45
|
+
exports.stringifyType = require_codegen.stringifyType;
|
|
46
|
+
exports.stringifyValue = require_codegen.stringifyValue;
|
|
47
|
+
exports.writeSchema = require_persistence.writeSchema;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { BundleOptions, bundle } from "./bundle.cjs";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { BaseSchemaSource, ExtractedSchema, JTDNumberType, JTDSchemaObjectForm, JTDSchemaObjectType, JTDSchemaSchemaSource, JTDSchemaType, JTDStringType, JTDType, JsonSchemaLike, JsonSchemaSchemaSource, ObjectSchema, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource } from "./types.cjs";
|
|
3
|
+
import { generateCode, stringifyType, stringifyValue } from "./codegen.cjs";
|
|
4
|
+
import { JTDTypes } from "./constants.cjs";
|
|
4
5
|
import { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant } from "./extract.cjs";
|
|
6
|
+
import { addProperty, getProperties, getPropertiesList, mergeSchemas } from "./helpers.cjs";
|
|
5
7
|
import { jsonSchemaToJtd } from "./jtd.cjs";
|
|
8
|
+
import { getCacheDirectory, getCacheFilePath, readSchema, readSchemaSafe, writeSchema } from "./persistence.cjs";
|
|
6
9
|
import { reflectionToJsonSchema } from "./reflection.cjs";
|
|
7
10
|
import { resolve, resolveModule, resolveReflection } from "./resolve.cjs";
|
|
8
|
-
import { isExtractedSchema, isJTDSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.cjs";
|
|
9
|
-
export { BaseSchemaSource, BundleOptions, ExtractedSchema, JTDNumberType, JTDSchemaSchemaSource, JTDSchemaType, JTDStringType, JTDType, JsonSchemaLike, JsonSchemaSchemaSource, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, isExtractedSchema, isJTDSchema, isSchema, isUntypedInput, isUntypedSchema, jsonSchemaToJtd, reflectionToJsonSchema, resolve, resolveModule, resolveReflection };
|
|
11
|
+
import { isExtractedSchema, isJTDSchema, isJTDSchemaObject, isObjectSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.cjs";
|
|
12
|
+
export { BaseSchemaSource, BundleOptions, ExtractedSchema, JTDNumberType, JTDSchemaObjectForm, JTDSchemaObjectType, JTDSchemaSchemaSource, JTDSchemaType, JTDStringType, JTDType, JTDTypes, JsonSchemaLike, JsonSchemaSchemaSource, ObjectSchema, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource, addProperty, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getProperties, getPropertiesList, isExtractedSchema, isJTDSchema, isJTDSchemaObject, isObjectSchema, isSchema, isUntypedInput, isUntypedSchema, jsonSchemaToJtd, mergeSchemas, readSchema, readSchemaSafe, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { BundleOptions, bundle } from "./bundle.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { BaseSchemaSource, ExtractedSchema, JTDNumberType, JTDSchemaObjectForm, JTDSchemaObjectType, JTDSchemaSchemaSource, JTDSchemaType, JTDStringType, JTDType, JsonSchemaLike, JsonSchemaSchemaSource, ObjectSchema, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource } from "./types.mjs";
|
|
3
|
+
import { generateCode, stringifyType, stringifyValue } from "./codegen.mjs";
|
|
4
|
+
import { JTDTypes } from "./constants.mjs";
|
|
4
5
|
import { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant } from "./extract.mjs";
|
|
6
|
+
import { addProperty, getProperties, getPropertiesList, mergeSchemas } from "./helpers.mjs";
|
|
5
7
|
import { jsonSchemaToJtd } from "./jtd.mjs";
|
|
8
|
+
import { getCacheDirectory, getCacheFilePath, readSchema, readSchemaSafe, writeSchema } from "./persistence.mjs";
|
|
6
9
|
import { reflectionToJsonSchema } from "./reflection.mjs";
|
|
7
10
|
import { resolve, resolveModule, resolveReflection } from "./resolve.mjs";
|
|
8
|
-
import { isExtractedSchema, isJTDSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
|
|
9
|
-
export { BaseSchemaSource, BundleOptions, ExtractedSchema, JTDNumberType, JTDSchemaSchemaSource, JTDSchemaType, JTDStringType, JTDType, JsonSchemaLike, JsonSchemaSchemaSource, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, isExtractedSchema, isJTDSchema, isSchema, isUntypedInput, isUntypedSchema, jsonSchemaToJtd, reflectionToJsonSchema, resolve, resolveModule, resolveReflection };
|
|
11
|
+
import { isExtractedSchema, isJTDSchema, isJTDSchemaObject, isObjectSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
|
|
12
|
+
export { BaseSchemaSource, BundleOptions, ExtractedSchema, JTDNumberType, JTDSchemaObjectForm, JTDSchemaObjectType, JTDSchemaSchemaSource, JTDSchemaType, JTDStringType, JTDType, JTDTypes, JsonSchemaLike, JsonSchemaSchemaSource, ObjectSchema, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource, addProperty, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getProperties, getPropertiesList, isExtractedSchema, isJTDSchema, isJTDSchemaObject, isObjectSchema, isSchema, isUntypedInput, isUntypedSchema, jsonSchemaToJtd, mergeSchemas, readSchema, readSchemaSafe, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { bundle } from "./bundle.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { isExtractedSchema, isJTDSchema, isJTDSchemaObject, isObjectSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
|
|
3
|
+
import { addProperty, getProperties, getPropertiesList, mergeSchemas } from "./helpers.mjs";
|
|
4
|
+
import { generateCode, stringifyType, stringifyValue } from "./codegen.mjs";
|
|
5
|
+
import { JTDTypes } from "./constants.mjs";
|
|
3
6
|
import { jsonSchemaToJtd } from "./jtd.mjs";
|
|
7
|
+
import { getCacheDirectory, getCacheFilePath, readSchema, readSchemaSafe, writeSchema } from "./persistence.mjs";
|
|
4
8
|
import { reflectionToJsonSchema } from "./reflection.mjs";
|
|
5
9
|
import { resolve, resolveModule, resolveReflection } from "./resolve.mjs";
|
|
6
|
-
import { isExtractedSchema, isJTDSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
|
|
7
10
|
import { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant } from "./extract.mjs";
|
|
8
11
|
|
|
9
|
-
export { bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, isExtractedSchema, isJTDSchema, isSchema, isUntypedInput, isUntypedSchema, jsonSchemaToJtd, reflectionToJsonSchema, resolve, resolveModule, resolveReflection };
|
|
12
|
+
export { JTDTypes, addProperty, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getProperties, getPropertiesList, isExtractedSchema, isJTDSchema, isJTDSchemaObject, isObjectSchema, isSchema, isUntypedInput, isUntypedSchema, jsonSchemaToJtd, mergeSchemas, readSchema, readSchemaSafe, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
|