@powerlines/schema 0.11.40 → 0.11.42

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.
Files changed (55) hide show
  1. package/dist/bundle.cjs +0 -1
  2. package/dist/codegen.cjs +12 -19
  3. package/dist/codegen.d.cts +7 -2
  4. package/dist/codegen.d.cts.map +1 -1
  5. package/dist/codegen.d.mts +7 -2
  6. package/dist/codegen.d.mts.map +1 -1
  7. package/dist/codegen.mjs +13 -18
  8. package/dist/codegen.mjs.map +1 -1
  9. package/dist/constants.cjs +0 -2
  10. package/dist/constants.d.cts +1 -1
  11. package/dist/constants.d.mts +1 -1
  12. package/dist/constants.mjs +0 -1
  13. package/dist/constants.mjs.map +1 -1
  14. package/dist/extract.cjs +23 -9
  15. package/dist/extract.d.cts +2 -3
  16. package/dist/extract.d.cts.map +1 -1
  17. package/dist/extract.d.mts +2 -3
  18. package/dist/extract.d.mts.map +1 -1
  19. package/dist/extract.mjs +28 -12
  20. package/dist/extract.mjs.map +1 -1
  21. package/dist/helpers.cjs +0 -1
  22. package/dist/helpers.d.cts.map +1 -1
  23. package/dist/helpers.d.mts.map +1 -1
  24. package/dist/helpers.mjs.map +1 -1
  25. package/dist/index.cjs +2 -1
  26. package/dist/index.d.cts +30 -4
  27. package/dist/index.d.cts.map +1 -0
  28. package/dist/index.d.mts +30 -4
  29. package/dist/index.d.mts.map +1 -0
  30. package/dist/index.mjs +3 -3
  31. package/dist/metadata.cjs +1 -2
  32. package/dist/metadata.d.cts.map +1 -1
  33. package/dist/metadata.d.mts.map +1 -1
  34. package/dist/metadata.mjs +1 -1
  35. package/dist/metadata.mjs.map +1 -1
  36. package/dist/persistence.cjs +0 -1
  37. package/dist/reflection.cjs +0 -1
  38. package/dist/resolve.cjs +0 -1
  39. package/dist/type-checks.cjs +27 -2
  40. package/dist/type-checks.d.cts +17 -1
  41. package/dist/type-checks.d.cts.map +1 -1
  42. package/dist/type-checks.d.mts +17 -1
  43. package/dist/type-checks.d.mts.map +1 -1
  44. package/dist/type-checks.mjs +25 -1
  45. package/dist/type-checks.mjs.map +1 -1
  46. package/dist/types.d.cts +43 -28
  47. package/dist/types.d.cts.map +1 -1
  48. package/dist/types.d.mts +44 -29
  49. package/dist/types.d.mts.map +1 -1
  50. package/dist/validate.cjs +27 -0
  51. package/dist/validate.mjs +25 -0
  52. package/dist/validate.mjs.map +1 -0
  53. package/package.json +20 -49
  54. package/dist/types.cjs +0 -0
  55. package/dist/types.mjs +0 -1
