/** * Response Envelope Schema Validator * * Zero-dependency runtime validator for provider response shapes. Used at the * provider boundary to detect when an upstream API silently changes its * response envelope — the classic "field renamed at 2am, parser throws at * 3am" failure mode. * * Philosophy: validate the *minimum* fields each provider's parser actually * reads. Don't re-type the full upstream schema — that creates brittle * over-specification that churns every time the provider adds an optional * field. Only the fields we touch matter. * * Usage: * validateSchema('anthropic', data, [ * { path: 'content', type: 'array' }, * { path: 'usage.input_tokens', type: 'number' }, * { path: 'usage.output_tokens', type: 'number' }, * { path: 'model', type: 'string' }, * ]); * * On first failure, throws SchemaDriftError. The caller (typically the * factory) catches it, fires the onSchemaDrift hook, and falls over to * another provider. */ export type SchemaFieldType = 'string' | 'number' | 'boolean' | 'array' | 'object' | 'string-or-null' | 'string-or-number'; export interface SchemaField { /** * Dot-separated path into the response object. Array indices not supported * in the path itself — use `items` to validate array element shapes. */ path: string; type: SchemaFieldType; /** * If true, missing paths are allowed and skipped. Useful for fields that * are genuinely optional (e.g. stop_sequence on Anthropic). */ optional?: boolean; /** * For type: 'array' — validate each element against a nested schema. * * - `shape`: a flat SchemaField[] applied to every element (all elements * the same shape). * - `variants` + `discriminator`: discriminated union. Each element is * routed by the value of `discriminator` (a field name on the element) * to the matching variant schema. **Unknown discriminator values are * allowed and skipped** — we want forward-compat for additive API * changes (e.g. Anthropic adds a new content block type). Only *missing* * discriminators or *wrong-typed* known variants trigger drift. */ items?: { shape?: SchemaField[]; discriminator?: string; variants?: Record; }; } /** * Validate a response envelope against a minimal field schema. * Throws SchemaDriftError on the first mismatch, with provider + path + * expected/actual types surfaced for observability. * * We fail fast rather than collecting all errors: the first drift is enough * to trigger fallback, and walking the whole schema when we're already * broken wastes budget. */ export declare function validateSchema(provider: string, data: unknown, fields: SchemaField[]): void; //# sourceMappingURL=schema-validator.d.ts.map