Field-level validation
Validate one property independently without parsing the complete object.
Zod · Module
.shape exposes the individual field schemas inside a Zod object schema. Use it to retrieve a validator for a specific key, such as Dog.shape.name.
For a Zod object schema, .shape exposes the schema assigned to each declared key. Access a property such as Dog.shape.name to retrieve that field's validator and use it independently or inspect its definition.
objectSchema.shape.keyThe schema associated with the selected object key.
import { z } from "zod";
const Dog = z.object({
name: z.string(),
age: z.number(),
});
const nameSchema = Dog.shape.name;
const ageSchema = Dog.shape.age;
console.log(nameSchema.parse("Rex"));
console.log(ageSchema.parse(4));The shape exposes each field's schema, which can then be inspected or used independently for validation.
Validate one property independently without parsing the complete object.
Inspect or reuse a field validator while building related schemas.
The .shape property provides access to the individual schemas inside a Zod object schema. For example, Dog.shape.name returns the schema used to validate the name field.
Follow-up: How would you validate only the name field? Retrieve Dog.shape.name and call its parse or safeParse method with the candidate value.
It lets code address a field schema by its key, such as Dog.shape.age. The result is a Zod schema for that field rather than the field's value.
Follow-up: What happens if the key is not declared in the object shape? TypeScript generally reports an invalid property access, while dynamically computed keys require appropriate type handling.
A field schema obtained through .shape can be reused for focused validation or further schema operations. The documentation illustrates accessing the internal schemas for named object fields.
Follow-up: How could this support schema composition? Use the retrieved field schema as an input to another Zod schema or composition method, depending on the desired structure.
z.objectz.stringz.numberparsesafeParse