@powerlines/schema 0.11.72 → 0.11.74
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2114 -97
- package/dist/index.d.cts +1841 -11
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1841 -13
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2002 -15
- package/dist/index.mjs.map +1 -0
- package/package.json +14 -14
- package/dist/_virtual/_rolldown/runtime.cjs +0 -37
- package/dist/bundle.cjs +0 -55
- package/dist/bundle.d.cts +0 -21
- package/dist/bundle.d.cts.map +0 -1
- package/dist/bundle.d.mts +0 -23
- package/dist/bundle.d.mts.map +0 -1
- package/dist/bundle.mjs +0 -54
- package/dist/bundle.mjs.map +0 -1
- package/dist/codegen.cjs +0 -81
- package/dist/codegen.d.cts +0 -26
- package/dist/codegen.d.cts.map +0 -1
- package/dist/codegen.d.mts +0 -28
- package/dist/codegen.d.mts.map +0 -1
- package/dist/codegen.mjs +0 -77
- package/dist/codegen.mjs.map +0 -1
- package/dist/constants.cjs +0 -69
- package/dist/constants.d.cts +0 -17
- package/dist/constants.d.cts.map +0 -1
- package/dist/constants.d.mts +0 -19
- package/dist/constants.d.mts.map +0 -1
- package/dist/constants.mjs +0 -65
- package/dist/constants.mjs.map +0 -1
- package/dist/extract.cjs +0 -490
- package/dist/extract.d.cts +0 -125
- package/dist/extract.d.cts.map +0 -1
- package/dist/extract.d.mts +0 -127
- package/dist/extract.d.mts.map +0 -1
- package/dist/extract.mjs +0 -480
- package/dist/extract.mjs.map +0 -1
- package/dist/helpers.cjs +0 -171
- package/dist/helpers.d.cts +0 -111
- package/dist/helpers.d.cts.map +0 -1
- package/dist/helpers.d.mts +0 -113
- package/dist/helpers.d.mts.map +0 -1
- package/dist/helpers.mjs +0 -164
- package/dist/helpers.mjs.map +0 -1
- package/dist/metadata.cjs +0 -61
- package/dist/metadata.d.cts +0 -31
- package/dist/metadata.d.cts.map +0 -1
- package/dist/metadata.d.mts +0 -33
- package/dist/metadata.d.mts.map +0 -1
- package/dist/metadata.mjs +0 -60
- package/dist/metadata.mjs.map +0 -1
- package/dist/persistence.cjs +0 -82
- package/dist/persistence.d.cts +0 -47
- package/dist/persistence.d.cts.map +0 -1
- package/dist/persistence.d.mts +0 -49
- package/dist/persistence.d.mts.map +0 -1
- package/dist/persistence.mjs +0 -79
- package/dist/persistence.mjs.map +0 -1
- package/dist/reflection.cjs +0 -434
- package/dist/reflection.d.cts +0 -11
- package/dist/reflection.d.cts.map +0 -1
- package/dist/reflection.d.mts +0 -13
- package/dist/reflection.d.mts.map +0 -1
- package/dist/reflection.mjs +0 -433
- package/dist/reflection.mjs.map +0 -1
- package/dist/resolve.cjs +0 -113
- package/dist/resolve.d.cts +0 -36
- package/dist/resolve.d.cts.map +0 -1
- package/dist/resolve.d.mts +0 -38
- package/dist/resolve.d.mts.map +0 -1
- package/dist/resolve.mjs +0 -110
- package/dist/resolve.mjs.map +0 -1
- package/dist/type-checks.cjs +0 -642
- package/dist/type-checks.d.cts +0 -316
- package/dist/type-checks.d.cts.map +0 -1
- package/dist/type-checks.d.mts +0 -318
- package/dist/type-checks.d.mts.map +0 -1
- package/dist/type-checks.mjs +0 -603
- package/dist/type-checks.mjs.map +0 -1
- package/dist/types.d.cts +0 -1152
- package/dist/types.d.cts.map +0 -1
- package/dist/types.d.mts +0 -1154
- package/dist/types.d.mts.map +0 -1
- package/dist/validate.cjs +0 -35
- package/dist/validate.mjs +0 -33
- package/dist/validate.mjs.map +0 -1
package/dist/extract.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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 { isFileReference } from \"@powerlines/core\";\nimport { esbuildPlugin } from \"@powerlines/deepkit/esbuild-plugin\";\nimport {\n deserializeType,\n isType,\n SerializedType,\n stringifyType,\n Type\n} from \"@powerlines/deepkit/vendor/type\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { extractFileReference } from \"@stryke/convert/extract-file-reference\";\nimport { murmurhash } from \"@stryke/hash\";\nimport { deepClone } from \"@stryke/helpers/deep-clone\";\nimport { isStandardJsonSchema } from \"@stryke/json\";\nimport { findFileExtensionSafe } from \"@stryke/path/find\";\nimport { joinPaths } from \"@stryke/path/join\";\nimport { list } from \"@stryke/string-format/list\";\nimport { isSetString } from \"@stryke/type-checks\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport { FileReferenceInput } from \"@stryke/types\";\nimport {\n extractJsonSchema as extractJsonSchemaZod,\n isZod3Type\n} from \"@stryke/zod\";\nimport { toJsonSchema } from \"@valibot/to-json-schema\";\nimport defu from \"defu\";\nimport * as z3 from \"zod/v3\";\nimport { BundleOptions } from \"./bundle\";\nimport { VALID_SOURCE_FILE_EXTENSIONS } from \"./constants\";\nimport { getCacheDirectory, writeSchema } from \"./persistence\";\nimport { reflectionToJsonSchema } from \"./reflection\";\nimport { resolve } from \"./resolve\";\nimport {\n isJsonSchema,\n isJsonSchemaObject,\n isSchema,\n isSchemaWithSource,\n isUntypedInput,\n isUntypedInputStrict,\n isUntypedSchema,\n isUntypedSchemaStrict,\n isValibotSchema\n} from \"./type-checks\";\nimport {\n ExtractedSchema,\n JsonSchema,\n Schema,\n SchemaInput,\n SchemaInputVariant,\n SchemaSource,\n SchemaSourceInput,\n SchemaSourceVariant,\n UntypedInputObject,\n UntypedSchema,\n ValibotSchema\n} from \"./types\";\n\nconst SCHEMA_BUNDLE_BASE_URI = \"https://powerlines.invalid/\";\n\nfunction normalizeUri(uri: string): string {\n return uri.endsWith(\"#\") ? uri.slice(0, -1) : uri;\n}\n\nfunction stripUriFragment(uri: string): string {\n const hashIndex = uri.indexOf(\"#\");\n\n return hashIndex >= 0 ? uri.slice(0, hashIndex) : uri;\n}\n\nfunction escapeJsonPointerToken(token: string): string {\n return token.replaceAll(\"~\", \"~0\").replaceAll(\"/\", \"~1\");\n}\n\nfunction toJsonPointer(path: string[]): string {\n if (path.length === 0) {\n return \"\";\n }\n\n return `/${path.map(segment => escapeJsonPointerToken(segment)).join(\"/\")}`;\n}\n\nfunction resolveUri(reference: string, baseUri: string): string {\n try {\n return normalizeUri(new URL(reference, baseUri).toString());\n } catch {\n return normalizeUri(reference);\n }\n}\n\nfunction collectReferenceTargets(\n value: unknown,\n path: string[],\n baseUri: string,\n uriToPointer: Map<string, string>,\n dynamicUriToFragment: Map<string, string>\n): void {\n if (!isSetObject(value)) {\n return;\n }\n\n const schema = value as Record<string, unknown>;\n const pointer = toJsonPointer(path);\n\n const currentBaseUri = isSetString(schema.$id)\n ? resolveUri(schema.$id, baseUri)\n : baseUri;\n\n const currentDocumentUri = stripUriFragment(currentBaseUri);\n\n uriToPointer.set(currentBaseUri, pointer);\n uriToPointer.set(currentDocumentUri, pointer);\n\n if (isSetString(schema.$anchor)) {\n uriToPointer.set(`${currentDocumentUri}#${schema.$anchor}`, pointer);\n }\n\n if (isSetString(schema.$dynamicAnchor)) {\n const dynamicTarget = `${currentDocumentUri}#${schema.$dynamicAnchor}`;\n uriToPointer.set(dynamicTarget, pointer);\n dynamicUriToFragment.set(dynamicTarget, `#${schema.$dynamicAnchor}`);\n }\n\n for (const [key, child] of Object.entries(schema)) {\n if (Array.isArray(child)) {\n child.forEach((entry, index) => {\n collectReferenceTargets(\n entry,\n [...path, key, String(index)],\n currentBaseUri,\n uriToPointer,\n dynamicUriToFragment\n );\n });\n continue;\n }\n\n collectReferenceTargets(\n child,\n [...path, key],\n currentBaseUri,\n uriToPointer,\n dynamicUriToFragment\n );\n }\n}\n\nfunction rewriteReferenceTargets(\n value: unknown,\n path: string[],\n baseUri: string,\n uriToPointer: Map<string, string>,\n dynamicUriToFragment: Map<string, string>\n): void {\n if (!isSetObject(value)) {\n return;\n }\n\n const schema = value as Record<string, unknown>;\n\n const currentBaseUri = isSetString(schema.$id)\n ? resolveUri(schema.$id, baseUri)\n : baseUri;\n\n if (isSetString(schema.$ref)) {\n const resolvedRefUri = resolveUri(schema.$ref, currentBaseUri);\n const pointer =\n uriToPointer.get(resolvedRefUri) ??\n uriToPointer.get(stripUriFragment(resolvedRefUri));\n\n if (pointer !== undefined) {\n schema.$ref = pointer.length > 0 ? `#${pointer}` : \"#\";\n }\n }\n\n if (isSetString(schema.$dynamicRef)) {\n const resolvedDynamicRefUri = resolveUri(\n schema.$dynamicRef,\n currentBaseUri\n );\n const dynamicFragment = dynamicUriToFragment.get(resolvedDynamicRefUri);\n\n if (dynamicFragment) {\n schema.$dynamicRef = dynamicFragment;\n } else {\n const pointer =\n uriToPointer.get(resolvedDynamicRefUri) ??\n uriToPointer.get(stripUriFragment(resolvedDynamicRefUri));\n\n if (pointer !== undefined) {\n schema.$dynamicRef = pointer.length > 0 ? `#${pointer}` : \"#\";\n }\n }\n }\n\n for (const [key, child] of Object.entries(schema)) {\n if (Array.isArray(child)) {\n child.forEach((entry, index) => {\n rewriteReferenceTargets(\n entry,\n [...path, key, String(index)],\n currentBaseUri,\n uriToPointer,\n dynamicUriToFragment\n );\n });\n continue;\n }\n\n rewriteReferenceTargets(\n child,\n [...path, key],\n currentBaseUri,\n uriToPointer,\n dynamicUriToFragment\n );\n }\n}\n\n/**\n * Bundles all external references in a JSON Schema into a single schema document by collecting all reference targets and rewriting the references to point to the bundled definitions. This ensures that the resulting schema is self-contained and can be used independently without relying on external documents.\n *\n * @param schema - The JSON Schema to bundle references for.\n * @returns A new JSON Schema with all references bundled and rewritten to point to the bundled definitions.\n */\nexport function bundleReferences(schema: JsonSchema): JsonSchema {\n if (!isSetObject(schema)) {\n return schema;\n }\n\n const bundledSchema = deepClone(schema) as Record<string, unknown>;\n const baseUri = isSetString(bundledSchema.$id)\n ? resolveUri(bundledSchema.$id, SCHEMA_BUNDLE_BASE_URI)\n : SCHEMA_BUNDLE_BASE_URI;\n\n const uriToPointer = new Map<string, string>();\n const dynamicUriToFragment = new Map<string, string>();\n\n collectReferenceTargets(\n bundledSchema,\n [],\n baseUri,\n uriToPointer,\n dynamicUriToFragment\n );\n\n rewriteReferenceTargets(\n bundledSchema,\n [],\n baseUri,\n uriToPointer,\n dynamicUriToFragment\n );\n\n return bundledSchema;\n}\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(schema: unknown): JsonSchema {\n return toJsonSchema(schema as never, {\n target: \"draft-2020-12\"\n }) as JsonSchema;\n}\n\nfunction convertUntypedSchemaToJsonSchema(\n schema: UntypedSchema | Record<string, unknown>\n): JsonSchema {\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;\n}\n\nfunction convertUntypedInputToJsonSchema(\n input: UntypedInputObject\n): JsonSchema {\n const inputObject = input as Record<string, unknown>;\n const base = isUntypedSchema(inputObject.$schema)\n ? convertUntypedSchemaToJsonSchema(inputObject.$schema)\n : {};\n const properties: Record<string, JsonSchema> = {};\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(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(nested.$schema);\n continue;\n }\n\n if (isUntypedSchema(value)) {\n properties[key] = convertUntypedSchemaToJsonSchema(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) ? base.properties : {};\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(\n variant: SchemaInputVariant,\n input: SchemaInput\n): string {\n if (isSetString(input)) {\n return murmurhash({ variant, input });\n } else if (typeof input === \"boolean\") {\n return murmurhash({ variant, input });\n } else if (isSetObject(input)) {\n if (isZod3Type(input)) {\n return murmurhash({ variant, input: input._def });\n } else if (isType(input)) {\n return murmurhash({ variant, input: stringifyType(input) });\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 }\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-2020-12) representation.\n */\nexport function extractReflection(reflection: Type): JsonSchema | undefined {\n if (!isType(reflection)) {\n return undefined;\n }\n\n return reflectionToJsonSchema(reflection);\n}\n\n/**\n * Extracts a JSON Schema from Zod, Standard Schema, Valibot, untyped, or JSON Schema inputs.\n *\n * @param schema - The schema input to extract a JSON Schema from.\n * @returns The extracted JSON Schema, or `undefined` if the input is not a supported schema type.\n */\nexport function extractJsonSchema(schema: unknown): JsonSchema | undefined {\n if (isSetObject(schema)) {\n if (isZod3Type(schema)) {\n return extractJsonSchemaZod(schema, {\n target: \"draft-2020-12\"\n }) as JsonSchema;\n }\n if (isUntypedInputStrict(schema)) {\n return convertUntypedInputToJsonSchema(schema);\n }\n if (isUntypedSchemaStrict(schema)) {\n return convertUntypedSchemaToJsonSchema(schema);\n }\n if (isStandardJsonSchema(schema)) {\n return schema[\"~standard\"].jsonSchema.input({\n target: \"draft-2020-12\"\n });\n }\n if (isValibotSchema(schema)) {\n return convertValibotSchemaToJsonSchema(schema);\n }\n if (isJsonSchema(schema)) {\n return schema;\n }\n }\n\n return undefined;\n}\n\n/**\n * Resolves the concrete source variant for a schema source input.\n *\n * @param input - The schema source input to inspect.\n * @returns The resolved schema source variant.\n * @throws Will throw an error when the input cannot be mapped to a supported source variant.\n */\nexport function extractResolvedVariant(\n input: SchemaSourceInput\n): SchemaSourceVariant {\n if (isSetObject(input)) {\n if (isZod3Type(input)) {\n return \"zod3\";\n } else if (isType(input)) {\n return \"reflection\";\n } else if (isUntypedInputStrict(input) || isUntypedSchemaStrict(input)) {\n return \"untyped\";\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 }\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\n/**\n * Determines the top-level input variant for schema extraction.\n *\n * @param input - The schema input to classify.\n * @returns The resolved schema input variant.\n */\nexport function extractVariant(input: SchemaInput): SchemaInputVariant {\n if (isSetString(input) || isFileReference(input)) {\n return \"file-reference\";\n }\n\n return extractResolvedVariant(input as SchemaSourceInput);\n}\n\n/**\n * Extracts and normalizes a JSON Schema from a concrete schema source input.\n *\n * @param input - The schema source input to extract from.\n * @param variant - Optional source variant override. When omitted, the variant is inferred from the input.\n * @returns A promise that resolves to a bundled JSON Schema.\n * @throws Will throw an error if no valid JSON Schema can be extracted from the input.\n */\nexport async function extractSchema(\n input: SchemaSourceInput,\n variant?: SchemaInputVariant\n): Promise<JsonSchema> {\n if (isSchemaWithSource(input)) {\n return input.schema;\n }\n\n const resolvedVariant = variant ?? extractResolvedVariant(input);\n\n let schema: JsonSchema | undefined;\n if (\n resolvedVariant === \"zod3\" ||\n resolvedVariant === \"json-schema\" ||\n resolvedVariant === \"standard-schema\" ||\n resolvedVariant === \"untyped\" ||\n resolvedVariant === \"valibot\"\n ) {\n schema = extractJsonSchema(input);\n } else if (resolvedVariant === \"reflection\") {\n schema = extractReflection(input as Type);\n }\n\n if (schema) {\n return bundleReferences(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\n/**\n * Builds source metadata for a schema input using a known source variant.\n *\n * @param variant - The schema source variant associated with the input.\n * @param input - The schema source input to wrap.\n * @returns The normalized schema source payload, including the source hash and variant.\n * @throws Will throw an error if the provided variant is unsupported.\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 Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, or a 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 Rolldown 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://zod.dev/\n * @see https://valibot.dev/\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 Valibot schema, any Standard JSON Schema type, a plain 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 Rolldown 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 extractSchemaWithSource(\n context: Context,\n input: SchemaInput,\n options: BundleOptions = {}\n): Promise<ExtractedSchema> {\n if (isSchemaWithSource(input)) {\n return input;\n }\n\n if (isSchema(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 === \"file-reference\") {\n const fileReference = extractFileReference(input as FileReferenceInput);\n if (!fileReference) {\n throw new Error(\n `Failed to extract a valid file reference from the provided input \"${JSON.stringify(\n input\n )}\". Please ensure that the input is correctly formatted as a file reference (e.g. \"./schema.ts#MySchema\") and that the file exists at the specified path.`\n );\n }\n\n const extension = findFileExtensionSafe(fileReference.file);\n if (extension && !VALID_SOURCE_FILE_EXTENSIONS.includes(extension)) {\n throw new Error(\n `The provided schema file input \"${\n fileReference.file\n }\" has an invalid file extension (.${\n extension\n }). Please ensure that the file has one of the following extensions: ${list(\n VALID_SOURCE_FILE_EXTENSIONS,\n { conjunction: \"or\" }\n )}.`\n );\n }\n\n let resolved = await resolve<SchemaSourceInput>(\n context,\n input as FileReferenceInput,\n defu(options, {\n plugins: [\n esbuildPlugin(context, {\n reflection: \"default\",\n level: \"all\"\n })\n ]\n })\n );\n\n try {\n const type = deserializeType(resolved as SerializedType[]);\n if (isType(type)) {\n resolved = type;\n }\n } catch {\n // If deserialization fails, we assume the resolved output is not a reflected type and proceed with it as-is.\n }\n\n source = extractSource(extractResolvedVariant(resolved), resolved);\n } else if (\n [\n \"json-schema\",\n \"standard-schema\",\n \"zod3\",\n \"untyped\",\n \"valibot\",\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 \"file-reference\", \"json-schema\", \"standard-schema\", \"zod3\", \"valibot\", \"untyped\", or \"reflection\".`\n );\n }\n\n return {\n variant,\n source,\n schema: await extractSchema(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 Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, or a 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 Rolldown 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 Valibot schema\n * const schema4 = await extract(context, valibotSchema);\n * // Resolve a schema definition from a reflected Deepkit Type object\n * const schema5 = await extract(context, reflectionType);\n * ```\n *\n * @see https://zod.dev/\n * @see https://valibot.dev/\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 Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, or a reflected Deepkit Type object.\n * @param options - Optional overrides for the Rolldown 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(\n context: Context,\n input: SchemaInput,\n options: BundleOptions = {}\n): Promise<Schema> {\n if (isSchemaWithSource(input) || isSchema(input)) {\n return input;\n }\n\n let result: Schema | 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 extractSchemaWithSource(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 Valibot schema, any Standard JSON Schema type, a plain 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4EA,MAAM,yBAAyB;AAE/B,SAAS,aAAa,KAAqB;CACzC,OAAO,IAAI,SAAS,GAAG,IAAI,IAAI,MAAM,GAAG,EAAE,IAAI;AAChD;AAEA,SAAS,iBAAiB,KAAqB;CAC7C,MAAM,YAAY,IAAI,QAAQ,GAAG;CAEjC,OAAO,aAAa,IAAI,IAAI,MAAM,GAAG,SAAS,IAAI;AACpD;AAEA,SAAS,uBAAuB,OAAuB;CACrD,OAAO,MAAM,WAAW,KAAK,IAAI,EAAE,WAAW,KAAK,IAAI;AACzD;AAEA,SAAS,cAAc,MAAwB;CAC7C,IAAI,KAAK,WAAW,GAClB,OAAO;CAGT,OAAO,IAAI,KAAK,KAAI,YAAW,uBAAuB,OAAO,CAAC,EAAE,KAAK,GAAG;AAC1E;AAEA,SAAS,WAAW,WAAmB,SAAyB;CAC9D,IAAI;EACF,OAAO,aAAa,IAAI,IAAI,WAAW,OAAO,EAAE,SAAS,CAAC;CAC5D,QAAQ;EACN,OAAO,aAAa,SAAS;CAC/B;AACF;AAEA,SAAS,wBACP,OACA,MACA,SACA,cACA,sBACM;CACN,IAAI,CAACA,cAAY,KAAK,GACpB;CAGF,MAAM,SAAS;CACf,MAAM,UAAU,cAAc,IAAI;CAElC,MAAM,iBAAiB,YAAY,OAAO,GAAG,IACzC,WAAW,OAAO,KAAK,OAAO,IAC9B;CAEJ,MAAM,qBAAqB,iBAAiB,cAAc;CAE1D,aAAa,IAAI,gBAAgB,OAAO;CACxC,aAAa,IAAI,oBAAoB,OAAO;CAE5C,IAAI,YAAY,OAAO,OAAO,GAC5B,aAAa,IAAI,GAAG,mBAAmB,GAAG,OAAO,WAAW,OAAO;CAGrE,IAAI,YAAY,OAAO,cAAc,GAA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GnC,OAAO;AACT"}
|
package/dist/helpers.cjs
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import __tsdown_shims_path from 'node:path'
|
|
4
|
-
import __tsdown_shims_url from 'node:url'
|
|
5
|
-
|
|
6
|
-
const __TSDOWN_SHIM_FILENAME__ = /* @__PURE__ */ __tsdown_shims_url.fileURLToPath(import.meta.url)
|
|
7
|
-
const __TSDOWN_SHIM_DIRNAME__ = /* @__PURE__ */ __tsdown_shims_path.dirname(__TSDOWN_SHIM_FILENAME__)
|
|
8
|
-
|
|
9
|
-
const require_constants = require('./constants.cjs');
|
|
10
|
-
const require_metadata = require('./metadata.cjs');
|
|
11
|
-
const require_type_checks = require('./type-checks.cjs');
|
|
12
|
-
let defu = require("defu");
|
|
13
|
-
let _stryke_type_checks = require("@stryke/type-checks");
|
|
14
|
-
let _stryke_helpers_get_unique = require("@stryke/helpers/get-unique");
|
|
15
|
-
let _stryke_path_find = require("@stryke/path/find");
|
|
16
|
-
|
|
17
|
-
//#region src/helpers.ts
|
|
18
|
-
/**
|
|
19
|
-
* Retrieves the JSON Schema from a Schema wrapper or returns the input if it's already a JSON Schema.
|
|
20
|
-
*
|
|
21
|
-
* @remarks
|
|
22
|
-
* This function checks if the input is a Schema wrapper (an object with a `schema` property) and returns the `schema` if it is. If the input is already a JSON Schema, it returns it directly. This allows for flexibility in handling both raw JSON Schema objects and wrapped schemas without needing to check the type at every usage point.
|
|
23
|
-
*
|
|
24
|
-
* @param input - The input which can be either a Schema wrapper or a JSON Schema object.
|
|
25
|
-
* @returns The JSON Schema object.
|
|
26
|
-
* @throws Will throw a TypeError if the input is neither a valid Schema wrapper nor a valid JSON Schema object.
|
|
27
|
-
*/
|
|
28
|
-
function getJsonSchema(input) {
|
|
29
|
-
const schema = require_type_checks.isSchema(input) ? input.schema : input;
|
|
30
|
-
if (!require_type_checks.isJsonSchema(schema)) throw new TypeError(`The provided input is not a valid JSON Schema: ${JSON.stringify(schema, null, 2)}`);
|
|
31
|
-
return schema;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Retrieves the JSON Schema in Object form from a Schema wrapper or returns the input if it's already a JSON Schema.
|
|
35
|
-
*
|
|
36
|
-
* @remarks
|
|
37
|
-
* This function checks if the input is a Schema wrapper (an object with a `schema` property) and returns the `schema` if it is. If the input is already a JSON Schema object, it returns it directly. This allows for flexibility in handling both raw JSON Schema objects and wrapped schemas without needing to check the type at every usage point.
|
|
38
|
-
*
|
|
39
|
-
* @param input - The input which can be either a Schema wrapper or a JSON Schema object.
|
|
40
|
-
* @returns The JSON Schema object.
|
|
41
|
-
* @throws Will throw a TypeError if the input is neither a valid Schema wrapper nor a valid JSON Schema object.
|
|
42
|
-
*/
|
|
43
|
-
function getJsonSchemaObject(input) {
|
|
44
|
-
const schema = getJsonSchema(input);
|
|
45
|
-
if (!require_type_checks.isJsonSchemaObject(schema)) throw new TypeError(`The provided input is not a valid JSON Schema object: ${JSON.stringify(schema, null, 2)}`);
|
|
46
|
-
return schema;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Extracts object properties from a JSON Schema object form.
|
|
50
|
-
*
|
|
51
|
-
* @remarks
|
|
52
|
-
* This function returns an empty object if the schema is not an object form or if it has no properties.
|
|
53
|
-
*
|
|
54
|
-
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
55
|
-
* @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.
|
|
56
|
-
*/
|
|
57
|
-
function getProperties(obj) {
|
|
58
|
-
const properties = {};
|
|
59
|
-
const schema = getJsonSchemaObject(obj);
|
|
60
|
-
if (!(0, _stryke_type_checks.isSetObject)(schema.properties)) return properties;
|
|
61
|
-
for (const [key, value] of Object.entries(schema.properties)) {
|
|
62
|
-
const propertySchema = {};
|
|
63
|
-
if (typeof value !== "boolean") Object.assign(propertySchema, value);
|
|
64
|
-
properties[key] = {
|
|
65
|
-
...propertySchema,
|
|
66
|
-
name: key,
|
|
67
|
-
required: !isPropertyOptional(schema, key),
|
|
68
|
-
default: schema.default?.[key] ?? propertySchema.default
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
return properties;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Returns object properties as an array.
|
|
75
|
-
*
|
|
76
|
-
* @remarks
|
|
77
|
-
* This is a convenience function that extracts properties using `getProperties` and returns them as an array.
|
|
78
|
-
*
|
|
79
|
-
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
80
|
-
* @returns An array of JSON Schema fragments representing the properties, each including metadata.
|
|
81
|
-
*/
|
|
82
|
-
function getPropertiesList(obj) {
|
|
83
|
-
return Object.values(getProperties(obj));
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Adds a property to a JSON Schema object form.
|
|
87
|
-
*
|
|
88
|
-
* @remarks
|
|
89
|
-
* 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.
|
|
90
|
-
*
|
|
91
|
-
* @param obj - The JSON Schema object form or a Schema wrapper to which the property should be added.
|
|
92
|
-
* @param name - The name of the property to add.
|
|
93
|
-
* @param property - The JSON Schema fragment representing the property's schema, including metadata.
|
|
94
|
-
* @throws Will throw an error if the provided schema is not an object form.
|
|
95
|
-
*/
|
|
96
|
-
function addProperty(obj, name, property) {
|
|
97
|
-
const schema = getJsonSchemaObject(obj);
|
|
98
|
-
schema.properties ??= {};
|
|
99
|
-
schema.required ??= [];
|
|
100
|
-
schema.properties[name] = {
|
|
101
|
-
...property,
|
|
102
|
-
name
|
|
103
|
-
};
|
|
104
|
-
if (!schema.required.includes(name)) schema.required.push(name);
|
|
105
|
-
if (schema.required.length === 0) delete schema.required;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Merges multiple JSON Schemas into one.
|
|
109
|
-
*
|
|
110
|
-
* @remarks
|
|
111
|
-
* This function takes multiple JSON Schemas 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.
|
|
112
|
-
*
|
|
113
|
-
* @param schemas - An array of JSON Schemas or Schema wrappers to merge.
|
|
114
|
-
* @returns A new JSON Schema that is the result of merging all input schemas.
|
|
115
|
-
*/
|
|
116
|
-
function merge(...schemas) {
|
|
117
|
-
let result = {};
|
|
118
|
-
for (const schema of schemas.reverse()) if (!result.type || result.type === schema.type) {
|
|
119
|
-
result = (0, defu.defu)(result, getJsonSchema(schema));
|
|
120
|
-
if (require_type_checks.isJsonSchemaObject(result)) result.required = (0, _stryke_helpers_get_unique.getUnique)(result.required ?? []);
|
|
121
|
-
}
|
|
122
|
-
return result;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Returns whether a JSON Schema fragment accepts `null`.
|
|
126
|
-
*
|
|
127
|
-
* @remarks
|
|
128
|
-
* This is true if the schema has `nullable: true` or if its `type` includes `"null"`.
|
|
129
|
-
*
|
|
130
|
-
* @param schema - The JSON Schema fragment to check.
|
|
131
|
-
* @returns `true` if the schema accepts `null`, otherwise `false`.
|
|
132
|
-
*/
|
|
133
|
-
function isSchemaNullable(schema) {
|
|
134
|
-
if (!(0, _stryke_type_checks.isSetObject)(schema)) return false;
|
|
135
|
-
if (schema.nullable === true) return true;
|
|
136
|
-
return require_metadata.readSchemaTypes(schema).includes("null");
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Returns whether an object property is optional (not listed in `required`).
|
|
140
|
-
*
|
|
141
|
-
* @remarks
|
|
142
|
-
* 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.
|
|
143
|
-
*
|
|
144
|
-
* @param parent - The parent JSON Schema object containing the property.
|
|
145
|
-
* @param propertyName - The name of the property to check for optionality.
|
|
146
|
-
* @returns `true` if the property is optional, otherwise `false`.
|
|
147
|
-
*/
|
|
148
|
-
function isPropertyOptional(parent, propertyName) {
|
|
149
|
-
if (!parent.properties?.[propertyName]) throw new Error(`The property "${propertyName}" does not exist in the parent schema.`);
|
|
150
|
-
return !(parent.required ?? []).includes(propertyName);
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Checks if a given file name has a valid schema input file extension.
|
|
154
|
-
*
|
|
155
|
-
* @param fileName - The file name to check for a valid schema input extension.
|
|
156
|
-
* @returns `true` if the file name has a valid schema input extension, otherwise `false`.
|
|
157
|
-
*/
|
|
158
|
-
function isValidSchemaInputFile(fileName) {
|
|
159
|
-
return require_constants.VALID_SOURCE_FILE_EXTENSIONS.includes((0, _stryke_path_find.findFileExtensionSafe)(fileName));
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
//#endregion
|
|
163
|
-
exports.addProperty = addProperty;
|
|
164
|
-
exports.getJsonSchema = getJsonSchema;
|
|
165
|
-
exports.getJsonSchemaObject = getJsonSchemaObject;
|
|
166
|
-
exports.getProperties = getProperties;
|
|
167
|
-
exports.getPropertiesList = getPropertiesList;
|
|
168
|
-
exports.isPropertyOptional = isPropertyOptional;
|
|
169
|
-
exports.isSchemaNullable = isSchemaNullable;
|
|
170
|
-
exports.isValidSchemaInputFile = isValidSchemaInputFile;
|
|
171
|
-
exports.merge = merge;
|
package/dist/helpers.d.cts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { JsonSchema, JsonSchemaObject, Schema } from "./types.cjs";
|
|
2
|
-
|
|
3
|
-
//#region src/helpers.d.ts
|
|
4
|
-
type GetPropertiesResult = JsonSchema & {
|
|
5
|
-
name: string;
|
|
6
|
-
required: boolean;
|
|
7
|
-
default?: unknown;
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* Retrieves the JSON Schema from a Schema wrapper or returns the input if it's already a JSON Schema.
|
|
11
|
-
*
|
|
12
|
-
* @remarks
|
|
13
|
-
* This function checks if the input is a Schema wrapper (an object with a `schema` property) and returns the `schema` if it is. If the input is already a JSON Schema, it returns it directly. This allows for flexibility in handling both raw JSON Schema objects and wrapped schemas without needing to check the type at every usage point.
|
|
14
|
-
*
|
|
15
|
-
* @param input - The input which can be either a Schema wrapper or a JSON Schema object.
|
|
16
|
-
* @returns The JSON Schema object.
|
|
17
|
-
* @throws Will throw a TypeError if the input is neither a valid Schema wrapper nor a valid JSON Schema object.
|
|
18
|
-
*/
|
|
19
|
-
declare function getJsonSchema(input: Schema | JsonSchema): JsonSchema;
|
|
20
|
-
/**
|
|
21
|
-
* Retrieves the JSON Schema in Object form from a Schema wrapper or returns the input if it's already a JSON Schema.
|
|
22
|
-
*
|
|
23
|
-
* @remarks
|
|
24
|
-
* This function checks if the input is a Schema wrapper (an object with a `schema` property) and returns the `schema` if it is. If the input is already a JSON Schema object, it returns it directly. This allows for flexibility in handling both raw JSON Schema objects and wrapped schemas without needing to check the type at every usage point.
|
|
25
|
-
*
|
|
26
|
-
* @param input - The input which can be either a Schema wrapper or a JSON Schema object.
|
|
27
|
-
* @returns The JSON Schema object.
|
|
28
|
-
* @throws Will throw a TypeError if the input is neither a valid Schema wrapper nor a valid JSON Schema object.
|
|
29
|
-
*/
|
|
30
|
-
declare function getJsonSchemaObject(input: Schema | JsonSchema): JsonSchemaObject;
|
|
31
|
-
/**
|
|
32
|
-
* Extracts object properties from a JSON Schema object form.
|
|
33
|
-
*
|
|
34
|
-
* @remarks
|
|
35
|
-
* This function returns an empty object if the schema is not an object form or if it has no properties.
|
|
36
|
-
*
|
|
37
|
-
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
38
|
-
* @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.
|
|
39
|
-
*/
|
|
40
|
-
declare function getProperties(obj: Schema | JsonSchemaObject): Record<string, JsonSchema & {
|
|
41
|
-
name: string;
|
|
42
|
-
required: boolean;
|
|
43
|
-
default?: unknown;
|
|
44
|
-
}>;
|
|
45
|
-
/**
|
|
46
|
-
* Returns object properties as an array.
|
|
47
|
-
*
|
|
48
|
-
* @remarks
|
|
49
|
-
* This is a convenience function that extracts properties using `getProperties` and returns them as an array.
|
|
50
|
-
*
|
|
51
|
-
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
52
|
-
* @returns An array of JSON Schema fragments representing the properties, each including metadata.
|
|
53
|
-
*/
|
|
54
|
-
declare function getPropertiesList(obj: Schema | JsonSchemaObject): (JsonSchema & {
|
|
55
|
-
name: string;
|
|
56
|
-
required: boolean;
|
|
57
|
-
default?: unknown;
|
|
58
|
-
})[];
|
|
59
|
-
/**
|
|
60
|
-
* Adds a property to a JSON Schema object form.
|
|
61
|
-
*
|
|
62
|
-
* @remarks
|
|
63
|
-
* 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.
|
|
64
|
-
*
|
|
65
|
-
* @param obj - The JSON Schema object form or a Schema wrapper to which the property should be added.
|
|
66
|
-
* @param name - The name of the property to add.
|
|
67
|
-
* @param property - The JSON Schema fragment representing the property's schema, including metadata.
|
|
68
|
-
* @throws Will throw an error if the provided schema is not an object form.
|
|
69
|
-
*/
|
|
70
|
-
declare function addProperty(obj: Schema | JsonSchemaObject, name: string, property: JsonSchema): void;
|
|
71
|
-
/**
|
|
72
|
-
* Merges multiple JSON Schemas into one.
|
|
73
|
-
*
|
|
74
|
-
* @remarks
|
|
75
|
-
* This function takes multiple JSON Schemas 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.
|
|
76
|
-
*
|
|
77
|
-
* @param schemas - An array of JSON Schemas or Schema wrappers to merge.
|
|
78
|
-
* @returns A new JSON Schema that is the result of merging all input schemas.
|
|
79
|
-
*/
|
|
80
|
-
declare function merge(...schemas: (JsonSchema | Schema)[]): JsonSchema;
|
|
81
|
-
/**
|
|
82
|
-
* Returns whether a JSON Schema fragment accepts `null`.
|
|
83
|
-
*
|
|
84
|
-
* @remarks
|
|
85
|
-
* This is true if the schema has `nullable: true` or if its `type` includes `"null"`.
|
|
86
|
-
*
|
|
87
|
-
* @param schema - The JSON Schema fragment to check.
|
|
88
|
-
* @returns `true` if the schema accepts `null`, otherwise `false`.
|
|
89
|
-
*/
|
|
90
|
-
declare function isSchemaNullable(schema?: JsonSchema): boolean;
|
|
91
|
-
/**
|
|
92
|
-
* Returns whether an object property is optional (not listed in `required`).
|
|
93
|
-
*
|
|
94
|
-
* @remarks
|
|
95
|
-
* 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.
|
|
96
|
-
*
|
|
97
|
-
* @param parent - The parent JSON Schema object containing the property.
|
|
98
|
-
* @param propertyName - The name of the property to check for optionality.
|
|
99
|
-
* @returns `true` if the property is optional, otherwise `false`.
|
|
100
|
-
*/
|
|
101
|
-
declare function isPropertyOptional(parent: JsonSchemaObject, propertyName: string): boolean;
|
|
102
|
-
/**
|
|
103
|
-
* Checks if a given file name has a valid schema input file extension.
|
|
104
|
-
*
|
|
105
|
-
* @param fileName - The file name to check for a valid schema input extension.
|
|
106
|
-
* @returns `true` if the file name has a valid schema input extension, otherwise `false`.
|
|
107
|
-
*/
|
|
108
|
-
declare function isValidSchemaInputFile(fileName: string): boolean;
|
|
109
|
-
//#endregion
|
|
110
|
-
export { GetPropertiesResult, addProperty, getJsonSchema, getJsonSchemaObject, getProperties, getPropertiesList, isPropertyOptional, isSchemaNullable, isValidSchemaInputFile, merge };
|
|
111
|
-
//# sourceMappingURL=helpers.d.cts.map
|
package/dist/helpers.d.cts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.cts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;KA2BY,mBAAA,GAAsB,UAAU;EAC1C,IAAA;EACA,QAAA;EACA,OAAA;AAAA;;;;;;;AAAO;AAaT;;;iBAAgB,aAAA,CAAc,KAAA,EAAO,MAAA,GAAS,UAAA,GAAa,UAAA;;;;;;;;;;AAAU;iBAyBrD,mBAAA,CACd,KAAA,EAAO,MAAA,GAAS,UAAA,GACf,gBAAA;;;;;;;;;;iBAoBa,aAAA,CACd,GAAA,EAAK,MAAA,GAAS,gBAAA,GACb,MAAA,SAED,UAAA;EAAe,IAAA;EAAc,QAAA;EAAmB,OAAA;AAAA;;;;;;;;;;iBAuClC,iBAAA,CAAkB,GAAA,EAAK,MAAA,GAAS,gBAAA,IAAgB,UAAA;;;;;;;;AAvCP;AAuCzD;;;;;;;iBAegB,WAAA,CACd,GAAA,EAAK,MAAA,GAAS,gBAAA,EACd,IAAA,UACA,QAAA,EAAU,UAAA;;;;;;;;;;iBA0BI,KAAA,IAAS,OAAA,GAAU,UAAA,GAAa,MAAA,MAAY,UAAA;;;;;;;;;;iBA0B5C,gBAAA,CAAiB,MAAmB,GAAV,UAAU;;;;;AApD9B;AA0BtB;;;;;iBAgDgB,kBAAA,CACd,MAAA,EAAQ,gBAAgB,EACxB,YAAA;;;;;;;iBAiBc,sBAAA,CAAuB,QAAgB"}
|
package/dist/helpers.d.mts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import __tsdown_shims_path from 'node:path';
|
|
2
|
-
import __tsdown_shims_url from 'node:url';
|
|
3
|
-
import { JsonSchema, JsonSchemaObject, Schema } from "./types.mjs";
|
|
4
|
-
|
|
5
|
-
//#region src/helpers.d.ts
|
|
6
|
-
type GetPropertiesResult = JsonSchema & {
|
|
7
|
-
name: string;
|
|
8
|
-
required: boolean;
|
|
9
|
-
default?: unknown;
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* Retrieves the JSON Schema from a Schema wrapper or returns the input if it's already a JSON Schema.
|
|
13
|
-
*
|
|
14
|
-
* @remarks
|
|
15
|
-
* This function checks if the input is a Schema wrapper (an object with a `schema` property) and returns the `schema` if it is. If the input is already a JSON Schema, it returns it directly. This allows for flexibility in handling both raw JSON Schema objects and wrapped schemas without needing to check the type at every usage point.
|
|
16
|
-
*
|
|
17
|
-
* @param input - The input which can be either a Schema wrapper or a JSON Schema object.
|
|
18
|
-
* @returns The JSON Schema object.
|
|
19
|
-
* @throws Will throw a TypeError if the input is neither a valid Schema wrapper nor a valid JSON Schema object.
|
|
20
|
-
*/
|
|
21
|
-
declare function getJsonSchema(input: Schema | JsonSchema): JsonSchema;
|
|
22
|
-
/**
|
|
23
|
-
* Retrieves the JSON Schema in Object form from a Schema wrapper or returns the input if it's already a JSON Schema.
|
|
24
|
-
*
|
|
25
|
-
* @remarks
|
|
26
|
-
* This function checks if the input is a Schema wrapper (an object with a `schema` property) and returns the `schema` if it is. If the input is already a JSON Schema object, it returns it directly. This allows for flexibility in handling both raw JSON Schema objects and wrapped schemas without needing to check the type at every usage point.
|
|
27
|
-
*
|
|
28
|
-
* @param input - The input which can be either a Schema wrapper or a JSON Schema object.
|
|
29
|
-
* @returns The JSON Schema object.
|
|
30
|
-
* @throws Will throw a TypeError if the input is neither a valid Schema wrapper nor a valid JSON Schema object.
|
|
31
|
-
*/
|
|
32
|
-
declare function getJsonSchemaObject(input: Schema | JsonSchema): JsonSchemaObject;
|
|
33
|
-
/**
|
|
34
|
-
* Extracts object properties from a JSON Schema object form.
|
|
35
|
-
*
|
|
36
|
-
* @remarks
|
|
37
|
-
* This function returns an empty object if the schema is not an object form or if it has no properties.
|
|
38
|
-
*
|
|
39
|
-
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
40
|
-
* @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.
|
|
41
|
-
*/
|
|
42
|
-
declare function getProperties(obj: Schema | JsonSchemaObject): Record<string, JsonSchema & {
|
|
43
|
-
name: string;
|
|
44
|
-
required: boolean;
|
|
45
|
-
default?: unknown;
|
|
46
|
-
}>;
|
|
47
|
-
/**
|
|
48
|
-
* Returns object properties as an array.
|
|
49
|
-
*
|
|
50
|
-
* @remarks
|
|
51
|
-
* This is a convenience function that extracts properties using `getProperties` and returns them as an array.
|
|
52
|
-
*
|
|
53
|
-
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
54
|
-
* @returns An array of JSON Schema fragments representing the properties, each including metadata.
|
|
55
|
-
*/
|
|
56
|
-
declare function getPropertiesList(obj: Schema | JsonSchemaObject): (JsonSchema & {
|
|
57
|
-
name: string;
|
|
58
|
-
required: boolean;
|
|
59
|
-
default?: unknown;
|
|
60
|
-
})[];
|
|
61
|
-
/**
|
|
62
|
-
* Adds a property to a JSON Schema object form.
|
|
63
|
-
*
|
|
64
|
-
* @remarks
|
|
65
|
-
* 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.
|
|
66
|
-
*
|
|
67
|
-
* @param obj - The JSON Schema object form or a Schema wrapper to which the property should be added.
|
|
68
|
-
* @param name - The name of the property to add.
|
|
69
|
-
* @param property - The JSON Schema fragment representing the property's schema, including metadata.
|
|
70
|
-
* @throws Will throw an error if the provided schema is not an object form.
|
|
71
|
-
*/
|
|
72
|
-
declare function addProperty(obj: Schema | JsonSchemaObject, name: string, property: JsonSchema): void;
|
|
73
|
-
/**
|
|
74
|
-
* Merges multiple JSON Schemas into one.
|
|
75
|
-
*
|
|
76
|
-
* @remarks
|
|
77
|
-
* This function takes multiple JSON Schemas 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.
|
|
78
|
-
*
|
|
79
|
-
* @param schemas - An array of JSON Schemas or Schema wrappers to merge.
|
|
80
|
-
* @returns A new JSON Schema that is the result of merging all input schemas.
|
|
81
|
-
*/
|
|
82
|
-
declare function merge(...schemas: (JsonSchema | Schema)[]): JsonSchema;
|
|
83
|
-
/**
|
|
84
|
-
* Returns whether a JSON Schema fragment accepts `null`.
|
|
85
|
-
*
|
|
86
|
-
* @remarks
|
|
87
|
-
* This is true if the schema has `nullable: true` or if its `type` includes `"null"`.
|
|
88
|
-
*
|
|
89
|
-
* @param schema - The JSON Schema fragment to check.
|
|
90
|
-
* @returns `true` if the schema accepts `null`, otherwise `false`.
|
|
91
|
-
*/
|
|
92
|
-
declare function isSchemaNullable(schema?: JsonSchema): boolean;
|
|
93
|
-
/**
|
|
94
|
-
* Returns whether an object property is optional (not listed in `required`).
|
|
95
|
-
*
|
|
96
|
-
* @remarks
|
|
97
|
-
* 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.
|
|
98
|
-
*
|
|
99
|
-
* @param parent - The parent JSON Schema object containing the property.
|
|
100
|
-
* @param propertyName - The name of the property to check for optionality.
|
|
101
|
-
* @returns `true` if the property is optional, otherwise `false`.
|
|
102
|
-
*/
|
|
103
|
-
declare function isPropertyOptional(parent: JsonSchemaObject, propertyName: string): boolean;
|
|
104
|
-
/**
|
|
105
|
-
* Checks if a given file name has a valid schema input file extension.
|
|
106
|
-
*
|
|
107
|
-
* @param fileName - The file name to check for a valid schema input extension.
|
|
108
|
-
* @returns `true` if the file name has a valid schema input extension, otherwise `false`.
|
|
109
|
-
*/
|
|
110
|
-
declare function isValidSchemaInputFile(fileName: string): boolean;
|
|
111
|
-
//#endregion
|
|
112
|
-
export { GetPropertiesResult, addProperty, getJsonSchema, getJsonSchemaObject, getProperties, getPropertiesList, isPropertyOptional, isSchemaNullable, isValidSchemaInputFile, merge };
|
|
113
|
-
//# sourceMappingURL=helpers.d.mts.map
|
package/dist/helpers.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.mts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;KA2BY,mBAAA,GAAsB,UAAU;EAC1C,IAAA;EACA,QAAA;EACA,OAAA;AAAA;;;;;;AAHF;;;;;iBAgBgB,aAAA,CAAc,KAAA,EAAO,MAAA,GAAS,UAAA,GAAa,UAAA;;;;AAblD;AAaT;;;;;;iBAyBgB,mBAAA,CACd,KAAA,EAAO,MAAA,GAAS,UAAA,GACf,gBAAA;;;;;;;AA3BkE;AAyBrE;;iBAsBgB,aAAA,CACd,GAAA,EAAK,MAAA,GAAS,gBAAA,GACb,MAAA,SAED,UAAA;EAAe,IAAA;EAAc,QAAA;EAAmB,OAAA;AAAA;;;;;;;AAxB/B;AAoBnB;;iBA2CgB,iBAAA,CAAkB,GAAA,EAAK,MAAA,GAAS,gBAAA,IAAgB,UAAA;;;;;;;;;;;;;;;;iBAehD,WAAA,CACd,GAAA,EAAK,MAAA,GAAS,gBAAA,EACd,IAAA,UACA,QAAA,EAAU,UAAA;AAlBZ;;;;;;;;;AAAA,iBA4CgB,KAAA,IAAS,OAAA,GAAU,UAAA,GAAa,MAAA,MAAY,UAAA;;;;;;;;AA7B5D;;iBAuDgB,gBAAA,CAAiB,MAAmB,GAAV,UAAU;;;;;;;;;;;iBAsBpC,kBAAA,CACd,MAAA,EAAQ,gBAAgB,EACxB,YAAA;;AA5EoB;AA0BtB;;;;iBAmEgB,sBAAA,CAAuB,QAAgB"}
|
package/dist/helpers.mjs
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import __tsdown_shims_path from 'node:path'
|
|
4
|
-
import __tsdown_shims_url from 'node:url'
|
|
5
|
-
|
|
6
|
-
const __TSDOWN_SHIM_FILENAME__ = /* @__PURE__ */ __tsdown_shims_url.fileURLToPath(import.meta.url)
|
|
7
|
-
const __TSDOWN_SHIM_DIRNAME__ = /* @__PURE__ */ __tsdown_shims_path.dirname(__TSDOWN_SHIM_FILENAME__)
|
|
8
|
-
|
|
9
|
-
import { VALID_SOURCE_FILE_EXTENSIONS } from "./constants.mjs";
|
|
10
|
-
import { readSchemaTypes } from "./metadata.mjs";
|
|
11
|
-
import { isJsonSchema, isJsonSchemaObject, isSchema } from "./type-checks.mjs";
|
|
12
|
-
import { defu as defu$1 } from "defu";
|
|
13
|
-
import { isSetObject } from "@stryke/type-checks";
|
|
14
|
-
import { getUnique } from "@stryke/helpers/get-unique";
|
|
15
|
-
import { findFileExtensionSafe } from "@stryke/path/find";
|
|
16
|
-
|
|
17
|
-
//#region src/helpers.ts
|
|
18
|
-
/**
|
|
19
|
-
* Retrieves the JSON Schema from a Schema wrapper or returns the input if it's already a JSON Schema.
|
|
20
|
-
*
|
|
21
|
-
* @remarks
|
|
22
|
-
* This function checks if the input is a Schema wrapper (an object with a `schema` property) and returns the `schema` if it is. If the input is already a JSON Schema, it returns it directly. This allows for flexibility in handling both raw JSON Schema objects and wrapped schemas without needing to check the type at every usage point.
|
|
23
|
-
*
|
|
24
|
-
* @param input - The input which can be either a Schema wrapper or a JSON Schema object.
|
|
25
|
-
* @returns The JSON Schema object.
|
|
26
|
-
* @throws Will throw a TypeError if the input is neither a valid Schema wrapper nor a valid JSON Schema object.
|
|
27
|
-
*/
|
|
28
|
-
function getJsonSchema(input) {
|
|
29
|
-
const schema = isSchema(input) ? input.schema : input;
|
|
30
|
-
if (!isJsonSchema(schema)) throw new TypeError(`The provided input is not a valid JSON Schema: ${JSON.stringify(schema, null, 2)}`);
|
|
31
|
-
return schema;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Retrieves the JSON Schema in Object form from a Schema wrapper or returns the input if it's already a JSON Schema.
|
|
35
|
-
*
|
|
36
|
-
* @remarks
|
|
37
|
-
* This function checks if the input is a Schema wrapper (an object with a `schema` property) and returns the `schema` if it is. If the input is already a JSON Schema object, it returns it directly. This allows for flexibility in handling both raw JSON Schema objects and wrapped schemas without needing to check the type at every usage point.
|
|
38
|
-
*
|
|
39
|
-
* @param input - The input which can be either a Schema wrapper or a JSON Schema object.
|
|
40
|
-
* @returns The JSON Schema object.
|
|
41
|
-
* @throws Will throw a TypeError if the input is neither a valid Schema wrapper nor a valid JSON Schema object.
|
|
42
|
-
*/
|
|
43
|
-
function getJsonSchemaObject(input) {
|
|
44
|
-
const schema = getJsonSchema(input);
|
|
45
|
-
if (!isJsonSchemaObject(schema)) throw new TypeError(`The provided input is not a valid JSON Schema object: ${JSON.stringify(schema, null, 2)}`);
|
|
46
|
-
return schema;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Extracts object properties from a JSON Schema object form.
|
|
50
|
-
*
|
|
51
|
-
* @remarks
|
|
52
|
-
* This function returns an empty object if the schema is not an object form or if it has no properties.
|
|
53
|
-
*
|
|
54
|
-
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
55
|
-
* @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.
|
|
56
|
-
*/
|
|
57
|
-
function getProperties(obj) {
|
|
58
|
-
const properties = {};
|
|
59
|
-
const schema = getJsonSchemaObject(obj);
|
|
60
|
-
if (!isSetObject(schema.properties)) return properties;
|
|
61
|
-
for (const [key, value] of Object.entries(schema.properties)) {
|
|
62
|
-
const propertySchema = {};
|
|
63
|
-
if (typeof value !== "boolean") Object.assign(propertySchema, value);
|
|
64
|
-
properties[key] = {
|
|
65
|
-
...propertySchema,
|
|
66
|
-
name: key,
|
|
67
|
-
required: !isPropertyOptional(schema, key),
|
|
68
|
-
default: schema.default?.[key] ?? propertySchema.default
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
return properties;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Returns object properties as an array.
|
|
75
|
-
*
|
|
76
|
-
* @remarks
|
|
77
|
-
* This is a convenience function that extracts properties using `getProperties` and returns them as an array.
|
|
78
|
-
*
|
|
79
|
-
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
80
|
-
* @returns An array of JSON Schema fragments representing the properties, each including metadata.
|
|
81
|
-
*/
|
|
82
|
-
function getPropertiesList(obj) {
|
|
83
|
-
return Object.values(getProperties(obj));
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Adds a property to a JSON Schema object form.
|
|
87
|
-
*
|
|
88
|
-
* @remarks
|
|
89
|
-
* 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.
|
|
90
|
-
*
|
|
91
|
-
* @param obj - The JSON Schema object form or a Schema wrapper to which the property should be added.
|
|
92
|
-
* @param name - The name of the property to add.
|
|
93
|
-
* @param property - The JSON Schema fragment representing the property's schema, including metadata.
|
|
94
|
-
* @throws Will throw an error if the provided schema is not an object form.
|
|
95
|
-
*/
|
|
96
|
-
function addProperty(obj, name, property) {
|
|
97
|
-
const schema = getJsonSchemaObject(obj);
|
|
98
|
-
schema.properties ??= {};
|
|
99
|
-
schema.required ??= [];
|
|
100
|
-
schema.properties[name] = {
|
|
101
|
-
...property,
|
|
102
|
-
name
|
|
103
|
-
};
|
|
104
|
-
if (!schema.required.includes(name)) schema.required.push(name);
|
|
105
|
-
if (schema.required.length === 0) delete schema.required;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Merges multiple JSON Schemas into one.
|
|
109
|
-
*
|
|
110
|
-
* @remarks
|
|
111
|
-
* This function takes multiple JSON Schemas 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.
|
|
112
|
-
*
|
|
113
|
-
* @param schemas - An array of JSON Schemas or Schema wrappers to merge.
|
|
114
|
-
* @returns A new JSON Schema that is the result of merging all input schemas.
|
|
115
|
-
*/
|
|
116
|
-
function merge(...schemas) {
|
|
117
|
-
let result = {};
|
|
118
|
-
for (const schema of schemas.reverse()) if (!result.type || result.type === schema.type) {
|
|
119
|
-
result = defu$1(result, getJsonSchema(schema));
|
|
120
|
-
if (isJsonSchemaObject(result)) result.required = getUnique(result.required ?? []);
|
|
121
|
-
}
|
|
122
|
-
return result;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Returns whether a JSON Schema fragment accepts `null`.
|
|
126
|
-
*
|
|
127
|
-
* @remarks
|
|
128
|
-
* This is true if the schema has `nullable: true` or if its `type` includes `"null"`.
|
|
129
|
-
*
|
|
130
|
-
* @param schema - The JSON Schema fragment to check.
|
|
131
|
-
* @returns `true` if the schema accepts `null`, otherwise `false`.
|
|
132
|
-
*/
|
|
133
|
-
function isSchemaNullable(schema) {
|
|
134
|
-
if (!isSetObject(schema)) return false;
|
|
135
|
-
if (schema.nullable === true) return true;
|
|
136
|
-
return readSchemaTypes(schema).includes("null");
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Returns whether an object property is optional (not listed in `required`).
|
|
140
|
-
*
|
|
141
|
-
* @remarks
|
|
142
|
-
* 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.
|
|
143
|
-
*
|
|
144
|
-
* @param parent - The parent JSON Schema object containing the property.
|
|
145
|
-
* @param propertyName - The name of the property to check for optionality.
|
|
146
|
-
* @returns `true` if the property is optional, otherwise `false`.
|
|
147
|
-
*/
|
|
148
|
-
function isPropertyOptional(parent, propertyName) {
|
|
149
|
-
if (!parent.properties?.[propertyName]) throw new Error(`The property "${propertyName}" does not exist in the parent schema.`);
|
|
150
|
-
return !(parent.required ?? []).includes(propertyName);
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Checks if a given file name has a valid schema input file extension.
|
|
154
|
-
*
|
|
155
|
-
* @param fileName - The file name to check for a valid schema input extension.
|
|
156
|
-
* @returns `true` if the file name has a valid schema input extension, otherwise `false`.
|
|
157
|
-
*/
|
|
158
|
-
function isValidSchemaInputFile(fileName) {
|
|
159
|
-
return VALID_SOURCE_FILE_EXTENSIONS.includes(findFileExtensionSafe(fileName));
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
//#endregion
|
|
163
|
-
export { addProperty, getJsonSchema, getJsonSchemaObject, getProperties, getPropertiesList, isPropertyOptional, isSchemaNullable, isValidSchemaInputFile, merge };
|
|
164
|
-
//# sourceMappingURL=helpers.mjs.map
|