/** * @file Shared types for schema validation. `Schema` is the Zod-shaped * duck-type contract — any validator with a `.safeParse(data)` method * returning `{ success, data?, error? }` satisfies it. socket-lib detects Zod * (v3 and v4) structurally via this interface; consumers bring their own Zod. * `ValidateResult` / `ValidationIssue` / `Infer` / `AnySchema` are the * normalized shapes produced by `@socketsecurity/lib/schema/validate` and * `@socketsecurity/lib/schema/parse`. */ /** * Result of a Zod-shaped schema's `.safeParse()` call. * * @template T - The expected type of the parsed data. */ export interface ParseResult { /** * Indicates whether parsing was successful. */ success: boolean; /** * Parsed and validated data (only present when `success` is `true`) */ data?: T | undefined; /** * Error information (only present when `success` is `false`) */ error?: unknown | undefined; } /** * Zod-shaped duck-type for any validator exposing `safeParse` / `parse`. * * @example * ;```ts * import { z } from 'zod' * * const userSchema = z.object({ name: z.string(), age: z.number() }) * * // Schema satisfies this interface * const schema: Schema = userSchema * const result = schema.safeParse({ name: 'Alice', age: 30 }) * ``` * * @template T - The expected output type after validation. */ export interface Schema { /** * Non-throwing parse. */ safeParse(data: unknown): ParseResult; /** * Throwing parse. */ parse(data: unknown): T; /** * Optional schema name for debugging. */ _name?: string | undefined; } /** * Internal structural shape of a Zod v4 schema — carries the inferred output * type on `_zod.output`. Used for type-only detection in `Infer`. * * @internal */ export interface ZodV4LikeSchema { _zod: { output: O; }; safeParse(data: unknown): unknown; } /** * Internal structural shape of a Zod v3 schema — carries the inferred output * type on `_output`. Used for type-only detection in `Infer`. * * @internal */ export interface ZodV3LikeSchema { _output: O; safeParse(data: unknown): unknown; } /** * Internal structural shape of a TypeBox `TSchema` — carries the inferred * output type on the phantom `static` field. Only used inside socket-lib for * type-only detection in `Infer`; external callers should pass Zod schemas. * * @internal */ export interface TypeBoxLikeSchema { static: unknown; } /** * Any schema kind the validators accept. */ export type AnySchema = ZodV4LikeSchema | ZodV3LikeSchema | TypeBoxLikeSchema | Schema; /** * Infer the validated output type from any supported schema kind. * * Order matters: TypeBox schemas carry a phantom `static` field, so we check * for TypeBox before falling through to Zod and the duck-type. */ export type Infer = S extends { static: infer Static; } ? Static : S extends { _zod: { output: infer O; }; } ? O : S extends { _output: infer O; } ? O : S extends Schema ? T : unknown; /** * A single normalized validation error. */ export interface ValidationIssue { /** * Array path into the value (e.g. `['user', 'age']`). */ path: Array; /** * Human-readable description of the failure. */ message: string; } /** * Tagged-union result of `validateSchema`. Callers narrow on `ok`. */ export type ValidateResult = { ok: true; value: T; } | { ok: false; errors: ValidationIssue[]; };