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

@lukasniessenesentri/react-zod-form

v1.0.17

Published

Performant, flexible and extensible forms library for React Hooks using Zod

Downloads

5

Readme

Form validation with Zod: ZERO boilerplate code.

Unfinished test version.


Installation: npm i @lukasniessenesentri/react-zod-form

Uses monkey patching for defining attributes on all form related elements and thereby allows writing form validaiton with zero-boilerplate code.


Properties you can add:

  • ZodSchema: zod schema that must pass
  • Required: Must be non empty, trims the string for check
  • IsValid: Custom validation function (input: string) -> boolean

How it works: onSubmit method you specified is only called if all input fields pass all properties you assigned to it. If none specified, it passes always. If multiple on one input, all of them must pass. If one does not pass, "onError" is called. All inputs that fail have their onErrors called, not only the first fail.

But: For multiple on one input, only the first one will trigger the error message to be shown. --> Order of checking: Required 1st, zodSchema 2nd, isValid 3rd


Working example:


import React, { useState } from 'react';
import { ZodForm } from 'react-zod-form';
import { z } from 'zod';
import { Input as MuiInput } from '@mui/material';
import { Input as ChakraInput } from '@chakra-ui/react'

export const VanillaForm = () => {

  const mailSchema = z.string().email("Invalid email");
  const min10 = z.string().min(10, "Too short");
  const min20 = z.string().min(20, "Too short bro");

  const [errorName, setErrorName] = useState("");
  const [errorMail, setErrorMail] = useState("");
  const [errorSuperLong, setErrorSuperLong] = useState("");
  const [errorRnd4, setErrorRnd4] = useState("");
  const [success, setSuccess] = useState("");

  function onErrorMail(msg: string | null) {
    if (msg) {
      setErrorMail(msg);
    } else {
      setErrorMail("");
    }
  }

  function onErrorName(msg: string | null) {
    if (msg) {
      setErrorName(msg);
    } else {
      setErrorName("");
    }
  }

  function onErrorLong(msg: string | null) {
    if (msg) {
      setErrorSuperLong(msg);
    } else {
      setErrorSuperLong("");
    }
  }

  function onErrorRnd4(msg: string | null) {
    if (msg) {
        setErrorRnd4(msg);
    } else {
        setErrorRnd4("");
    }
  }

  function handleSubmit(formData: any) {
    setSuccess(JSON.stringify(formData));
    console.log(formData);
  }

  function customValidator(input: string) {
    return {
        success: input.includes("cool"),
        errorMessage: "Must include word 'cool'"
    };
  }

    return (
        <>
            <ZodForm onSubmit={handleSubmit}>

                <MuiInput 
                    name="name1" 
                    placeholder="Name..."
                    ZodSchema={min10} 
                    HandleError={onErrorName}
                />
                <p>{errorName}</p>

                <ChakraInput 
                    name="name99" 
                    placeholder="chakra name..."
                    ZodSchema={min10} 
                    HandleError={onErrorName}
                />
                <p>{errorName}</p>

                <input 
                    name="mail2" 
                    placeholder="Email bruva..."
                    ZodSchema={mailSchema}
                    HandleError={onErrorMail}
                />
                <p>{errorMail}</p>

                <textarea 
                    name="long" 
                    placeholder="Longer..."
                    ZodSchema={min20}
                    HandleError={onErrorLong}
                />
                <p>{errorSuperLong}</p>

                <input 
                    name="rnd4" 
                    placeholder="Random things man..."
                    IsRequired="value required"
                    IsValid={customValidator}
                    HandleError={onErrorRnd4}
                />
                <p>{errorRnd4}</p>

            </ZodForm>

            {success && (
                <>
                    <h1>Passed:</h1>
                    <p>
                        {success}
                    </p>
                </>
            )}
        </>
    );
};

Note: our monkey patched attribute all start with Capital letters. That is to avoid confusion and collusion with other libraries. It marks them and avoids problems.