npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zod-refine

v1.1.1

Published

An adapter that allows using Zod schemas as Recoil Sync validators instead of Refine.

Downloads

225

Readme

zod-refine Github CI NPM version

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.

Why?

Recoil Sync provides its own type-refinement/validator library, called Refine, however Zod provides better TypeScript integration and more features, so there's that.

Installation

npm i zod-refine
yarn add zod-refine

Usage

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()),
    }),
  ],
});

Advanced Usage

Upgrading an atom's type

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),
        ])
      ),
    }),
  ],
});

API Reference

getRefineCheckerForZodSchema()

export function getRefineCheckerForZodSchema<S extends Schema>(
  schema: S
): Checker<z.infer<S>>;

Known problems

Error handling

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.

License

MIT