Skip to content
On this page

Farfetched and Zod

Integration is distributed as a separate package, you have to install it and its peer dependencies before usage

sh
pnpm install zod @farfetched/zod
sh
yarn add zod @farfetched/zod
sh
npm install zod @farfetched/zod

zodContract

Creates a Contract based on given ZodType.

ts
import { z as zod } from 'zod';
import { zodContract } from '@farfetched/zod';

const Asteroid = zod.object({
  type: zod.literal('asteroid'),
  mass: zod.number(),
});

const asteroidContract = zodContract(Asteroid);

/* typeof asteroidContract === Contract<
 *   unknown, 👈 it accepts something unknown
 *   { type: 'asteriod', mass: number }, 👈 and validates if it is an asteroid
 * >
 */

Released under the MIT License.