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

react-form-rules

v0.0.14

Published

Declarative and reusable validation rules for React forms — simple, flexible, and easy to integrate.

Readme

react-form-rules

Smart and flexible form validation for React using contextual input rules.
Show dynamic helper messages on inputs, prevent form submission if invalid, and automatically scroll/select the first error field.

Features

  • ✅ Input-level validation via useInputRules hook
  • ✅ Dynamic helperText shown while typing
  • ✅ Prevent form submission if any rule fails
  • ✅ Automatically scroll to and select the first invalid input field
  • ✅ Minimal setup, fully typed with TypeScript
  • ✅ No external dependencies

Installation

npm install react-form-rules
# or
yarn add react-form-rules

Usage

import { Form, useInputRules } from "react-form-rules";
import type { Rules } from "react-form-rules";

function FormControl({ label, rules }: { label: string; rules: Rules }) {
  const { ref, error, helperText } = useInputRules({ rules });

  return (
    <div style={{ marginBottom: 12 }}>
      <label>
        {label}
        <input ref={ref} />
      </label>
      {error && <p style={{ color: "red" }}>{helperText}</p>}
    </div>
  );
}

function Login() {
  return (
    <Form onSubmit={() => alert("All valid!")}>
      <FormControl
        label="Email"
        rules={[(value) => value.includes("@") || "Email is not valid"]}
      />
      <FormControl
        label="Password"
        rules={[
          (value) => value.length > 6 || "Password must be longer than 6",
        ]}
      />
      <button type="submit">Submit</button>
    </Form>
  );
}

Note: The <Form> component must wrap all inputs that use useInputRules or WithInputRules hooks. These hooks depend on React context provided by <Form> for validation and error management. Using them outside of <Form> will cause errors or unexpected behavior.


WithInputRules Component

An alternative render-prop component that provides input validation utilities to its child as a function.

<WithInputRules rules={[(val) => val.length > 0 || "This field is required"]}>
  {({ ref, error, helperText }) => (
    <div className={error ? "text-error-700" : undefined}>
      <input ref={ref} />
      <p>{helperText}</p>
    </div>
  )}
</WithInputRules>

This component internally uses useInputRules and passes the result to its child function, enabling flexible and declarative validation rendering.


Example Of Rules

Rules are simple functions that receive the input value and return either true (valid) or a string with an error message.

const required: Rules = [(value) => !!value.trim() || "Field is required"];

const otp: Rules = [
  ...required,
  (value) => value.length === 5 || "OTP must be 5 characters",
];

These are only examples. You can define your own rule arrays and pass them to useInputRules({ rules }).

Props

<Form />

| Prop | Type | Default | Description | | --------------- | -------- | ------- | ----------------------------------------- | | onSubmit | Function | - | Called when all inputs are valid | | onError | Function | - | Called when at least one input is invalid | | scrollOnError | boolean | true | Scroll to first invalid input | | selectOnError | boolean | true | Select input text on first invalid input |

useInputRules() return

| Field | Type | Description | | ------------ | ---------------- | -------------------------------------- | | ref | ref callback | Pass to <input /> or <textarea /> | | helperText | string \| null | Message from first failing rule | | error | boolean | Whether the input is currently invalid |

GitHub

https://github.com/amindasoomi1/react-form-rules


License

MIT