You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use `zx.fold` to define a recursive traversal of a zod schema. Useful when building a schema rewriter.
1894
+
Use `zx.fold` to define a recursive traversal of a Zod schema. Useful when building a schema rewriter.
1895
1895
1896
1896
`zx.fold` is a powertool. Most of `@traversable/zod` uses `zx.fold` under the hood.
1897
1897
1898
-
Compared to the rest of the library, it's fairly "low-level", so unless you're doing something pretty advanced you probably won't need to use it directly.
1898
+
Compared to the rest of the library, it's fairly "low-level", so unless you're doing something more advanced you probably won't need to use it directly.
1899
1899
1900
-
#### Example
1900
+
#### Examples
1901
+
1902
+
1. Example: Custom schema rewriter
1903
+
1904
+
Let's write a schema rewriter that takes an arbitrary Zod schema, and applies a custom transformation to only `z.string` schemas. For this contrived example, we'll be converting string values to uppercase.
1905
+
1906
+
> [!NOTE]
1907
+
>
1908
+
> You can play with this example on [StackBlitz](https://stackblitz.com/edit/traversable-zod-fold-example?file=test%2Fexample.test.ts&initialPath=__vitest__/)
> Notice the use of `z.clone`: this is only necessary when your target is __also__ a Zod schema. This is to ensure that none of the schema's class properties are lost in the traversal.
1940
+
1941
+
Thanks to [@Refzlund](https://github.com/Refzlund) for suggesting that we add this example to the docs!
1942
+
1943
+
1944
+
2. Example: Mock data generator
1901
1945
1902
-
Let's write a function that takes an arbitrary zod schema, and generates mock data that satisfies the schema (a.k.a. a "faker").
1946
+
Let's write a function that takes an arbitrary Zod schema, and generates mock data that satisfies the schema (a.k.a. a "faker").
1903
1947
1904
1948
> [!NOTE]
1905
1949
> You can play with this example on [StackBlitz](https://stackblitz.com/edit/traversable-zod-faker-example?file=test%2Ffake.test.ts,src%2Ffake.ts&initialPath=__vitest__/)
0 commit comments