Skip to content

Commit

Permalink
Add documentation for Object.shape()
Browse files Browse the repository at this point in the history
  • Loading branch information
pierreis committed Apr 26, 2020
1 parent 99f4ff0 commit 0fb1615
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Reference Types

- [object](#object)
- [pick/omit/partial](#object.pick/omit/partial)
- [shape](#object.shape)
- [record](#record)
- [array](#array)
- [tuple](#tuple)
Expand Down Expand Up @@ -392,11 +393,9 @@ emptyObjSchema.parse({ key: 'value' }); // => succeeds
strictEmptyObjSchema.parse({ key: 'value' }); // => throws ValidationError because not expected key: "key"

const personSchema = myzod.object({
name: myzod.string().min(2),
age: myzod.number({ min: 0 }).nullable(),
name: myzod.string(),
});

type Person = Infer<typeof personSchema>; // => { name: string; age: number | null }
const shape = personSchema.shape(); // => returns { name: myzod.string() }
```

#### object.withPredicate
Expand All @@ -413,6 +412,18 @@ const registrationSchema = myzod
.withPredicate(value => value.password === value.confirmedPassword, 'password and confirmed do not match');
```

#### object.shape

You can extract the shape from an ObjectType.

```typescript
const registrationSchema = myzod
.object({
email: z.string()
})
.withPredicate(value => value.password === value.confirmedPassword, 'password and confirmed do not match');
```

#### object.pick/omit/partial

The Object type has utility methods pick, omit, and partial for creating new ObjectType schemas based on the current instance. Note once more that predicates do not carry over from base schema.
Expand Down

0 comments on commit 0fb1615

Please sign in to comment.