Skip to content

Commit de65a5c

Browse files
authored
docs: lead the properties section with the check and add a Zod Mini tab (#6598)
z.properties() is a check again since #6594, so the section now opens with the spread form both Zod and Zod Mini have, in a tab pair. The z.instanceof() method follows as the Zod-only sugar that narrows the inferred type. Also states that z.instanceof() returns the exact object passed in. The no-clone fact moves up to the Instanceof intro, where it covers every instance schema rather than just the properties check.
1 parent f1448f7 commit de65a5c

1 file changed

Lines changed: 43 additions & 9 deletions

File tree

‎packages/docs/content/api.mdx‎

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,14 @@ z.instanceof(URL);
23242324
z.instanceof(Error);
23252325
```
23262326

2327+
Parsing returns the exact object that was passed in. Unlike `z.object()`, nothing is cloned, so the prototype, the unlisted properties, and the object identity all survive.
2328+
2329+
```ts
2330+
const url = new URL("https://example.com");
2331+
2332+
z.instanceof(URL).parse(url) === url; // true
2333+
```
2334+
23272335
### `z.property()` [#property]
23282336

23292337
To validate a particular property of a class instance against a Zod schema:
@@ -2348,27 +2356,53 @@ blobSchema.parse("hello there!"); // ✅
23482356
blobSchema.parse("hello."); // ❌
23492357
```
23502358

2351-
### `.properties()`
2359+
### `z.properties()` [#properties]
23522360

2353-
Use `.properties()` to check several properties at once. Each call narrows the inferred type.
2361+
To check several properties at once:
2362+
2363+
<Tabs groupId="lib" items={["Zod", "Zod Mini"]}>
2364+
<Tab value="Zod">
2365+
```ts
2366+
const okResponse = z.instanceof(Response).check(
2367+
...z.properties({
2368+
status: z.number().min(200).max(299),
2369+
redirected: z.literal(false),
2370+
})
2371+
);
2372+
2373+
okResponse.parse(new Response("ok")); // ✅
2374+
okResponse.parse(new Response("", { status: 404 })); // ❌ status
2375+
```
2376+
</Tab>
2377+
<Tab value="Zod Mini">
2378+
```ts
2379+
const okResponse = z.instanceof(Response).check(
2380+
...z.properties({
2381+
status: z.number().check(z.minimum(200), z.maximum(299)),
2382+
redirected: z.literal(false),
2383+
})
2384+
);
2385+
2386+
okResponse.parse(new Response("ok")); // ✅
2387+
okResponse.parse(new Response("", { status: 404 })); // ❌ status
2388+
```
2389+
</Tab>
2390+
</Tabs>
2391+
2392+
Each property is asserted in place. The schemas in the shape are validated but their results are discarded, so a transform or a default never changes the parsed value.
2393+
2394+
In Zod, `z.instanceof()` also has a `.properties()` method. It spreads the same check and narrows the inferred type:
23542395

23552396
```ts
23562397
const okResponse = z.instanceof(Response).properties({
23572398
status: z.number().min(200).max(299),
23582399
redirected: z.literal(false),
23592400
});
23602401

2361-
okResponse.parse(new Response("ok")); // ✅
2362-
okResponse.parse(new Response("", { status: 404 })); // ❌ status
2363-
23642402
type OkResponse = z.infer<typeof okResponse>;
23652403
// => Response & { status: number; redirected: false }
23662404
```
23672405

2368-
The check asserts each property in place. Unlike `z.object()`, it never builds a new object, so prototypes, unlisted keys, and object identity survive. Schemas inside the shape are validated but their results are discarded, so a transform or a default never changes the parsed value.
2369-
2370-
The underlying `z.properties()` is a check, so it spreads into `.check()` too: `z.instanceof(Response).check(...z.properties({ ... }))`. It asserts on whatever the base schema produced, so `z.string().check(...z.properties({ length: z.number().min(3) }))` reads a string's length.
2371-
23722406
## Refinements
23732407

23742408
Every Zod schema stores an array of *refinements*. Refinements are a way to perform custom validation that Zod doesn't provide a native API for.

0 commit comments

Comments
 (0)