Skip to content

Commit

Permalink
Update documentation about working with ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
Haaxor1689 committed Nov 26, 2023
1 parent 76c77bc commit 0392892
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,24 @@ type MySchema = z.output<typeof MySchema>;
MySchema.toBuffer([1, 2, 3, 4]);
```

You can also access the current context when creating transformations to reference other attributes from the parent type (if any). The easiest way to do this is by using the `resolvePath` helper function.

```ts
const MySchema = n.object({
hasAlpha: n.bool(),
data: n.array(n.int8(), 'fill').transform(
(v, ctx) => {
n.resolvePath(['hasAlpha'], ctx); // will hold value of `hasAlpha` attribute from parent object
return v;
},
(v, ctx) => {
n.resolvePath(['hasAlpha'], ctx); // will hold value of `hasAlpha` attribute from parent object
return v;
}
)
});
```

### `.fromBuffer`

`.fromBuffer(data: Uint8Array): Output`
Expand Down

0 comments on commit 0392892

Please sign in to comment.