import { JSONSchema, FromSchema as FromSchema$1 } from 'json-schema-to-ts'; import zod from 'zod'; /** * A minimal JSON schema type. * * This type is used to narrow the type of a JSON schema to a minimal type * that is compatible with the `json-schema-to-ts` library. */ type JsonSchemaMinimal = { type: 'object'; } | { anyOf: unknown[]; } | { allOf: unknown[]; } | { oneOf: unknown[]; }; /** * A JSON schema */ type JsonSchema = Exclude & JsonSchemaMinimal; /** * Infer the data type of a JsonSchema. * * @param T - The `JsonSchema` to infer the data type of. * @param Options - Configuration options for the type inference. The `validated` flag determines whether the schema has been validated. If `validated` is true, all properties are required unless specified otherwise. If false, properties with default values are optional. * * @returns The inferred type. * * @example * ```ts * const mySchema = { * type: 'object', * properties: { * name: { type: 'string' }, * email: { type: 'string' }, * }, * required: ['name'], * additionalProperties: false, * } as const satisfies JsonSchema; * * // has type { name: string, email?: string } * type MySchema = InferJsonSchema; * ``` */ type InferJsonSchema = T extends JsonSchemaMinimal ? T extends JSONSchema ? Options['validated'] extends true ? FromSchema$1 : FromSchema$1 : never : never; /** * A ZodSchema used to validate a JSON object. */ type ZodSchema = zod.ZodType, zod.ZodTypeDef, Record>; /** * A minimal ZodSchema type. * * It is necessary to define a minimal ZodSchema type to enable correct inference * when Zod is not available, as Typescript doesn't support detection of module * availability via `typeof import('zod')`. */ type ZodSchemaMinimal = { readonly safeParseAsync: unknown; }; /** * Infer the data type of a ZodSchema. * * @param T - The ZodSchema to infer the data type of. * @param Options - Configuration options for the type inference. The `validated` flag determines whether the schema has been validated. If `validated` is true, all properties are required unless specified otherwise. If false, properties with default values are optional. * * @example * ```ts * const mySchema = z.object({ * name: z.string(), * email: z.string().optional(), * }); * * // has type { name: string, email?: string } * type MySchema = InferZodSchema; * ``` */ type InferZodSchema = T extends ZodSchemaMinimal ? T extends { readonly _output: infer Output extends Record; readonly _input: infer Input extends Record; } ? Options['validated'] extends true ? Output : Input : never : never; /** * A schema used to validate a JSON object. */ type Schema = JsonSchemaMinimal | ZodSchemaMinimal; /** * Main utility type for schema inference * * @param T - The Schema to infer the type of. * @param Options - Configuration options for the type inference. The `validated` flag determines whether the schema has been validated. If `validated` is true, all properties are required unless specified otherwise. If false, properties with default values are optional. */ type InferSchema = InferJsonSchema | InferZodSchema; /** * Infer the type of a Schema for unvalidated data. * * The resulting type has default properties set to optional, * reflecting the fact that the data is unvalidated and has * not had default properties set. * * @example * ```ts * type MySchema = FromSchemaUnvalidated; * ``` */ type FromSchemaUnvalidated = InferSchema; /** * Infer the type of a Schema for validated data. * * The resulting type has default properties set to required, * reflecting the fact that the data has been validated and * default properties have been set. * * @example * ```ts * type MySchema = FromSchema; * ``` */ type FromSchema = InferSchema; export type { FromSchemaUnvalidated as F, JsonSchema as J, Schema as S, ZodSchema as Z, FromSchema as a, ZodSchemaMinimal as b };