import * as z from "zod"; export { z }; /** Shared options bag mirroring TypeBox's `SchemaOptions`, used by all schema helpers in this module. */ export type SchemaOptions = { title?: string; description?: string; default?: unknown; error?: string; errorMessage?: string; excludeFromApiSpecGeneration?: boolean; }; /** Resolves the validation error message from either `error` or `errorMessage` option. */ export const errorMessage = (options?: SchemaOptions) => options?.error ?? options?.errorMessage; /** Attaches non-undefined {@link SchemaOptions} fields to a schema via `.meta()`. */ export function withMetadata( schema: T, options?: SchemaOptions, ): T { if (!options) return schema; const { error: _, ...metadata } = { ...options, errorMessage: errorMessage(options), }; return schema.meta(metadata); } /** Applies {@link SchemaOptions} metadata to an object schema. */ export function withObjectOptions( schema: T, options?: SchemaOptions, ): T { return withMetadata(schema, options); } /** Drop-in replacement for `z.object()` that accepts {@link SchemaOptions} for metadata and validation. */ export function object( shape: T, options?: SchemaOptions, ) { return withObjectOptions(z.object(shape), options); } /** Like {@link object} but uses `z.strictObject()` — rejects unknown keys. Marks the schema with `x-strictObject` metadata for the OpenRPC generator. */ export function strictObject( shape: T, options?: SchemaOptions, ) { return withObjectOptions(z.strictObject(shape), options).meta({ "x-strictObject": true, }); } /** Drop-in replacement for `z.union()` with options-first arg order (matches TypeBox's `Type.Union`). */ export function union< T extends readonly [z.ZodType, z.ZodType, ...z.ZodType[]], >(options: SchemaOptions | undefined, schemas: T) { return withMetadata( z.union(schemas, { error: errorMessage(options) }), options, ); }