brncsk/zod-refine(opens in a new tab)
Language: TypeScript
An adapter that allows using Zod schemas as Recoil Sync validators instead of Refine.
- stars
- 4
- watchers
- 1
- forks
- 0
- open issues
- 0
- Last updated
- 08/20/2022
Language: TypeScript
An adapter that allows using Zod schemas as Recoil Sync validators instead of Refine.
zod-refine is an adapter library that lets you use
Zod schemas for validating atom values in
Recoil Sync effects.
Recoil Sync is an add-on library for Recoil, Meta's fairly new-ish state management library for React.
Recoil Sync provides its own type-refinement/validator library, called Refine, however Zod provides better TypeScript integration and more features, so there's that.
npm i zod-refine yarn add zod-refine
zod-refine's sole export is a function named getRefineCheckerForZodSchema().
It takes a single Zod.Schema
(a.k.a. ZodType)
and returns the associated
Refine Checker.
The following is an introductory example on checking a Recoil
atom's value in a Recoil Sync
syncEffect.
Using Refine:
import { atom } from 'recoil'; import { syncEffect, refine } from 'recoil-sync'; const testAtom = atom<number>({ key: 'test', default: 0, key: 'test', effects: [ syncEffect({ refine: refine.number(), }), ], });
Using zod-refine:
import { atom } from 'recoil'; import { syncEffect } from 'recoil-sync'; import { z } from 'zod'; import { getRefineCheckerForZodSchema } from 'zod-refine'; const testAtom = atom<number>({ key: 'test', default: 0, effects: [ syncEffect({ refine: getRefineCheckerForZodSchema(z.number()), }), ], });
One can
upgrade an atom's type
using Refine's match() and asType() checkers:
const myAtom = atom<number>({ key: 'MyAtom', default: 0, effects: [ syncEffect({ refine: match( number(), asType(string(), x => parseInt(x)), asType(object({ value: number() }), x => x.value) ), }), ], });
This idiom can be adapted to Zod using z.union() / transform():
const myAtom = atom<number>({ key: 'MyAtom', default: 0, effects: [ syncEffect({ refine: getRefineCheckerForZodSchema( z.union([ z.number(), z.string().transform(x => parseInt(x)), z.object({ value: z.number() }).transform(x => x.value), ]) ), }), ], });
export function getRefineCheckerForZodSchema<S extends Schema>( schema: S ): Checker<z.infer<S>>;
Refine can only handle a single error when reporting validation problems.
When Zod reports multiple issues, zod-refine only passes the first one to
Refine.
MIT