@@ -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 { rolldownPlugin } from \"@powerlines/deepkit/rolldown-plugin\";\nimport { isType, stringifyType, Type } from \"@powerlines/deepkit/vendor/type\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { murmurhash } from \"@stryke/hash\";\nimport { isStandardJsonSchema } 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 \"rolldown\";\nimport * as z3 from \"zod/v3\";\nimport { getCacheDirectory, writeSchema } from \"./persistence\";\nimport { reflectionToJsonSchema } from \"./reflection\";\nimport { resolve } from \"./resolve\";\nimport {\n isExtractedSchema,\n isJsonSchema,\n isSchema,\n isUntypedInput,\n isUntypedSchema\n} from \"./type-checks\";\nimport {\n ExtractedSchema,\n JsonSchema,\n Schema,\n SchemaInput,\n SchemaInputVariant,\n SchemaSource,\n SchemaSourceInput,\n SchemaSourceVariant,\n TypeDefinitionReference,\n UntypedInputObject,\n UntypedSchema\n} from \"./types\";\n\nfunction convertNestedUntypedSchema(value: unknown): unknown {\n if (isUntypedSchema(value)) {\n return convertUntypedSchemaToJsonSchema(value);\n }\n\n if (isSetObject(value)) {\n if (isUntypedInput(value)) {\n return convertUntypedInputToJsonSchema(value);\n }\n\n const nested = value as Record<string, unknown>;\n if (\"$schema\" in nested && isUntypedSchema(nested.$schema)) {\n return convertUntypedSchemaToJsonSchema(nested.$schema);\n }\n }\n\n return value;\n}\n\nfunction convertNestedUntypedSchemaArray(value: unknown): unknown {\n if (!Array.isArray(value)) {\n return value;\n }\n\n return value.map(item => convertNestedUntypedSchema(item));\n}\n\nfunction convertUntypedSchemaToJsonSchema<T = unknown>(\n schema: UntypedSchema | Record<string, unknown>\n): JsonSchema<T> {\n const source = schema as Record<string, unknown>;\n const jsonSchema: Record<string, unknown> = {};\n\n for (const [key, value] of Object.entries(source)) {\n if (\n key === \"tsType\" ||\n key === \"markdownType\" ||\n key === \"tags\" ||\n key === \"args\" ||\n key === \"resolve\"\n ) {\n continue;\n }\n\n if (key === \"id\" && isSetString(value)) {\n jsonSchema.$id = value;\n continue;\n }\n\n if (\n key === \"properties\" ||\n key === \"patternProperties\" ||\n key === \"dependentSchemas\" ||\n key === \"$defs\" ||\n key === \"definitions\"\n ) {\n if (!isSetObject(value)) {\n jsonSchema[key] = value;\n continue;\n }\n\n jsonSchema[key] = Object.fromEntries(\n Object.entries(value).map(([propertyKey, propertyValue]) => [\n propertyKey,\n convertNestedUntypedSchema(propertyValue)\n ])\n );\n continue;\n }\n\n if (\n key === \"items\" ||\n key === \"contains\" ||\n key === \"if\" ||\n key === \"then\" ||\n key === \"else\" ||\n key === \"not\" ||\n key === \"propertyNames\" ||\n key === \"additionalProperties\" ||\n key === \"unevaluatedProperties\"\n ) {\n jsonSchema[key] = convertNestedUntypedSchema(value);\n continue;\n }\n\n if (key === \"oneOf\" || key === \"anyOf\" || key === \"allOf\") {\n jsonSchema[key] = convertNestedUntypedSchemaArray(value);\n continue;\n }\n\n jsonSchema[key] = value;\n }\n\n return jsonSchema as JsonSchema<T>;\n}\n\nfunction convertUntypedInputToJsonSchema<T = unknown>(\n input: UntypedInputObject\n): JsonSchema<T> {\n const inputObject = input as Record<string, unknown>;\n const base = isUntypedSchema(inputObject.$schema)\n ? convertUntypedSchemaToJsonSchema<T>(inputObject.$schema)\n : ({} as JsonSchema<T>);\n const properties: Record<string, JsonSchema<T>> = {};\n\n for (const [key, value] of Object.entries(inputObject)) {\n if (key.startsWith(\"$\")) {\n continue;\n }\n\n if (!isSetObject(value)) {\n continue;\n }\n\n if (isUntypedInput(value)) {\n properties[key] = convertUntypedInputToJsonSchema<T>(value);\n continue;\n }\n\n const nested = value as Record<string, unknown>;\n if (\"$schema\" in nested && isUntypedSchema(nested.$schema)) {\n properties[key] = convertUntypedSchemaToJsonSchema<T>(nested.$schema);\n continue;\n }\n\n if (isUntypedSchema(value)) {\n properties[key] = convertUntypedSchemaToJsonSchema<T>(value);\n }\n }\n\n const baseProperties = isSetObject(base.properties)\n ? (base.properties as Record<string, JsonSchema<T>>)\n : {};\n const mergedProperties = {\n ...baseProperties,\n ...properties\n };\n\n return {\n ...base,\n type: base.type ?? \"object\",\n ...(Object.keys(mergedProperties).length > 0\n ? { properties: mergedProperties }\n : {})\n } as JsonSchema<T>;\n}\n\n/**\n * Creates a hash string for a given schema definition input.\n */\nexport function extractHash<T = unknown>(\n variant: SchemaInputVariant,\n input: SchemaInput<T>\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 (isJsonSchema(input)) {\n return murmurhash({ variant, input });\n } else if (isUntypedInput(input)) {\n return murmurhash({\n variant,\n input: convertUntypedInputToJsonSchema(input)\n });\n } else if (isUntypedSchema(input)) {\n return murmurhash({\n variant,\n input: convertUntypedSchemaToJsonSchema(input)\n });\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 Type} into a JSON Schema (draft-07) representation.\n */\nexport function extractReflection<T = unknown>(\n reflection: Type\n): JsonSchema<T> | undefined {\n if (!isType(reflection)) {\n return undefined;\n }\n\n return reflectionToJsonSchema<T>(reflection);\n}\n\n/**\n * Extracts a JSON Schema from Zod, Standard Schema, untyped, or JSON Schema inputs.\n */\nexport function extractJsonSchema<T = unknown>(\n schema: unknown\n): JsonSchema<T> | undefined {\n if (\n isSetObject(schema) &&\n (isZod3Type(schema) ||\n isStandardJsonSchema(schema) ||\n isJsonSchema(schema) ||\n isUntypedInput(schema) ||\n isUntypedSchema(schema))\n ) {\n if (isZod3Type(schema)) {\n return extractJsonSchemaZod(schema) as JsonSchema<T>;\n }\n if (isStandardJsonSchema(schema)) {\n return schema[\"~standard\"].jsonSchema.input({\n target: \"draft-2020-12\"\n }) as JsonSchema<T>;\n }\n if (isUntypedInput(schema)) {\n return convertUntypedInputToJsonSchema<T>(schema);\n }\n if (isUntypedSchema(schema)) {\n return convertUntypedSchemaToJsonSchema<T>(schema);\n }\n return schema;\n }\n\n return undefined;\n}\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 (isJsonSchema(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\nexport function extractVariant<T = unknown>(\n input: SchemaInput<T>\n): SchemaInputVariant {\n if (isSetString(input) || isTypeDefinition(input)) {\n return \"type-definition\";\n }\n\n return extractResolvedVariant(input as SchemaSourceInput);\n}\n\nexport async function extractSchemaSchema<T = unknown>(\n input: SchemaSourceInput,\n variant?: SchemaInputVariant\n): Promise<JsonSchema<T>> {\n if (isExtractedSchema<T>(input)) {\n return input.schema;\n }\n\n const resolvedVariant = variant ?? extractResolvedVariant(input);\n\n let schema: JsonSchema<T> | undefined;\n if (\n resolvedVariant === \"zod3\" ||\n resolvedVariant === \"json-schema\" ||\n resolvedVariant === \"standard-schema\" ||\n resolvedVariant === \"untyped\"\n ) {\n schema = extractJsonSchema<T>(input);\n } else if (resolvedVariant === \"reflection\") {\n schema = extractReflection(input as Type);\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, an untyped schema, or a reflected Deepkit Type object.`\n );\n}\n\nexport function extractSource(\n variant: SchemaSourceVariant,\n input: SchemaSourceInput\n): 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 JsonSchema\n };\n } else if (variant === \"reflection\") {\n return {\n hash: extractHash(variant, input),\n variant: \"reflection\",\n schema: input as Type\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, an untyped schema, or a reflected Deepkit Type object.`\n );\n}\n\n/**\n * Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a type definition reference (e.g. a file path with an export), it will be resolved and bundled using ESBuild to obtain the actual schema definition before extraction.\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 *\n * @param context - The context object providing access to the file system and cache path.\n * @param input - The schema definition input to extract, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a string or a type definition reference, it will be resolved and bundled to obtain the actual schema definition before extraction.\n * @param options - Optional overrides for the ESBuild configuration used during extraction. This can include custom plugins, loaders, or other build options to control how the schema definition is resolved and bundled when the input is a type definition reference.\n * @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object. The function will attempt to extract a valid JSON Schema from the provided input, and if successful, it will return the schema. If the extraction process fails or if the input is not a valid schema definition, it will throw an error indicating the failure.\n */\nexport async function extractSchema<T = unknown>(\n context: Context,\n input: SchemaInput,\n options: Partial<BuildOptions> = {}\n): Promise<ExtractedSchema<T>> {\n if (isExtractedSchema<T>(input)) {\n return input;\n }\n\n if (isSchema<T>(input)) {\n return {\n ...input,\n source: {\n hash: extractHash(\"json-schema\", input.schema),\n variant: \"json-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 rolldownPlugin(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 \"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\", \"standard-schema\", \"zod3\", \"untyped\", or \"reflection\".`\n );\n }\n\n return {\n variant,\n source,\n schema: await extractSchemaSchema<T>(source.schema, source.variant),\n hash\n };\n}\n\n/**\n * Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a type definition reference (e.g. a file path with an export), it will be resolved and bundled using ESBuild to obtain the actual schema definition before extraction.\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 context object providing access to the file system and cache path.\n * @param input - The schema definition input to extract, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.\n * @param options - Optional overrides for the ESBuild configuration used during extraction.\n * @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object.\n * @throws Will throw an error if the input is not a valid schema definition or if the extraction process fails to produce a valid schema.\n */\nexport async function extract<T = unknown>(\n context: Context,\n input: SchemaInput,\n options: Partial<BuildOptions> = {}\n): Promise<Schema<T>> {\n if (isExtractedSchema<T>(input) || isSchema<T>(input)) {\n return input;\n }\n\n let result: Schema<T> | 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 JsonSchema\n };\n }\n }\n\n result ??= await extractSchema<T>(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, 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":";;;;;;;;;;;;;;;;AA2DA,SAAS,2BAA2B,OAAyB;CAC3D,IAAI,gBAAgB,KAAK,GACvB,OAAO,iCAAiC,KAAK;CAG/C,IAAIA,cAAY,KAAK,GAAG;EACtB,IAAI,eAAe,KAAK,GACtB,OAAO,gCAAgC,KAAK;EAG9C,MAAM,SAAS;EACf,IAAI,aAAa,UAAU,gBAAgB,OAAO,OAAO,GACvD,OAAO,iCAAiC,OAAO,OAAO;CAE1D;CAEA,OAAO;AACT;AAEA,SAAS,gCAAgC,OAAyB;C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;;;;;;;;;;;;AA+BA,eAAsB,QACpB,SACA,OACA,UAAiC,CAAC,GACd;CACpB,IAAI,kBAAqB,KAAK,KAAK,SAAY,KAAK,GAClD,OAAO;CAGT,IAAI;CAEJ,MAAM,UAAU,eAAe,KAAK;CACpC,MAAM,OAAO,YAAY,SAAS,KAAK;CAEvC,MAAM,gBAAgB,UAAU,kBAAkB,OAAO,GAAG,GAAG,KAAK,MAAM;CAC1E,IACE,QAAQ,OAAO,cAAc,QAC7B,QAAQ,GAAG,WAAW,aAAa,GACnC;EACA,MAAM,SAAS,MAAM,QAAQ,GAAG,KAAK,aAAa;EAClD,IAAI,QACF,SAAS;GACP;GACA;GACA,QAAQ,KAAK,MAAM,MAAM;EAC3B;CAEJ;CAEA,WAAW,MAAM,cAAiB,SAAS,OAAO,OAAO;CACzD,IAAI,CAAC,QAAQ,QACX,MAAM,IAAI,MACR,gMACF;CAGF,IAAI,QAAQ,OAAO,cAAc,MAC/B,MAAM,YAAY,SAAS,MAAM;CAGnC,OAAO;AACT"}
1
+ {"version":3,"file":"extract.mjs","names":["isSetObject","extractJsonSchema","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 { rolldownPlugin } from \"@powerlines/deepkit/rolldown-plugin\";\nimport { isType, stringifyType, Type } from \"@powerlines/deepkit/vendor/type\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { murmurhash } from \"@stryke/hash\";\nimport { isStandardJsonSchema } 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 { toJsonSchema } from \"@valibot/to-json-schema\";\nimport defu from \"defu\";\nimport type { BuildOptions } from \"rolldown\";\nimport * as z3 from \"zod/v3\";\nimport { getCacheDirectory, writeSchema } from \"./persistence\";\nimport { reflectionToJsonSchema } from \"./reflection\";\nimport { resolve } from \"./resolve\";\nimport {\n isExtractedSchema,\n isJsonSchema,\n isJsonSchemaObject,\n isSchema,\n isUntypedInput,\n isUntypedSchema,\n isValibotSchema\n} from \"./type-checks\";\nimport {\n ExtractedSchema,\n JsonSchema,\n Schema,\n SchemaInput,\n SchemaInputVariant,\n SchemaSource,\n SchemaSourceInput,\n SchemaSourceVariant,\n TypeDefinitionReference,\n UntypedInputObject,\n UntypedSchema,\n ValibotSchema\n} from \"./types\";\n\nfunction convertNestedUntypedSchema(value: unknown): unknown {\n if (isUntypedSchema(value)) {\n return convertUntypedSchemaToJsonSchema(value);\n }\n\n if (isSetObject(value)) {\n if (isUntypedInput(value)) {\n return convertUntypedInputToJsonSchema(value);\n }\n\n const nested = value as Record<string, unknown>;\n if (\"$schema\" in nested && isUntypedSchema(nested.$schema)) {\n return convertUntypedSchemaToJsonSchema(nested.$schema);\n }\n }\n\n return value;\n}\n\nfunction convertNestedUntypedSchemaArray(value: unknown): unknown {\n if (!Array.isArray(value)) {\n return value;\n }\n\n return value.map(item => convertNestedUntypedSchema(item));\n}\n\nfunction convertValibotSchemaToJsonSchema<T = unknown>(\n schema: unknown\n): JsonSchema<T> {\n return toJsonSchema(schema as never, {\n target: \"draft-07\"\n }) as JsonSchema<T>;\n}\n\nfunction convertUntypedSchemaToJsonSchema<T = unknown>(\n schema: UntypedSchema | Record<string, unknown>\n): JsonSchema<T> {\n const source = schema as Record<string, unknown>;\n const jsonSchema: Record<string, unknown> = {};\n\n for (const [key, value] of Object.entries(source)) {\n if (\n key === \"tsType\" ||\n key === \"markdownType\" ||\n key === \"tags\" ||\n key === \"args\" ||\n key === \"resolve\"\n ) {\n continue;\n }\n\n if (key === \"id\" && isSetString(value)) {\n jsonSchema.$id = value;\n continue;\n }\n\n if (\n key === \"properties\" ||\n key === \"patternProperties\" ||\n key === \"dependentSchemas\" ||\n key === \"$defs\" ||\n key === \"definitions\"\n ) {\n if (!isSetObject(value)) {\n jsonSchema[key] = value;\n continue;\n }\n\n jsonSchema[key] = Object.fromEntries(\n Object.entries(value).map(([propertyKey, propertyValue]) => [\n propertyKey,\n convertNestedUntypedSchema(propertyValue)\n ])\n );\n continue;\n }\n\n if (\n key === \"items\" ||\n key === \"contains\" ||\n key === \"if\" ||\n key === \"then\" ||\n key === \"else\" ||\n key === \"not\" ||\n key === \"propertyNames\" ||\n key === \"additionalProperties\" ||\n key === \"unevaluatedProperties\"\n ) {\n jsonSchema[key] = convertNestedUntypedSchema(value);\n continue;\n }\n\n if (key === \"oneOf\" || key === \"anyOf\" || key === \"allOf\") {\n jsonSchema[key] = convertNestedUntypedSchemaArray(value);\n continue;\n }\n\n jsonSchema[key] = value;\n }\n\n return jsonSchema as JsonSchema<T>;\n}\n\nfunction convertUntypedInputToJsonSchema<T = unknown>(\n input: UntypedInputObject\n): JsonSchema<T> {\n const inputObject = input as Record<string, unknown>;\n const base = (\n isUntypedSchema(inputObject.$schema)\n ? convertUntypedSchemaToJsonSchema<T>(inputObject.$schema)\n : {}\n ) as JsonSchema<T>;\n const properties: Record<string, JsonSchema<T>> = {};\n\n for (const [key, value] of Object.entries(inputObject)) {\n if (key.startsWith(\"$\")) {\n continue;\n }\n\n if (!isSetObject(value)) {\n continue;\n }\n\n if (isUntypedInput(value)) {\n properties[key] = convertUntypedInputToJsonSchema<T>(value);\n continue;\n }\n\n const nested = value as Record<string, unknown>;\n if (\"$schema\" in nested && isUntypedSchema(nested.$schema)) {\n properties[key] = convertUntypedSchemaToJsonSchema<T>(nested.$schema);\n continue;\n }\n\n if (isUntypedSchema(value)) {\n properties[key] = convertUntypedSchemaToJsonSchema<T>(value);\n }\n }\n\n if (!isJsonSchemaObject(base)) {\n throw new Error(\n `Failed to convert untyped input to JSON Schema. The base schema must be a valid JSON Schema object.`\n );\n }\n\n const baseProperties = isSetObject(base.properties)\n ? (base.properties as Record<string, JsonSchema<T>>)\n : {};\n const mergedProperties = {\n ...baseProperties,\n ...properties\n };\n\n return {\n ...base,\n type: base.type ?? \"object\",\n ...(Object.keys(mergedProperties).length > 0\n ? { properties: mergedProperties }\n : {})\n };\n}\n\n/**\n * Creates a hash string for a given schema definition input.\n */\nexport function extractHash<T = unknown>(\n variant: SchemaInputVariant,\n input: SchemaInput<T>\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 (isJsonSchema(input)) {\n return murmurhash({ variant, input });\n } else if (isValibotSchema(input)) {\n return murmurhash({\n variant,\n input: convertValibotSchemaToJsonSchema(input)\n });\n } else if (isUntypedInput(input)) {\n return murmurhash({\n variant,\n input: convertUntypedInputToJsonSchema(input)\n });\n } else if (isUntypedSchema(input)) {\n return murmurhash({\n variant,\n input: convertUntypedSchemaToJsonSchema(input)\n });\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, a Valibot BaseSchema, or a reflected Deepkit Type object.`\n );\n}\n\n/**\n * Converts a reflected Deepkit {@link Type} into a JSON Schema (draft-07) representation.\n */\nexport function extractReflection<T = unknown>(\n reflection: Type\n): JsonSchema<T> | undefined {\n if (!isType(reflection)) {\n return undefined;\n }\n\n return reflectionToJsonSchema<T>(reflection);\n}\n\n/**\n * Extracts a JSON Schema from Zod, Standard Schema, Valibot, untyped, or JSON Schema inputs.\n */\nexport function extractJsonSchema<T = unknown>(\n schema: unknown\n): JsonSchema<T> | undefined {\n if (isSetObject(schema)) {\n if (isZod3Type(schema)) {\n return extractJsonSchemaZod(schema) as JsonSchema<T>;\n }\n if (isStandardJsonSchema(schema)) {\n return schema[\"~standard\"].jsonSchema.input({\n target: \"draft-2020-12\"\n }) as JsonSchema<T>;\n }\n if (isValibotSchema(schema)) {\n return convertValibotSchemaToJsonSchema<T>(schema);\n }\n if (isUntypedInput(schema)) {\n return convertUntypedInputToJsonSchema<T>(schema);\n }\n if (isUntypedSchema(schema)) {\n return convertUntypedSchemaToJsonSchema<T>(schema);\n }\n if (isJsonSchema<T>(schema)) {\n return schema;\n }\n }\n\n return undefined;\n}\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 (isJsonSchema(input)) {\n return \"json-schema\";\n } else if (isValibotSchema(input)) {\n return \"valibot\";\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 Valibot BaseSchema, a reflected Deepkit Type object, or an Untyped schema.`\n );\n}\n\nexport function extractVariant<T = unknown>(\n input: SchemaInput<T>\n): SchemaInputVariant {\n if (isSetString(input) || isTypeDefinition(input)) {\n return \"type-definition\";\n }\n\n return extractResolvedVariant(input as SchemaSourceInput);\n}\n\nasync function extractSchemaSchema<T = unknown>(\n input: SchemaSourceInput,\n variant?: SchemaInputVariant\n): Promise<JsonSchema<T>> {\n if (isExtractedSchema<T>(input)) {\n return input.schema;\n }\n\n const resolvedVariant = variant ?? extractResolvedVariant(input);\n\n let schema: JsonSchema<T> | undefined;\n if (\n resolvedVariant === \"zod3\" ||\n resolvedVariant === \"json-schema\" ||\n resolvedVariant === \"standard-schema\" ||\n resolvedVariant === \"untyped\" ||\n resolvedVariant === \"valibot\"\n ) {\n schema = extractJsonSchema<T>(input);\n } else if (resolvedVariant === \"reflection\") {\n schema = extractReflection(input as Type);\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 Valibot BaseSchema, an untyped schema, or a reflected Deepkit Type object.`\n );\n}\n\nexport function extractSource(\n variant: SchemaSourceVariant,\n input: SchemaSourceInput\n): 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 JsonSchema\n };\n } else if (variant === \"valibot\") {\n return {\n hash: extractHash(variant, input),\n variant: \"valibot\",\n schema: input as ValibotSchema\n };\n } else if (variant === \"reflection\") {\n return {\n hash: extractHash(variant, input),\n variant: \"reflection\",\n schema: input as Type\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, an untyped schema, or a reflected Deepkit Type object.`\n );\n}\n\n/**\n * Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a type definition reference (e.g. a file path with an export), it will be resolved and bundled using ESBuild to obtain the actual schema definition before extraction.\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 *\n * @param context - The context object providing access to the file system and cache path.\n * @param input - The schema definition input to extract, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a string or a type definition reference, it will be resolved and bundled to obtain the actual schema definition before extraction.\n * @param options - Optional overrides for the ESBuild configuration used during extraction. This can include custom plugins, loaders, or other build options to control how the schema definition is resolved and bundled when the input is a type definition reference.\n * @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object. The function will attempt to extract a valid JSON Schema from the provided input, and if successful, it will return the schema. If the extraction process fails or if the input is not a valid schema definition, it will throw an error indicating the failure.\n */\nexport async function extractSchema<T = unknown>(\n context: Context,\n input: SchemaInput,\n options: Partial<BuildOptions> = {}\n): Promise<ExtractedSchema<T>> {\n if (isExtractedSchema<T>(input)) {\n return input;\n }\n\n if (isSchema<T>(input)) {\n return {\n ...input,\n source: {\n hash: extractHash(\"json-schema\", input.schema),\n variant: \"json-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 rolldownPlugin(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 \"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\", \"standard-schema\", \"zod3\", \"untyped\", or \"reflection\".`\n );\n }\n\n return {\n variant,\n source,\n schema: await extractSchemaSchema<T>(source.schema, source.variant),\n hash\n };\n}\n\n/**\n * Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a type definition reference (e.g. a file path with an export), it will be resolved and bundled using ESBuild to obtain the actual schema definition before extraction.\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 context object providing access to the file system and cache path.\n * @param input - The schema definition input to extract, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.\n * @param options - Optional overrides for the ESBuild configuration used during extraction.\n * @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object.\n * @throws Will throw an error if the input is not a valid schema definition or if the extraction process fails to produce a valid schema.\n */\nexport async function extract<T = unknown>(\n context: Context,\n input: SchemaInput,\n options: Partial<BuildOptions> = {}\n): Promise<Schema<T>> {\n if (isExtractedSchema<T>(input) || isSchema<T>(input)) {\n return input;\n }\n\n let result: Schema<T> | 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 JsonSchema<T>\n };\n }\n }\n\n result ??= await extractSchema<T>(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, 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":";;;;;;;;;;;;;;;;;AA+DA,SAAS,2BAA2B,OAAyB;CAC3D,IAAI,gBAAgB,KAAK,GACvB,OAAO,iCAAiC,KAAK;CAG/C,IAAIA,cAAY,KAAK,GAAG;EACtB,IAAI,eAAe,KAAK,GACtB,OAAO,gCAAgC,KAAK;EAG9C,MAAM,SAAS;EACf,IAAI,aAAa,UAAU,gBAAgB,OAAO,OAAO,GACvD,OAAO,iCAAiC,OAAO,OAAO;CAE1D;CAEA,OAAO;AACT;AAEA,SAAS,gCAAgC,OAAyB;CAChE,IAAI,CAAC,MAAM,QAAQ,KAAK,GACtB,OAAO;CAGT,OAAO,MAAM,KAAI,SAAQ,2BAA2B,IAAI,CAAC;AAC3D;AAEA,SAAS,iCACP,QACe;CACf,OAAO,aAAa,QAAiB,EACnC,QAAQ,WACV,CAAC;AACH;AAEA,SAAS,iCACP,QACe;CACf,MAAM,SAAS;CACf,MAAM,aAAsC,CAAC;CAE7C,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,MAAM,GAAG;EACjD,IACE,QAAQ,YACR,QAAQ,kBACR,QAAQ,UACR,QAAQ,UACR,QAAQ,WAER;EAGF,IAAI,Q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ACA,UAAiC,CAAC,GACd;CACpB,IAAI,kBAAqB,KAAK,KAAK,SAAY,KAAK,GAClD,OAAO;CAGT,IAAI;CAEJ,MAAM,UAAU,eAAe,KAAK;CACpC,MAAM,OAAO,YAAY,SAAS,KAAK;CAEvC,MAAM,gBAAgB,UAAU,kBAAkB,OAAO,GAAG,GAAG,KAAK,MAAM;CAC1E,IACE,QAAQ,OAAO,cAAc,QAC7B,QAAQ,GAAG,WAAW,aAAa,GACnC;EACA,MAAM,SAAS,MAAM,QAAQ,GAAG,KAAK,aAAa;EAClD,IAAI,QACF,SAAS;GACP;GACA;GACA,QAAQ,KAAK,MAAM,MAAM;EAC3B;CAEJ;CAEA,WAAW,MAAM,cAAiB,SAAS,OAAO,OAAO;CACzD,IAAI,CAAC,QAAQ,QACX,MAAM,IAAI,MACR,gMACF;CAGF,IAAI,QAAQ,OAAO,cAAc,MAC/B,MAAM,YAAY,SAAS,MAAM;CAGnC,OAAO;AACT"}
package/dist/helpers.cjs CHANGED
@@ -1,4 +1,3 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
1
  const require_metadata = require('./metadata.cjs');
3
2
  const require_type_checks = require('./type-checks.cjs');
4
3
  let defu = require("defu");
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.d.cts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;AAsCA;;;;;;;iBAAgB,aAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,IAAK,MAAA,SAAe,kBAAA,CAAmB,CAAA;;;;;;;;;;iBA2BrD,iBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,IAAK,KAAA,CAAM,kBAAA,CAAmB,CAAA;;;;;;;;;AA7BU;AA2BtE;;iBAiBgB,WAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,C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- {"version":3,"file":"helpers.mjs","names":[],"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 { isPropertyOptional, isSchemaNullable } from \"./metadata\";\nimport { isJsonSchemaObject, isSchema } from \"./type-checks\";\nimport {\n JsonSchema,\n JsonSchemaObject,\n JsonSchemaProperty,\n Schema\n} from \"./types\";\n\n/**\n * Extracts object properties from a JSON Schema object form.\n *\n * @remarks\n * This function returns an empty object if the schema is not an object form or if it has no properties.\n *\n * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.\n * @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.\n */\nexport function getProperties<\n T extends Record<string, any> = Record<string, any>\n>(obj: Schema<T> | JsonSchema<T>): Record<string, JsonSchemaProperty<T>> {\n const properties: Record<string, JsonSchemaProperty<T>> = {};\n const schema: JsonSchema<T> = isSchema<T>(obj) ? obj.schema : obj;\n if (!isJsonSchemaObject<T>(schema) || !isSetObject(schema.properties)) {\n return properties;\n }\n\n for (const [key, value] of Object.entries(schema.properties)) {\n properties[key] = {\n ...value,\n name: key,\n nullable: isSchemaNullable<T>(value) || isPropertyOptional(schema, key)\n };\n }\n\n return properties;\n}\n\n/**\n * Returns object properties as an array.\n *\n * @remarks\n * This is a convenience function that extracts properties using `getProperties` and returns them as an array.\n *\n * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.\n * @returns An array of JSON Schema fragments representing the properties, each including metadata.\n */\nexport function getPropertiesList<\n T extends Record<string, any> = Record<string, any>\n>(obj: Schema<T> | JsonSchema<T>): Array<JsonSchemaProperty<T>> {\n return Object.values(getProperties<T>(obj));\n}\n\n/**\n * Adds a property to a JSON Schema object form.\n *\n * @remarks\n * This function modifies the provided schema in place by adding a new property with the specified name and schema. It also updates the `required` array based on the `optional` flag of the property. If the property is marked as optional, it will be removed from the `required` array; otherwise, it will be added to it.\n *\n * @param obj - The JSON Schema object form or a Schema wrapper to which the property should be added.\n * @param name - The name of the property to add.\n * @param property - The JSON Schema fragment representing the property's schema, including metadata.\n * @throws Will throw an error if the provided schema is not an object form.\n */\nexport function addProperty<\n T extends Record<string, any> = Record<string, any>\n>(\n obj: Schema<T> | JsonSchema<T>,\n name: string,\n property: JsonSchemaProperty<T>\n): void {\n const schema = (isSchema<T>(obj) ? obj.schema : obj) as JsonSchemaObject<T>;\n if (!isJsonSchemaObject<T>(schema)) {\n throw new Error(\"Cannot add property to non-object schema\");\n }\n\n schema.properties ??= {};\n schema.required ??= [];\n\n schema.properties[name] = { ...property, name };\n if (property?.optional) {\n schema.required = schema.required.filter(key => key !== name);\n } else if (!schema.required.includes(name)) {\n schema.required.push(name);\n }\n\n if (schema.required.length === 0) {\n delete schema.required;\n }\n}\n\n/**\n * Merges multiple JSON Schema object forms into one.\n *\n * @remarks\n * This function takes multiple JSON Schema objects or Schema wrappers and merges them into a single JSON Schema object. The merging process combines properties and metadata from all provided schemas, with later schemas in the arguments list taking precedence over earlier ones in case of conflicts. The resulting schema will include all unique properties and metadata from the input schemas.\n *\n * @param schemas - An array of JSON Schema objects or Schema wrappers to merge.\n * @returns A new JSON Schema object that is the result of merging all input schemas.\n */\nexport function mergeSchemas<\n T extends Record<string, any> = Record<string, any>\n>(...schemas: (JsonSchema<T> | Schema<T>)[]): JsonSchema<T> {\n const result: JsonSchema<T> = {} as JsonSchema<T>;\n for (const schema of schemas) {\n const jsonSchema: JsonSchema<T> = isSchema<T>(schema)\n ? schema.schema\n : schema;\n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B;CAEA,OAAO;AACT"}
1
+ {"version":3,"file":"helpers.mjs","names":[],"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 { isPropertyOptional, isSchemaNullable } from \"./metadata\";\nimport { isJsonSchemaObject, isSchema } from \"./type-checks\";\nimport {\n JsonSchema,\n JsonSchemaObject,\n JsonSchemaProperty,\n Schema\n} from \"./types\";\n\n/**\n * Extracts object properties from a JSON Schema object form.\n *\n * @remarks\n * This function returns an empty object if the schema is not an object form or if it has no properties.\n *\n * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.\n * @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.\n */\nexport function getProperties<\n T extends Record<string, any> = Record<string, any>\n>(obj: Schema<T> | JsonSchema<T>): Record<string, JsonSchemaProperty<T>> {\n const properties: Record<string, JsonSchemaProperty<T>> = {};\n const schema: JsonSchema<T> = isSchema<T>(obj) ? obj.schema : obj;\n if (!isJsonSchemaObject<T>(schema) || !isSetObject(schema.properties)) {\n return properties;\n }\n\n for (const [key, value] of Object.entries(schema.properties)) {\n properties[key] = {\n ...value,\n name: key,\n nullable:\n isSchemaNullable(value as JsonSchema<any>) ||\n isPropertyOptional(schema, key)\n };\n }\n\n return properties;\n}\n\n/**\n * Returns object properties as an array.\n *\n * @remarks\n * This is a convenience function that extracts properties using `getProperties` and returns them as an array.\n *\n * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.\n * @returns An array of JSON Schema fragments representing the properties, each including metadata.\n */\nexport function getPropertiesList<\n T extends Record<string, any> = Record<string, any>\n>(obj: Schema<T> | JsonSchema<T>): Array<JsonSchemaProperty<T>> {\n return Object.values(getProperties<T>(obj));\n}\n\n/**\n * Adds a property to a JSON Schema object form.\n *\n * @remarks\n * This function modifies the provided schema in place by adding a new property with the specified name and schema. It also updates the `required` array based on the `optional` flag of the property. If the property is marked as optional, it will be removed from the `required` array; otherwise, it will be added to it.\n *\n * @param obj - The JSON Schema object form or a Schema wrapper to which the property should be added.\n * @param name - The name of the property to add.\n * @param property - The JSON Schema fragment representing the property's schema, including metadata.\n * @throws Will throw an error if the provided schema is not an object form.\n */\nexport function addProperty<\n T extends Record<string, any> = Record<string, any>\n>(\n obj: Schema<T> | JsonSchema<T>,\n name: string,\n property: JsonSchemaProperty<T>\n): void {\n const schema = (isSchema<T>(obj) ? obj.schema : obj) as JsonSchemaObject<T>;\n if (!isJsonSchemaObject<T>(schema)) {\n throw new Error(\"Cannot add property to non-object schema\");\n }\n\n schema.properties ??= {};\n schema.required ??= [];\n\n schema.properties[name] = { ...property, name };\n if (property?.optional) {\n schema.required = schema.required.filter(key => key !== name);\n } else if (!schema.required.includes(name)) {\n schema.required.push(name);\n }\n\n if (schema.required.length === 0) {\n delete schema.required;\n }\n}\n\n/**\n * Merges multiple JSON Schema object forms into one.\n *\n * @remarks\n * This function takes multiple JSON Schema objects or Schema wrappers and merges them into a single JSON Schema object. The merging process combines properties and metadata from all provided schemas, with later schemas in the arguments list taking precedence over earlier ones in case of conflicts. The resulting schema will include all unique properties and metadata from the input schemas.\n *\n * @param schemas - An array of JSON Schema objects or Schema wrappers to merge.\n * @returns A new JSON Schema object that is the result of merging all input schemas.\n */\nexport function mergeSchemas<\n T extends Record<string, any> = Record<string, any>\n>(...schemas: (JsonSchema<T> | Schema<T>)[]): JsonSchema<T> {\n const result: JsonSchema<T> = {} as JsonSchema<T>;\n for (const schema of schemas) {\n const jsonSchema: JsonSchema<T> = isSchema<T>(schema)\n ? schema.schema\n : schema;\n if (!isJsonSchemaObject<T>(jsonSchema)) {\n continue;\n }\n\n defu(result, jsonSchema);\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAsCA,SAAgB,cAEd,KAAuE;CACvE,MAAM,aAAoD,CAAC;CAC3D,MAAM,SAAwB,SAAY,GAAG,IAAI,IAAI,SAAS;CAC9D,IAAI,CAAC,mBAAsB,MAAM,KAAK,CAAC,YAAY,OAAO,UAAU,GAClE,OAAO;CAGT,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,UAAU,GACzD,WAAW,OAAO;EAChB,GAAG;EACH,MAAM;EACN,UACE,iBAAiB,KAAwB,KACzC,mBAAmB,QAAQ,GAAG;CAClC;CAGF,OAAO;AACT;;;;;;;;;;AAWA,SAAgB,kBAEd,KAA8D;CAC9D,OAAO,OAAO,OAAO,cAAiB,GAAG,CAAC;AAC5C;;;;;;;;;;;;AAaA,SAAgB,YAGd,KACA,MACA,UACM;CACN,MAAM,SAAU,SAAY,GAAG,IAAI,IAAI,SAAS;CAChD,IAAI,CAAC,mBAAsB,MAAM,GAC/B,MAAM,IAAI,MAAM,0CAA0C;CAG5D,OAAO,eAAe,CAAC;CACvB,OAAO,aAAa,CAAC;CAErB,OAAO,WAAW,QAAQ;EAAE,GAAG;EAAU;CAAK;CAC9C,IAAI,UAAU,UACZ,OAAO,WAAW,OAAO,SAAS,QAAO,QAAO,QAAQ,IAAI;MACvD,IAAI,CAAC,OAAO,SAAS,SAAS,IAAI,GACvC,OAAO,SAAS,KAAK,IAAI;CAG3B,IAAI,OAAO,SAAS,WAAW,GAC7B,OAAO,OAAO;AAElB;;;;;;;;;;AAWA,SAAgB,aAEd,GAAG,SAAuD;CAC1D,MAAM,SAAwB,CAAC;CAC/B,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,aAA4B,SAAY,MAAM,IAChD,OAAO,SACP;EACJ,IAAI,CAAC,mBAAsB,UAAU,GACnC;EAGF,OAAK,QAAQ,UAAU;CACzB;CAEA,OAAO;AACT"}
package/dist/index.cjs CHANGED
@@ -22,7 +22,6 @@ exports.extractJsonSchema = require_extract.extractJsonSchema;
22
22
  exports.extractReflection = require_extract.extractReflection;
23
23
  exports.extractResolvedVariant = require_extract.extractResolvedVariant;
24
24
  exports.extractSchema = require_extract.extractSchema;
25
- exports.extractSchemaSchema = require_extract.extractSchemaSchema;
26
25
  exports.extractSource = require_extract.extractSource;
27
26
  exports.extractVariant = require_extract.extractVariant;
28
27
  exports.generateCode = require_codegen.generateCode;
@@ -39,8 +38,10 @@ exports.isNullableSchema = require_codegen.isNullableSchema;
39
38
  exports.isPropertyOptional = require_metadata.isPropertyOptional;
40
39
  exports.isSchema = require_type_checks.isSchema;
41
40
  exports.isSchemaNullable = require_metadata.isSchemaNullable;
41
+ exports.isStandardSchema = require_type_checks.isStandardSchema;
42
42
  exports.isUntypedInput = require_type_checks.isUntypedInput;
43
43
  exports.isUntypedSchema = require_type_checks.isUntypedSchema;
44
+ exports.isValibotSchema = require_type_checks.isValibotSchema;
44
45
  exports.mergeSchemas = require_helpers.mergeSchemas;
45
46
  exports.readSchema = require_persistence.readSchema;
46
47
  exports.readSchemaSafe = require_persistence.readSchemaSafe;
package/dist/index.d.cts CHANGED
@@ -1,12 +1,38 @@
1
1
  import { BundleOptions, bundle } from "./bundle.cjs";
2
- import { BaseSchemaSource, ExtractedSchema, JsonSchema, JsonSchemaLike, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource } from "./types.cjs";
2
+ import { BaseSchemaSource, ExtractedSchema, JsonSchema, JsonSchemaLike, JsonSchemaNullable, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, ValibotSchema, ValibotSchemaSource, Zod3SchemaSource } from "./types.cjs";
3
3
  import { generateCode, isNullableSchema, stringifyType, stringifyValue } from "./codegen.cjs";
4
4
  import { JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchemaTypes } from "./constants.cjs";
5
- import { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant } from "./extract.cjs";
5
+ import { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSource, extractVariant } from "./extract.cjs";
6
6
  import { addProperty, getProperties, getPropertiesList, mergeSchemas } from "./helpers.cjs";
7
7
  import { applySchemaMetadata, getPrimarySchemaType, isPropertyOptional, isSchemaNullable, readSchemaTypes } from "./metadata.cjs";
8
8
  import { getCacheDirectory, getCacheFilePath, readSchema, readSchemaSafe, writeSchema } from "./persistence.cjs";
9
9
  import { reflectionToJsonSchema } from "./reflection.cjs";
10
10
  import { resolve, resolveModule, resolveReflection } from "./resolve.cjs";
11
- import { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.cjs";
12
- export { BaseSchemaSource, BundleOptions, ExtractedSchema, JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchema, JsonSchemaLike, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonSchemaTypes, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource, addProperty, applySchemaMetadata, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getPrimarySchemaType, getProperties, getPropertiesList, isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isNullableSchema, isPropertyOptional, isSchema, isSchemaNullable, isUntypedInput, isUntypedSchema, mergeSchemas, readSchema, readSchemaSafe, readSchemaTypes, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
11
+ import { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isStandardSchema, isUntypedInput, isUntypedSchema, isValibotSchema } from "./type-checks.cjs";
12
+
13
+ //#region src/index.d.ts
14
+ declare module "zod" {
15
+ interface GlobalMeta {
16
+ id?: string;
17
+ title?: string;
18
+ description?: string;
19
+ docs?: string;
20
+ alias?: string[];
21
+ tags?: string[];
22
+ deprecated?: boolean;
23
+ hidden?: boolean;
24
+ ignore?: boolean;
25
+ internal?: boolean;
26
+ runtime?: boolean;
27
+ examples?: unknown[];
28
+ readOnly?: boolean;
29
+ writeOnly?: boolean;
30
+ contentEncoding?: string;
31
+ contentMediaType?: string;
32
+ contentSchema?: string;
33
+ [keyword: string]: unknown;
34
+ }
35
+ }
36
+ //#endregion
37
+ export { BaseSchemaSource, BundleOptions, ExtractedSchema, JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchema, JsonSchemaLike, JsonSchemaNullable, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonSchemaTypes, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, ValibotSchema, ValibotSchemaSource, Zod3SchemaSource, addProperty, applySchemaMetadata, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getPrimarySchemaType, getProperties, getPropertiesList, isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isNullableSchema, isPropertyOptional, isSchema, isSchemaNullable, isStandardSchema, isUntypedInput, isUntypedSchema, isValibotSchema, mergeSchemas, readSchema, readSchemaSafe, readSchemaTypes, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
38
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;;;;;;;;;YAmBY,UAAA;IACR,EAAA;IACA,KAAA;IACA,WAAA;IACA,IAAA;IACA,KAAA;IACA,IAAA;IACA,UAAA;IACA,MAAA;IACA,MAAA;IACA,QAAA;IACA,OAAA;IACA,QAAA;IACA,QAAA;IACA,SAAA;IACA,eAAA;IACA,gBAAA;IACA,aAAA;IAAA,CACC,OAAA;EAAA;AAAA"}
package/dist/index.d.mts CHANGED
@@ -1,12 +1,38 @@
1
1
  import { BundleOptions, bundle } from "./bundle.mjs";
2
- import { BaseSchemaSource, ExtractedSchema, JsonSchema, JsonSchemaLike, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource } from "./types.mjs";
2
+ import { BaseSchemaSource, ExtractedSchema, JsonSchema, JsonSchemaLike, JsonSchemaNullable, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, ValibotSchema, ValibotSchemaSource, Zod3SchemaSource } from "./types.mjs";
3
3
  import { generateCode, isNullableSchema, stringifyType, stringifyValue } from "./codegen.mjs";
4
4
  import { JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchemaTypes } from "./constants.mjs";
5
- import { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant } from "./extract.mjs";
5
+ import { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSource, extractVariant } from "./extract.mjs";
6
6
  import { addProperty, getProperties, getPropertiesList, mergeSchemas } from "./helpers.mjs";
7
7
  import { applySchemaMetadata, getPrimarySchemaType, isPropertyOptional, isSchemaNullable, readSchemaTypes } from "./metadata.mjs";
8
8
  import { getCacheDirectory, getCacheFilePath, readSchema, readSchemaSafe, writeSchema } from "./persistence.mjs";
9
9
  import { reflectionToJsonSchema } from "./reflection.mjs";
10
10
  import { resolve, resolveModule, resolveReflection } from "./resolve.mjs";
11
- import { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
12
- export { BaseSchemaSource, BundleOptions, ExtractedSchema, JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchema, JsonSchemaLike, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonSchemaTypes, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource, addProperty, applySchemaMetadata, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getPrimarySchemaType, getProperties, getPropertiesList, isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isNullableSchema, isPropertyOptional, isSchema, isSchemaNullable, isUntypedInput, isUntypedSchema, mergeSchemas, readSchema, readSchemaSafe, readSchemaTypes, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
11
+ import { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isStandardSchema, isUntypedInput, isUntypedSchema, isValibotSchema } from "./type-checks.mjs";
12
+
13
+ //#region src/index.d.ts
14
+ declare module "zod" {
15
+ interface GlobalMeta {
16
+ id?: string;
17
+ title?: string;
18
+ description?: string;
19
+ docs?: string;
20
+ alias?: string[];
21
+ tags?: string[];
22
+ deprecated?: boolean;
23
+ hidden?: boolean;
24
+ ignore?: boolean;
25
+ internal?: boolean;
26
+ runtime?: boolean;
27
+ examples?: unknown[];
28
+ readOnly?: boolean;
29
+ writeOnly?: boolean;
30
+ contentEncoding?: string;
31
+ contentMediaType?: string;
32
+ contentSchema?: string;
33
+ [keyword: string]: unknown;
34
+ }
35
+ }
36
+ //#endregion
37
+ export { BaseSchemaSource, BundleOptions, ExtractedSchema, JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchema, JsonSchemaLike, JsonSchemaNullable, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonSchemaTypes, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, ValibotSchema, ValibotSchemaSource, Zod3SchemaSource, addProperty, applySchemaMetadata, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getPrimarySchemaType, getProperties, getPropertiesList, isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isNullableSchema, isPropertyOptional, isSchema, isSchemaNullable, isStandardSchema, isUntypedInput, isUntypedSchema, isValibotSchema, mergeSchemas, readSchema, readSchemaSafe, readSchemaTypes, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
38
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;;;;;;;;;YAmBY,UAAA;IACR,EAAA;IACA,KAAA;IACA,WAAA;IACA,IAAA;IACA,KAAA;IACA,IAAA;IACA,UAAA;IACA,MAAA;IACA,MAAA;IACA,QAAA;IACA,OAAA;IACA,QAAA;IACA,QAAA;IACA,SAAA;IACA,eAAA;IACA,gBAAA;IACA,aAAA;IAAA,CACC,OAAA;EAAA;AAAA"}
package/dist/index.mjs CHANGED
@@ -1,12 +1,12 @@
1
1
  import { bundle } from "./bundle.mjs";
2
2
  import { JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchemaTypes } from "./constants.mjs";
3
3
  import { applySchemaMetadata, getPrimarySchemaType, isPropertyOptional, isSchemaNullable, readSchemaTypes } from "./metadata.mjs";
4
- import { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
4
+ import { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isStandardSchema, isUntypedInput, isUntypedSchema, isValibotSchema } from "./type-checks.mjs";
5
5
  import { addProperty, getProperties, getPropertiesList, mergeSchemas } from "./helpers.mjs";
6
6
  import { generateCode, isNullableSchema, stringifyType, stringifyValue } from "./codegen.mjs";
7
7
  import { getCacheDirectory, getCacheFilePath, readSchema, readSchemaSafe, writeSchema } from "./persistence.mjs";
8
8
  import { reflectionToJsonSchema } from "./reflection.mjs";
9
9
  import { resolve, resolveModule, resolveReflection } from "./resolve.mjs";
10
- import { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant } from "./extract.mjs";
10
+ import { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSource, extractVariant } from "./extract.mjs";
11
11
 
12
- export { JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchemaTypes, addProperty, applySchemaMetadata, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getPrimarySchemaType, getProperties, getPropertiesList, isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isNullableSchema, isPropertyOptional, isSchema, isSchemaNullable, isUntypedInput, isUntypedSchema, mergeSchemas, readSchema, readSchemaSafe, readSchemaTypes, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
12
+ export { JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchemaTypes, addProperty, applySchemaMetadata, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getPrimarySchemaType, getProperties, getPropertiesList, isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isNullableSchema, isPropertyOptional, isSchema, isSchemaNullable, isStandardSchema, isUntypedInput, isUntypedSchema, isValibotSchema, mergeSchemas, readSchema, readSchemaSafe, readSchemaTypes, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
package/dist/metadata.cjs CHANGED
@@ -1,4 +1,3 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
1
  const require_constants = require('./constants.cjs');
3
2
  let _stryke_type_checks = require("@stryke/type-checks");
4
3
  let _stryke_type_checks_is_set_object = require("@stryke/type-checks/is-set-object");
@@ -45,7 +44,7 @@ function isSchemaNullable(schema) {
45
44
  * @returns `true` if the property is optional, otherwise `false`.
46
45
  */
47
46
  function isPropertyOptional(parent, propertyName) {
48
- return !(parent.required ?? []).includes(propertyName);
47
+ return !(parent.required ?? []).includes(propertyName) && !isSchemaNullable(parent.properties[propertyName]);
49
48
  }
50
49
  /**
51
50
  * Normalizes the JSON Schema `type` keyword to a string array.
@@ -1 +1 @@
1
- {"version":3,"file":"metadata.d.cts","names":[],"sources":["../src/metadata.ts"],"mappings":";;;;;AAmCA;;;;;iBAAgB,mBAAA,gCAEI,cAAA,GAAiB,cAAA,CAAA,CACnC,MAAA,EAAQ,UAAA,CAAW,CAAA,GAAI,QAAA,EAAU,SAAA,eAAwB,UAAA,CAAW,CAAA;;;;;;;;;;iBAyBtD,gBAAA,aAAA,CAA8B,MAAA,GAAS,UAAU,CAAC,CAAA;;;;;;;;;AAzBK;AAyBvE;iBAwBgB,kBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,MAAA,EAAQ,gBAAA,CAAiB,CAAA,GAAI,YAAA;;;;;;;;AA1BqC;AAwBpE;iBAiBgB,eAAA,aAAA,CACd,MAAA,GAAS,UAAA,CAAW,CAAA,IACnB,uBAAA;;;;;;;iBAgBa,oBAAA,aAAA,CACd,MAAA,GAAS,UAAA,CAAW,CAAA,IACnB,uBAAA"}
1
+ {"version":3,"file":"metadata.d.cts","names":[],"sources":["../src/metadata.ts"],"mappings":";;;;;AAoCA;;;;;iBAAgB,mBAAA,gCAEI,cAAA,GAAiB,cAAA,CAAA,CACnC,MAAA,EAAQ,UAAA,CAAW,CAAA,GAAI,QAAA,EAAU,SAAA,eAAwB,UAAA,CAAW,CAAA;;;;;;;;;;iBAyBtD,gBAAA,aAAA,CAA8B,MAAA,GAAS,UAAU,CAAC,CAAA;;;;;;;;;AAzBK;AAyBvE;iBAwBgB,kBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,MAAA,EAAQ,gBAAA,CAAiB,CAAA,GAAI,YAAA;;;;;;;;AA1BqC;AAwBpE;iBAoBgB,eAAA,aAAA,CACd,MAAA,GAAS,UAAA,CAAW,CAAA,IACnB,uBAAA;;;;;;;iBAgBa,oBAAA,aAAA,CACd,MAAA,GAAS,UAAA,CAAW,CAAA,IACnB,uBAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"metadata.d.mts","names":[],"sources":["../src/metadata.ts"],"mappings":";;;;;AAmCA;;;;;iBAAgB,mBAAA,gCAEI,cAAA,GAAiB,cAAA,CAAA,CACnC,MAAA,EAAQ,UAAA,CAAW,CAAA,GAAI,QAAA,EAAU,SAAA,eAAwB,UAAA,CAAW,CAAA;;;;;;;;;;iBAyBtD,gBAAA,aAAA,CAA8B,MAAA,GAAS,UAAU,CAAC,CAAA;;;;;;;;;AAzBK;AAyBvE;iBAwBgB,kBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,MAAA,EAAQ,gBAAA,CAAiB,CAAA,GAAI,YAAA;;;;;;;;AA1BqC;AAwBpE;iBAiBgB,eAAA,aAAA,CACd,MAAA,GAAS,UAAA,CAAW,CAAA,IACnB,uBAAA;;;;;;;iBAgBa,oBAAA,aAAA,CACd,MAAA,GAAS,UAAA,CAAW,CAAA,IACnB,uBAAA"}
1
+ {"version":3,"file":"metadata.d.mts","names":[],"sources":["../src/metadata.ts"],"mappings":";;;;;AAoCA;;;;;iBAAgB,mBAAA,gCAEI,cAAA,GAAiB,cAAA,CAAA,CACnC,MAAA,EAAQ,UAAA,CAAW,CAAA,GAAI,QAAA,EAAU,SAAA,eAAwB,UAAA,CAAW,CAAA;;;;;;;;;;iBAyBtD,gBAAA,aAAA,CAA8B,MAAA,GAAS,UAAU,CAAC,CAAA;;;;;;;;;AAzBK;AAyBvE;iBAwBgB,kBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,MAAA,EAAQ,gBAAA,CAAiB,CAAA,GAAI,YAAA;;;;;;;;AA1BqC;AAwBpE;iBAoBgB,eAAA,aAAA,CACd,MAAA,GAAS,UAAA,CAAW,CAAA,IACnB,uBAAA;;;;;;;iBAgBa,oBAAA,aAAA,CACd,MAAA,GAAS,UAAA,CAAW,CAAA,IACnB,uBAAA"}
package/dist/metadata.mjs CHANGED
@@ -44,7 +44,7 @@ function isSchemaNullable(schema) {
44
44
  * @returns `true` if the property is optional, otherwise `false`.
45
45
  */
46
46
  function isPropertyOptional(parent, propertyName) {
47
- return !(parent.required ?? []).includes(propertyName);
47
+ return !(parent.required ?? []).includes(propertyName) && !isSchemaNullable(parent.properties[propertyName]);
48
48
  }
49
49
  /**
50
50
  * Normalizes the JSON Schema `type` keyword to a string array.
@@ -1 +1 @@
1
- {"version":3,"file":"metadata.mjs","names":["isSetObject"],"sources":["../src/metadata.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 { isSetString } from \"@stryke/type-checks\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport { JSON_SCHEMA_METADATA_KEYS } from \"./constants\";\nimport {\n JsonSchema,\n JsonSchemaObject,\n JsonSchemaPrimitiveType,\n SchemaMetadata\n} from \"./types\";\n\n/**\n * Applies Powerlines schema metadata onto a JSON Schema fragment.\n *\n * @param schema - The JSON Schema fragment to apply metadata to.\n * @param metadata - The Powerlines schema metadata to apply.\n * @returns A new JSON Schema fragment with the metadata applied.\n */\nexport function applySchemaMetadata<\n T = unknown,\n TMetadata extends SchemaMetadata = SchemaMetadata\n>(schema: JsonSchema<T>, metadata: TMetadata | undefined): JsonSchema<T> {\n if (!metadata) {\n return schema;\n }\n\n const result: JsonSchema<T> = { ...schema };\n for (const key of JSON_SCHEMA_METADATA_KEYS) {\n const value = metadata[key];\n if (value !== undefined && value !== null) {\n result[key as keyof JsonSchema<T>] = value;\n }\n }\n\n return result;\n}\n\n/**\n * Returns whether a JSON Schema fragment accepts `null`.\n *\n * @remarks\n * This is true if the schema has `nullable: true` or if its `type` includes `\"null\"`.\n *\n * @param schema - The JSON Schema fragment to check.\n * @returns `true` if the schema accepts `null`, otherwise `false`.\n */\nexport function isSchemaNullable<T = unknown>(schema?: JsonSchema<T>): boolean {\n if (!isSetObject(schema)) {\n return false;\n }\n\n if (schema.nullable === true) {\n return true;\n }\n\n const types = readSchemaTypes(schema);\n\n return types.includes(\"null\");\n}\n\n/**\n * Returns whether an object property is optional (not listed in `required`).\n *\n * @remarks\n * In JSON Schema, object properties are optional by default unless they are listed in the `required` array of the parent schema. This function checks whether a given property name is not included in the `required` array of its parent schema, indicating that it is optional.\n *\n * @param parent - The parent JSON Schema object containing the property.\n * @param propertyName - The name of the property to check for optionality.\n * @returns `true` if the property is optional, otherwise `false`.\n */\nexport function isPropertyOptional<\n T extends Record<string, any> = Record<string, any>\n>(parent: JsonSchemaObject<T>, propertyName: string): boolean {\n const required = parent.required ?? [];\n\n return !required.includes(propertyName);\n}\n\n/**\n * Normalizes the JSON Schema `type` keyword to a string array.\n *\n * @remarks\n * This function ensures that the `type` keyword of a JSON Schema fragment is always represented as an array of strings, even if it was originally defined as a single string. This normalization simplifies type checking and processing of JSON Schemas by providing a consistent format for the `type` information.\n *\n * @param schema - The JSON Schema fragment to read types from.\n * @returns An array of JSON Schema primitive type names defined in the `type` keyword, or an empty array if no valid types are found.\n */\nexport function readSchemaTypes<T = unknown>(\n schema?: JsonSchema<T>\n): JsonSchemaPrimitiveType[] {\n if (Array.isArray(schema?.type)) {\n return schema.type.filter(isSetString) as JsonSchemaPrimitiveType[];\n }\n if (isSetString(schema?.type)) {\n return [schema.type as JsonSchemaPrimitiveType];\n }\n return [];\n}\n\n/**\n * Returns the primary non-null JSON Schema type name for a fragment.\n *\n * @param schema - The JSON Schema fragment to check.\n * @returns The primary non-null JSON Schema type name, or `undefined` if none is found.\n */\nexport function getPrimarySchemaType<T = unknown>(\n schema?: JsonSchema<T>\n): JsonSchemaPrimitiveType | undefined {\n if (!isSetObject(schema)) {\n return undefined;\n }\n\n return readSchemaTypes(schema).find(type => type !== \"null\");\n}\n"],"mappings":";;;;;;;;;;;;AAmCA,SAAgB,oBAGd,QAAuB,UAAgD;CACvE,IAAI,CAAC,UACH,OAAO;CAGT,MAAM,SAAwB,EAAE,GAAG,OAAO;CAC1C,KAAK,MAAM,OAAO,2BAA2B;EAC3C,MAAM,QAAQ,SAAS;EACvB,IAAI,UAAU,UAAa,UAAU,MACnC,OAAO,OAA8B;CAEzC;CAEA,OAAO;AACT;;;;;;;;;;AAWA,SAAgB,iBAA8B,QAAiC;CAC7E,IAAI,CAACA,cAAY,MAAM,GACrB,OAAO;CAGT,IAAI,OAAO,aAAa,MACtB,OAAO;CAKT,OAFc,gBAAgB,MAEnB,EAAE,SAAS,MAAM;AAC9B;;;;;;;;;;;AAYA,SAAgB,mBAEd,QAA6B,cAA+B;CAG5D,OAAO,EAFU,OAAO,YAAY,CAAC,GAEpB,SAAS,YAAY;AACxC;;;;;;;;;;AAWA,SAAgB,gBACd,QAC2B;CAC3B,IAAI,MAAM,QAAQ,QAAQ,IAAI,GAC5B,OAAO,OAAO,KAAK,OAAO,WAAW;CAEvC,IAAI,YAAY,QAAQ,IAAI,GAC1B,OAAO,CAAC,OAAO,IAA+B;CAEhD,OAAO,CAAC;AACV;;;;;;;AAQA,SAAgB,qBACd,QACqC;CACrC,IAAI,CAACA,cAAY,MAAM,GACrB;CAGF,OAAO,gBAAgB,MAAM,EAAE,MAAK,SAAQ,SAAS,MAAM;AAC7D"}
1
+ {"version":3,"file":"metadata.mjs","names":["isSetObject"],"sources":["../src/metadata.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 { isSetString } from \"@stryke/type-checks\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport { JSON_SCHEMA_METADATA_KEYS } from \"./constants\";\nimport {\n JsonSchema,\n JsonSchemaNullable,\n JsonSchemaObject,\n JsonSchemaPrimitiveType,\n SchemaMetadata\n} from \"./types\";\n\n/**\n * Applies Powerlines schema metadata onto a JSON Schema fragment.\n *\n * @param schema - The JSON Schema fragment to apply metadata to.\n * @param metadata - The Powerlines schema metadata to apply.\n * @returns A new JSON Schema fragment with the metadata applied.\n */\nexport function applySchemaMetadata<\n T = unknown,\n TMetadata extends SchemaMetadata = SchemaMetadata\n>(schema: JsonSchema<T>, metadata: TMetadata | undefined): JsonSchema<T> {\n if (!metadata) {\n return schema;\n }\n\n const result: JsonSchema<T> = { ...schema };\n for (const key of JSON_SCHEMA_METADATA_KEYS) {\n const value = metadata[key];\n if (value !== undefined && value !== null) {\n result[key as keyof JsonSchema<T>] = value;\n }\n }\n\n return result;\n}\n\n/**\n * Returns whether a JSON Schema fragment accepts `null`.\n *\n * @remarks\n * This is true if the schema has `nullable: true` or if its `type` includes `\"null\"`.\n *\n * @param schema - The JSON Schema fragment to check.\n * @returns `true` if the schema accepts `null`, otherwise `false`.\n */\nexport function isSchemaNullable<T = unknown>(schema?: JsonSchema<T>): boolean {\n if (!isSetObject(schema)) {\n return false;\n }\n\n if ((schema as JsonSchemaNullable<T>).nullable === true) {\n return true;\n }\n\n const types = readSchemaTypes(schema);\n\n return types.includes(\"null\");\n}\n\n/**\n * Returns whether an object property is optional (not listed in `required`).\n *\n * @remarks\n * In JSON Schema, object properties are optional by default unless they are listed in the `required` array of the parent schema. This function checks whether a given property name is not included in the `required` array of its parent schema, indicating that it is optional.\n *\n * @param parent - The parent JSON Schema object containing the property.\n * @param propertyName - The name of the property to check for optionality.\n * @returns `true` if the property is optional, otherwise `false`.\n */\nexport function isPropertyOptional<\n T extends Record<string, any> = Record<string, any>\n>(parent: JsonSchemaObject<T>, propertyName: string): boolean {\n const required = parent.required ?? [];\n\n return (\n !required.includes(propertyName) &&\n !isSchemaNullable<T>(parent.properties[propertyName] as JsonSchema<T>)\n );\n}\n\n/**\n * Normalizes the JSON Schema `type` keyword to a string array.\n *\n * @remarks\n * This function ensures that the `type` keyword of a JSON Schema fragment is always represented as an array of strings, even if it was originally defined as a single string. This normalization simplifies type checking and processing of JSON Schemas by providing a consistent format for the `type` information.\n *\n * @param schema - The JSON Schema fragment to read types from.\n * @returns An array of JSON Schema primitive type names defined in the `type` keyword, or an empty array if no valid types are found.\n */\nexport function readSchemaTypes<T = unknown>(\n schema?: JsonSchema<T>\n): JsonSchemaPrimitiveType[] {\n if (Array.isArray(schema?.type)) {\n return schema.type.filter(isSetString) as JsonSchemaPrimitiveType[];\n }\n if (isSetString(schema?.type)) {\n return [schema.type as JsonSchemaPrimitiveType];\n }\n return [];\n}\n\n/**\n * Returns the primary non-null JSON Schema type name for a fragment.\n *\n * @param schema - The JSON Schema fragment to check.\n * @returns The primary non-null JSON Schema type name, or `undefined` if none is found.\n */\nexport function getPrimarySchemaType<T = unknown>(\n schema?: JsonSchema<T>\n): JsonSchemaPrimitiveType | undefined {\n if (!isSetObject(schema)) {\n return undefined;\n }\n\n return readSchemaTypes(schema).find(type => type !== \"null\");\n}\n"],"mappings":";;;;;;;;;;;;AAoCA,SAAgB,oBAGd,QAAuB,UAAgD;CACvE,IAAI,CAAC,UACH,OAAO;CAGT,MAAM,SAAwB,EAAE,GAAG,OAAO;CAC1C,KAAK,MAAM,OAAO,2BAA2B;EAC3C,MAAM,QAAQ,SAAS;EACvB,IAAI,UAAU,UAAa,UAAU,MACnC,OAAO,OAA8B;CAEzC;CAEA,OAAO;AACT;;;;;;;;;;AAWA,SAAgB,iBAA8B,QAAiC;CAC7E,IAAI,CAACA,cAAY,MAAM,GACrB,OAAO;CAGT,IAAK,OAAiC,aAAa,MACjD,OAAO;CAKT,OAFc,gBAAgB,MAEnB,EAAE,SAAS,MAAM;AAC9B;;;;;;;;;;;AAYA,SAAgB,mBAEd,QAA6B,cAA+B;CAG5D,OACE,EAHe,OAAO,YAAY,CAAC,GAGzB,SAAS,YAAY,KAC/B,CAAC,iBAAoB,OAAO,WAAW,aAA8B;AAEzE;;;;;;;;;;AAWA,SAAgB,gBACd,QAC2B;CAC3B,IAAI,MAAM,QAAQ,QAAQ,IAAI,GAC5B,OAAO,OAAO,KAAK,OAAO,WAAW;CAEvC,IAAI,YAAY,QAAQ,IAAI,GAC1B,OAAO,CAAC,OAAO,IAA+B;CAEhD,OAAO,CAAC;AACV;;;;;;;AAQA,SAAgB,qBACd,QACqC;CACrC,IAAI,CAACA,cAAY,MAAM,GACrB;CAGF,OAAO,gBAAgB,MAAM,EAAE,MAAK,SAAQ,SAAS,MAAM;AAC7D"}
@@ -1,4 +1,3 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
1
  const require_type_checks = require('./type-checks.cjs');
3
2
  const require_extract = require('./extract.cjs');
4
3
  let _stryke_path_join = require("@stryke/path/join");
@@ -1,4 +1,3 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
1
  const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
3
2
  const require_type_checks = require('./type-checks.cjs');
4
3
  let defu = require("defu");
package/dist/resolve.cjs CHANGED
@@ -1,4 +1,3 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
1
  const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
3
2
  const require_bundle = require('./bundle.cjs');
4
3
  let defu = require("defu");
@@ -1,4 +1,3 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
1
  const require_constants = require('./constants.cjs');
3
2
  let _stryke_type_checks = require("@stryke/type-checks");
4
3
  let _stryke_json = require("@stryke/json");
@@ -60,6 +59,30 @@ const TYPE_KEYWORD_VALIDATORS = {
60
59
  null: {}
61
60
  };
62
61
  /**
62
+ * Type guard for Standard Schema V1 objects.
63
+ *
64
+ * @param input - The value to check.
65
+ * @returns True if the input is a Standard Schema V1 object, false otherwise.
66
+ */
67
+ function isStandardSchema(input) {
68
+ if (!(0, _stryke_type_checks.isSetObject)(input)) return false;
69
+ const schema = input;
70
+ if (!(0, _stryke_type_checks.isSetObject)(schema["~standard"])) return false;
71
+ const standard = schema["~standard"];
72
+ return standard.version === 1 && (0, _stryke_type_checks.isFunction)(standard.validate);
73
+ }
74
+ /**
75
+ * Type guard for Valibot BaseSchema objects.
76
+ *
77
+ * @param input - The value to check.
78
+ * @returns True if the input is a Valibot BaseSchema, false otherwise.
79
+ */
80
+ function isValibotSchema(input) {
81
+ if (!(0, _stryke_type_checks.isSetObject)(input) || !isStandardSchema(input)) return false;
82
+ const schema = input;
83
+ return schema.kind === "schema" && (0, _stryke_type_checks.isSetString)(schema.type) && isSetBoolean(schema.async) && (0, _stryke_type_checks.isFunction)(schema.reference) && (0, _stryke_type_checks.isSetString)(schema.expects) && (0, _stryke_type_checks.isFunction)(schema["~run"]);
84
+ }
85
+ /**
63
86
  * Type guard for JSON Schema types.
64
87
  *
65
88
  * @remarks
@@ -173,5 +196,7 @@ exports.isJsonSchema = isJsonSchema;
173
196
  exports.isJsonSchemaObject = isJsonSchemaObject;
174
197
  exports.isNullOnlyJsonSchema = isNullOnlyJsonSchema;
175
198
  exports.isSchema = isSchema;
199
+ exports.isStandardSchema = isStandardSchema;
176
200
  exports.isUntypedInput = isUntypedInput;
177
- exports.isUntypedSchema = isUntypedSchema;
201
+ exports.isUntypedSchema = isUntypedSchema;
202
+ exports.isValibotSchema = isValibotSchema;
@@ -1,7 +1,23 @@
1
1
  import { ExtractedSchema, JsonSchema, JsonSchemaObject, Schema as Schema$1 } from "./types.cjs";
2
+ import { StandardSchemaV1 } from "@standard-schema/spec";
2
3
  import { InputObject, Schema } from "untyped";
4
+ import { BaseSchema } from "valibot";
3
5
 
4
6
  //#region src/type-checks.d.ts
7
+ /**
8
+ * Type guard for Standard Schema V1 objects.
9
+ *
10
+ * @param input - The value to check.
11
+ * @returns True if the input is a Standard Schema V1 object, false otherwise.
12
+ */
13
+ declare function isStandardSchema(input: unknown): input is StandardSchemaV1;
14
+ /**
15
+ * Type guard for Valibot BaseSchema objects.
16
+ *
17
+ * @param input - The value to check.
18
+ * @returns True if the input is a Valibot BaseSchema, false otherwise.
19
+ */
20
+ declare function isValibotSchema(input: unknown): input is BaseSchema<any, any, any>;
5
21
  /**
6
22
  * Type guard for JSON Schema types.
7
23
  *
@@ -61,5 +77,5 @@ declare function isUntypedInput(input: unknown): input is InputObject;
61
77
  declare function isSchema<T = unknown>(input: unknown): input is Schema$1<T>;
62
78
  declare function isExtractedSchema<T = unknown>(input: unknown): input is ExtractedSchema<T>;
63
79
  //#endregion
64
- export { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isUntypedInput, isUntypedSchema };
80
+ export { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isStandardSchema, isUntypedInput, isUntypedSchema, isValibotSchema };
65
81
  //# sourceMappingURL=type-checks.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"type-checks.d.cts","names":[],"sources":["../src/type-checks.ts"],"mappings":";;;;;;AAyHA;;;;;;;iBAAgB,YAAA,aAAA,CACd,KAAA,YACC,KAAA,IAAS,UAAU,CAAC,CAAA;;;AAAC;AA4FxB;;;;;;iBAAgB,kBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,KAAA,YAAiB,KAAA,IAAS,gBAAA,CAAiB,CAAA;;;;;;;;;;iBAgB7B,oBAAA,CACd,KAAA,YACC,KAAA,IAAS,UAAU;AAlBwB;AAgB9C;;;;;;;;AAhB8C,iBA+B9B,eAAA,CAAgB,KAAA,YAAiB,KAAA,IAAS,MAAa;AAAvE;;;;;;;;AAAuE;AAAvE,iBA2DgB,cAAA,CAAe,KAAA,YAAiB,KAAA,IAAS,WAAkB;;;;;;;iBAsB3D,QAAA,aAAA,CAAsB,KAAA,YAAiB,KAAA,IAAS,QAAM,CAAC,CAAA;AAAA,iBAYvD,iBAAA,aAAA,CACd,KAAA,YACC,KAAA,IAAS,eAAe,CAAC,CAAA"}
1
+ {"version":3,"file":"type-checks.d.cts","names":[],"sources":["../src/type-checks.ts"],"mappings":";;;;;;;;AAwHA;;;;iBAAgB,gBAAA,CAAiB,KAAA,YAAiB,KAAA,IAAS,gBAAgB;;;;AAAA;AAqB3E;;iBAAgB,eAAA,CACd,KAAA,YACC,KAAA,IAAS,UAAU;;;;;;AAAA;AA0BtB;;;iBAAgB,YAAA,aAAA,CACd,KAAA,YACC,KAAA,IAAS,UAAU,CAAC,CAAA;;;;;;;AAAC;AA4FxB;;iBAAgB,kBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,KAAA,YAAiB,KAAA,IAAS,gBAAA,CAAiB,CAAA;;;;;;;;;;iBAgB7B,oBAAA,CACd,KAAA,YACC,KAAA,IAAS,UAAU;;;;;AAlBwB;AAgB9C;;;;iBAegB,eAAA,CAAgB,KAAA,YAAiB,KAAA,IAAS,MAAa;;;;AAbjD;AAatB;;;;;iBA2DgB,cAAA,CAAe,KAAA,YAAiB,KAAA,IAAS,WAAkB;;;AA3DJ;AA2DvE;;;iBAsBgB,QAAA,aAAA,CAAsB,KAAA,YAAiB,KAAA,IAAS,QAAM,CAAC,CAAA;AAAA,iBAYvD,iBAAA,aAAA,CACd,KAAA,YACC,KAAA,IAAS,eAAe,CAAC,CAAA"}
@@ -1,7 +1,23 @@
1
1
  import { ExtractedSchema, JsonSchema, JsonSchemaObject, Schema as Schema$1 } from "./types.mjs";
2
+ import { StandardSchemaV1 } from "@standard-schema/spec";
2
3
  import { InputObject, Schema } from "untyped";
4
+ import { BaseSchema } from "valibot";
3
5
 
4
6
  //#region src/type-checks.d.ts
7
+ /**
8
+ * Type guard for Standard Schema V1 objects.
9
+ *
10
+ * @param input - The value to check.
11
+ * @returns True if the input is a Standard Schema V1 object, false otherwise.
12
+ */
13
+ declare function isStandardSchema(input: unknown): input is StandardSchemaV1;
14
+ /**
15
+ * Type guard for Valibot BaseSchema objects.
16
+ *
17
+ * @param input - The value to check.
18
+ * @returns True if the input is a Valibot BaseSchema, false otherwise.
19
+ */
20
+ declare function isValibotSchema(input: unknown): input is BaseSchema<any, any, any>;
5
21
  /**
6
22
  * Type guard for JSON Schema types.
7
23
  *
@@ -61,5 +77,5 @@ declare function isUntypedInput(input: unknown): input is InputObject;
61
77
  declare function isSchema<T = unknown>(input: unknown): input is Schema$1<T>;
62
78
  declare function isExtractedSchema<T = unknown>(input: unknown): input is ExtractedSchema<T>;
63
79
  //#endregion
64
- export { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isUntypedInput, isUntypedSchema };
80
+ export { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isStandardSchema, isUntypedInput, isUntypedSchema, isValibotSchema };
65
81
  //# sourceMappingURL=type-checks.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"type-checks.d.mts","names":[],"sources":["../src/type-checks.ts"],"mappings":";;;;;;AAyHA;;;;;;;iBAAgB,YAAA,aAAA,CACd,KAAA,YACC,KAAA,IAAS,UAAU,CAAC,CAAA;;;AAAC;AA4FxB;;;;;;iBAAgB,kBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,KAAA,YAAiB,KAAA,IAAS,gBAAA,CAAiB,CAAA;;;;;;;;;;iBAgB7B,oBAAA,CACd,KAAA,YACC,KAAA,IAAS,UAAU;AAlBwB;AAgB9C;;;;;;;;AAhB8C,iBA+B9B,eAAA,CAAgB,KAAA,YAAiB,KAAA,IAAS,MAAa;AAAvE;;;;;;;;AAAuE;AAAvE,iBA2DgB,cAAA,CAAe,KAAA,YAAiB,KAAA,IAAS,WAAkB;;;;;;;iBAsB3D,QAAA,aAAA,CAAsB,KAAA,YAAiB,KAAA,IAAS,QAAM,CAAC,CAAA;AAAA,iBAYvD,iBAAA,aAAA,CACd,KAAA,YACC,KAAA,IAAS,eAAe,CAAC,CAAA"}
1
+ {"version":3,"file":"type-checks.d.mts","names":[],"sources":["../src/type-checks.ts"],"mappings":";;;;;;;;AAwHA;;;;iBAAgB,gBAAA,CAAiB,KAAA,YAAiB,KAAA,IAAS,gBAAgB;;;;AAAA;AAqB3E;;iBAAgB,eAAA,CACd,KAAA,YACC,KAAA,IAAS,UAAU;;;;;;AAAA;AA0BtB;;;iBAAgB,YAAA,aAAA,CACd,KAAA,YACC,KAAA,IAAS,UAAU,CAAC,CAAA;;;;;;;AAAC;AA4FxB;;iBAAgB,kBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,KAAA,YAAiB,KAAA,IAAS,gBAAA,CAAiB,CAAA;;;;;;;;;;iBAgB7B,oBAAA,CACd,KAAA,YACC,KAAA,IAAS,UAAU;;;;;AAlBwB;AAgB9C;;;;iBAegB,eAAA,CAAgB,KAAA,YAAiB,KAAA,IAAS,MAAa;;;;AAbjD;AAatB;;;;;iBA2DgB,cAAA,CAAe,KAAA,YAAiB,KAAA,IAAS,WAAkB;;;AA3DJ;AA2DvE;;;iBAsBgB,QAAA,aAAA,CAAsB,KAAA,YAAiB,KAAA,IAAS,QAAM,CAAC,CAAA;AAAA,iBAYvD,iBAAA,aAAA,CACd,KAAA,YACC,KAAA,IAAS,eAAe,CAAC,CAAA"}
@@ -59,6 +59,30 @@ const TYPE_KEYWORD_VALIDATORS = {
59
59
  null: {}
60
60
  };
61
61
  /**
62
+ * Type guard for Standard Schema V1 objects.
63
+ *
64
+ * @param input - The value to check.
65
+ * @returns True if the input is a Standard Schema V1 object, false otherwise.
66
+ */
67
+ function isStandardSchema(input) {
68
+ if (!isSetObject(input)) return false;
69
+ const schema = input;
70
+ if (!isSetObject(schema["~standard"])) return false;
71
+ const standard = schema["~standard"];
72
+ return standard.version === 1 && isFunction(standard.validate);
73
+ }
74
+ /**
75
+ * Type guard for Valibot BaseSchema objects.
76
+ *
77
+ * @param input - The value to check.
78
+ * @returns True if the input is a Valibot BaseSchema, false otherwise.
79
+ */
80
+ function isValibotSchema(input) {
81
+ if (!isSetObject(input) || !isStandardSchema(input)) return false;
82
+ const schema = input;
83
+ return schema.kind === "schema" && isSetString(schema.type) && isSetBoolean(schema.async) && isFunction(schema.reference) && isSetString(schema.expects) && isFunction(schema["~run"]);
84
+ }
85
+ /**
62
86
  * Type guard for JSON Schema types.
63
87
  *
64
88
  * @remarks
@@ -167,5 +191,5 @@ function isExtractedSchema(input) {
167
191
  }
168
192
 
169
193
  //#endregion
170
- export { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isUntypedInput, isUntypedSchema };
194
+ export { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isStandardSchema, isUntypedInput, isUntypedSchema, isValibotSchema };
171
195
  //# sourceMappingURL=type-checks.mjs.map