Accept arbitrary input
Validating untyped or heterogeneous external data when no runtime value restriction is appropriate.
Zod · Module
z.any() defines a schema that accepts values of any type and infers any in TypeScript. As an object property, it requires the key unless combined with .optional().
z.any() creates a schema whose inferred TypeScript type is any and whose validation accepts arbitrary input values. When used as an object property, the property remains required; call .optional() when the property itself may be absent.
z.any()A Zod schema accepting any value and inferring the TypeScript type any.
z.any() accepts arbitrary input values rather than restricting them to a specific primitive, object, or collection type.any, so downstream static checking does not narrow the accepted value.z.any() differs from the related z.unknown() schema: the former infers any, while the latter infers unknown.z.any() property is required when nested in an object; parsing an object that omits the key fails.undefined; presence of the key, rather than its value's type, satisfies the property..optional() changes the object-property behavior so the key may be omitted.z.any() does not make an object key optional. An object must contain the key, even when its value is undefined; use .optional() to allow omission.import { z } from "zod";
const Payload = z.object({
value: z.any(),
optionalValue: z.any().optional(),
});
Payload.parse({ value: undefined });
Payload.parse({ value: { source: "external" } });
Payload.parse({ value: 42, optionalValue: undefined });z.any() accepts values of any runtime type. In an object schema, the property is still required unless .optional() is added.
Validating untyped or heterogeneous external data when no runtime value restriction is appropriate.
Representing an object field that may contain any value, including undefined, while still requiring the field to exist.
Allowing an object field to be absent as well as unrestricted when present.
z.any() validate and infer?z.any() is Zod's schema for arbitrary values, and its inferred TypeScript type is any. It accepts values without restricting their runtime type.
Follow-up: How do you allow an object property using z.any() to be omitted? Use z.any().optional(), because the base schema still requires the property key.
z.any() make an object property optional?In an object schema, z.any() makes the property key required. The value may be undefined, but the key itself must be present.
Follow-up: What input distinguishes the two forms? { a: undefined } satisfies z.object({ a: z.any() }), while {} does not; adding .optional() makes {} valid.
Use z.any().optional() when both arbitrary values and an absent property are allowed. The documentation contrasts this with plain z.any(), which requires the key.
Follow-up: What TypeScript shape does this correspond to? The optional form corresponds to a property that may be omitted, unlike the required { a: any } shape.
z.unknown()z.object()z.optional()