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

@metamorph/valtio-zod

v0.7.5

Published

Validate store values in valtio with zod

Downloads

2

Readme

valtio-zod GitHub Actions Workflow Status NPM Version NPM License

Validate your valtio store values with Zod

What is this

valtio is a proxy state library for ReactJS and VanillaJS.

Zod is a TypeScript-first schema validation library with static type inference.

valtio-zod is an attempt to make validating your store values with zod as easy as possible.

Installation

Zod and valtio are peer dependencies.

npm install zod valtio valtio-zod

Project Status

This project is in early development. We are still trying to iron out details on how best to implement this. All ideas and feedback are welcome and strongly encouraged.

How to use it

import { schema } from 'valtio-zod';

const userSchema = z.object({
  name: z.string(),
  age: z.number(),
});

const userState = schema(userSchema).proxy({
  name: 'John Doe',
  age: 30,
});

state.name = 'Jane Doe';
// state.name = 'Jane Doe'

state.name = 55; // Error
// state.name = 'Jane Doe'

API

Basic

schema(zodSchema)

This function takes a ZodSchema and returns a on object that has a proxy function that returns a tuple of 2 values.

  1. The proxy that can be used to trap the values being set on the valtio store and validate them against the ZodSchema before passing them along.
  2. The underlying valtio store. This object is what must be used when using useSnapshot() to access the values in the store.
import { schema } from 'valtio-zod';

const userSchema = z.object({
  name: z.string(),
  age: z.number(),
});

const userState = schema(userSchema).proxy({
  name: 'John Doe',
  age: 30,
});

// state is the proxy, snap is the underlying valtio store. You must use the snap object
// when you use useSnapshot()

useSnapshot(state)

This function takes a store object created by schema.proxy() and returns the useSnapshot function from valtio attached to the underlying valtio store.

import { schema, useSnapshot } from 'valtio-zod';

const userSchema = z.object({
  name: z.string(),
  age: z.number(),
});

const userState = schema(userSchema).proxy({
  name: 'John Doe',
  age: 30,
});

//... inside a react component
const snap = useSnapshot(userState);
//...

Configuration

These properties can be passed to the proxy function to configure how each schema is handled. parseSafe and errorHandler are also available as properties on the vzGlobalConfig object to configure the default behavior of all schemas that don't provide their own configuration.

| property | type | description | | -------------- | ---------------------- | ------------------------------------------------------------------ | | parseSafe | boolean | Tells zod whether or not to throw an error when a value is invalid | | errorHandler | (error: any) => void | A function that is called when a value is invalid |

parseSafe example

import { schema } from 'valtio-zod';

const userSchema = z.object({
  name: z.string(),
});

const userState = schema(userSchema).proxy(
  {
    name: 'John Doe',
    age: 30,
  },
  {
    parseSafe: true,
  },
);

userState.name = 'Jane Doe';
// userState.name = 'Jane Doe'

userState.name = 55; // Error
// this will call the errorHandler function without throwing an error
// userState.name = 'Jane Doe'

parseSafe is also available as a property on the vzGlobalConfig object

import { vzGlobalConfig } from 'valtio-zod';

vzGlobalConfig.parseSafe = true;

errorHandler example

This will allow you to use your own error handling logic (or use Zod.Error)

import { schema, errorHandler } from 'valtio-zod';

const userSchema = z.object({
  name: z.string(),
  age: z.number(),
});

const userState = schema(userSchema).proxy(
  {
    name: 'John Doe',
    age: 30,
  },
  {
    errorHandler: (error) => {
      console.error(error);
    },
  },
);