Skip to content

Function: apiObject() ​

ts
function apiObject<Shape, Prefix>(shape, additionalKeyPrefix?): ZodObject<Shape, ApiObjectConfig<Prefix>>;

Defined in: src/core/apiObject.ts:26

Builds an object schema for an API response.

Loose by default: keys the API sends but the spec does not document pass straight through, so a field added upstream never breaks a consumer between releases.

Under AUDIT_SCHEMAS=true it builds strict objects instead, which is how the audit run learns where an undocumented key sits — zod reports unrecognized_keys with a path, and nothing else in the pipeline knows the shape well enough to say. Those failures do not reach the caller: createClient records them and hands back the response anyway, so one stale schema cannot cut the audit short. Loose validation cannot report drift at all, since accepting anything extra is precisely what it is for.

The declared type is loose on the way out and exact on the way in: a model read from a response keeps the keys it does not describe, while a model written into a request names only its own, so a misspelt key is a compile error. It stays that in both modes, deliberately. The switch is read at runtime, so the compiler cannot follow it, and the published declarations must not shift with an environment variable.

Type Parameters ​

Type ParameterDefault type
Shape extends Readonly<{ [k: string]: $ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>; }>-
Prefix extends string | undefinedundefined

Parameters ​

ParameterType
shapeShape
additionalKeyPrefix?Prefix

Returns ​

ZodObject<Shape, ApiObjectConfig<Prefix>>