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 πŸ™

Β© 2025 – Pkg Stats / Ryan Hefner

@coxy/react-validator

v5.0.1

Published

πŸš€ Simple validation form for React or NodeJS apps with zod. useValidator are included ;)

Downloads

697

Readme

@Coxy/react-validator


Simple React Form Validation

@Coxy/react-validator provides a simple and flexible way to validate forms in React.

Supports data validation both in-browser and on Node.js.

Requires React >=16.3.


Installation

npm install @coxy/react-validator
# or
yarn add @coxy/react-validator

Quick Start Example

import React, { useState } from 'react';
import { ValidatorWrapper, rules, ValidatorField } from '@coxy/react-validator';

const validator = React.createRef();

const handleSubmit = () => {
  const { isValid, message, errors } = validator.current.validate();
  if (!isValid) {
    console.log(isValid, message, errors);
    return;
  }
  alert('Form is valid!');
};

export default function Example() {
  const [email, setEmail] = useState('');

  return (
    <ValidatorWrapper ref={validator}>
      <ValidatorField value={email} rules={rules.email}>
        {({ isValid, message }) => (
          <>
            <input value={email} onChange={({ target: { value } }) => setEmail(value)} />
            {!isValid && <div style={{ color: 'red' }}>{message}</div>}
          </>
        )}
      </ValidatorField>

      <button onClick={handleSubmit} type="button">
        Submit
      </button>
    </ValidatorWrapper>
  );
}

More examples available here.


Built-in Validation Rules

Create your own rules easily, or use the built-in ones:

const rules = {
  notEmpty: [z.string().min(1, { error: 'Field is required' })],
  isTrue: [z.boolean({ error: 'Value is required' }).and(z.literal(true))],
  email: [z.string().min(1, { error: 'Email is required' }), z.email({ message: 'Email is invalid' })],
}

| Name | Type | Description | |----------|-------|--------------------------------------------| | email | Array | Validate non-empty email with regex check. | | notEmpty | Array | Check if a string is not empty. | | isTrue | Array | Ensure a boolean value is present. |


React API

<ValidatorWrapper /> Props

| Name | Default | Required | Description | |------------------|---------|----------|----------------------------------------------------| | ref | null | No | React ref or useRef for control. | | stopAtFirstError | false | No | Stop validating after the first error. |

<ValidatorField /> Props

| Name | Default | Required | Description | |----------|-----------|----------|---------------------------------------| | value | undefined | Yes | Value to validate. | | rules | [] | Yes | Validation rules array. | | required | true | No | Whether the field is required. | | id | null | No | ID for the field (for manual access). |

useValidator

Validate a value directly in your component:

import { useValidator, rules } from '@coxy/react-validator';

const [isValid, { errors }] = useValidator('[email protected]', rules.email);

console.log(isValid); // true or false
console.log(errors);  // array of error messages

Validator Class (Node.js / Manual Usage)

Use it server-side or in custom flows.

Validator Constructor Options

| Name | Default | Required | Description | |------------------|---------|----------|------------------------------------------------------| | stopAtFirstError | false | No | Stop validating after first error across all fields. |

Methods

.addField()

const validator = new Validator({ stopAtFirstError: true });

const field = validator.addField({
  rules: rules.email,
  value: '12345',
});

.getField(id)

const field = validator.getField('password-field');
console.log(field);

.removeField(field)

validator.removeField(field);

.validate()

const { isValid, message, errors } = validator.validate();
console.log(isValid, errors);

Full Server-Side Example

import { Validator, rules } from '@coxy/react-validator';

const validator = new Validator();

validator.addField({
  id: 'email',
  rules: rules.email,
  value: '[email protected]',
});

validator.addField({
  id: 'password',
  rules: rules.isTrue,
  value: true,
});

const result = validator.validate();

if (result.isValid) {
  console.log('Validation passed!');
} else {
  console.error('Validation failed:', result.errors);
}

License

MIT Β© Dsshard


Notes

  • Lightweight, flexible, easy to integrate.
  • Server-side and client-side validation supported.
  • Create custom rules for different scenarios.
  • Easy to add dynamic validation logic.

Fun Fact: Early validation of user inputs in a form can reduce backend server load by up to 40% compared to server-only validation!