new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Zod · all subjects

schema definitions/objects

16 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

z.object() defines required fields by default

To define an object schema, use z.object() with field definitions. By default, all properties in the object schema are required. To make a property optional, use .optional() on that field schema.

z.object() field optionality

z.object() defines object schemas. All properties are required by default. Make a property optional with .optional(): z.object({ name: z.string(), age: z.number().optional() }). In Zod Mini, wrap the schema: z.object({ name: z.string(), age: z.optional(z.number()) }).

z.object() strips unrecognized keys by default

By default, z.object() strips unrecognized keys from the parsed result. Dog.parse({ name: 'Yeller', extraKey: true }) returns { name: 'Yeller' }.

z.strictObject() throws on unknown keys

z.strictObject() is a strict object schema that throws an error when unknown keys are found in the input.

z.looseObject() allows unknown keys

z.looseObject() is a loose object schema that allows unknown keys to pass through unchanged in the parsed result.

z.object().catchall() validates unknown keys

Use .catchall(schema) to define a schema that validates any unrecognized keys. In Zod: z.object({ name: z.string() }).catchall(z.string()). In Zod Mini: z.catchall(z.object({ name: z.string() }), z.string()).

z.object().shape accesses field schemas

Access internal field schemas via .shape property. Example: Dog.shape.name returns the string schema. In Zod Mini, use .def.shape instead.

z.object().keyof() creates enum from object keys

Creates a ZodEnum from the keys of an object schema. In Zod: Dog.keyof(). In Zod Mini: z.keyof(Dog).

z.object().extend() adds fields to object schema

In Zod: Dog.extend({ breed: z.string() }). In Zod Mini: z.extend(Dog, { breed: z.string() }). If keys overlap, the second schema overrides the first. Alternative: use spread syntax z.object({ ...Dog.shape, breed: z.string() }).

z.object().safeExtend() prevents type narrowing

z.object().safeExtend() works like .extend() but prevents overwriting a property with a non-assignable schema. The result's inferred type extends the original in TypeScript sense. Use when extending schemas containing refinements. In Zod Mini: z.safeExtend().

z.object().pick() and .omit() for key selection

In Zod: Recipe.pick({ title: true }) or Recipe.omit({ id: true }). In Zod Mini: z.pick(Recipe, { title: true }) or z.omit(Recipe, { id: true }).

z.object().partial() makes fields optional

In Zod: Recipe.partial() makes all fields optional, or Recipe.partial({ ingredients: true }) for selected fields. In Zod Mini: z.partial(Recipe) or z.partial(Recipe, { ingredients: true }).

z.object().required() makes fields required

In Zod: Recipe.required() makes all fields required, or Recipe.required({ description: true }) for selected fields. In Zod Mini: z.required(Recipe) or z.required(Recipe, { description: true }).

Recursive object schemas using getters

Define self-referential types using a getter function on object keys. This lets JavaScript resolve the cyclical schema at runtime. Example: z.object({ name: z.string(), get subcategories() { return z.array(Category) } }). Can also represent mutually recursive types with multiple schemas referencing each other.

Recursive schema type annotation workaround

If recursive type inference fails with implicit return type error, add explicit type annotation to the getter: get subactivities(): z.ZodNullable<z.ZodArray<typeof Activity>> { return z.nullable(z.array(Activity)) }.

Passing cyclical data causes infinite loop

Though recursive schemas are supported, passing cyclical data into Zod will cause an infinite loop.

Give your agent this brain