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

use-validation-react

v1.0.6

Published

a lightweight react library for hook-based declarative form validation

Downloads

19

Readme

use-validation-react

A simple react hook for all your form validations.

How to install:

Install via npm

npm i use-validation-react

or via yarn

yarn add use-validation-react

How to use:

Library exports useValidation hook. The hook accepts one argument, validation records

const validations = {
  name: [
    {
      rule: ValidationRule.Required,
      errorMessage: 'Please provide a valid first name',
    },
  ],
  about: [
    {
      rule: ValidationRule.MinLength,
      errorMessage: 'About should be 20 characters or more',
      meta: 20
    },
  ],
  phoneNumber: [
    {
      rule: ValidationRule.Phone,
      errorMessage: 'Please provide a valid phone number',
    },
  ],
  email: [
    {
      rule: ValidationRule.Email,
      errorMessage: 'Please provide a valid email',
    },
  ],
}

You could then use these validation rules inside your component as below Note: The property names in your object that you need to validate should match the property names inside your validation rules.

const Profile = () => {
  const [profileData, setProfileData] = useState({
    name: '',
    about: '',
    phoneNumber: '',
    email: '',
  });
  
  const {
    validationResult,
    isValidated,
    isFormValid,
    validateFormElement,
    clearValidations,
    validateAll
  } = useValidation(updateProfileValidations);
  
  return (
    <>
      <input type="text" value={profileData.name} />
      ...
    </>
  )
}

To check if your form is valid, you simply call isFormValid with the object to validate

const formIsValid = await isFormValid(profileData);

This will run validation for your whole form and return true or false.

You may also want to show error messages for each input that is invalid.

You could access the validation result for each input using validationResult

e.g., To show an error message for name input, you could use the following: Note: Validation are only run on demand. To know if validations have been run use isValidated boolean.

<input type="text" value={profileData.name} />
{isValidated && !validationResult['name'].isValid && (<div>{validationResult['name'].messages[0]}</div>)}

This will display an error message if name input has been validated and the validation returned false.

What can you use?

| Property | Type | Description | |:--------------------|:-----------------------|:-----------------------| | validationResult | Record<string, { isValid: boolean; messages?: string[]; }> | A key value pair that is populated once the validations have run. The value contains two properties: isValid: Boolean specifying whether an input is valid or not messages: An array of all the messages for an input for which the validations have failed | | isValidated | boolean | A boolean specifying whether validations have run. Validations are considered to have run if, isFormValid, validateFormElement or validateAll have been called | | isFormValid | (form: any) => Promise<boolean> | Method that accepts the object to be validated as argument and returns a Promise that resolves to a true if all validations pass and false if one or more validations fail. | | validateFormElement | (form: any, elementKey: string) => Promise<void> | Sometimes you may want to only run validations for a particular form element. This method accepts two arguments, object to validate and the specific property name. This will update the validationResult with the result of this particular input. | | clearValidations | () => void | Calling this method clears the validationResult. | | validateAll | (form: any) => Promise<Record<string, { isValid: boolean; messages?: string[]; }>> | Does the same thing as isFormValid, but returns the validationResult instead of a boolean. | | isElementValid | (elementKey: string) => boolean | Checks if the form element with the given key is valid or not. Note: Calling this method does not run the validation rules for that particular form element. To run validations, use validateFormElement |

Available Validation Rules

| Rule | Description | Notes | |:-----|:------------|:------| |Required|Returns true only if the input has non-empty value|| |Email|Returns true only if the value matches email regex|Regex used: /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/| |Number|Returns true only if the value is a number or can be parsed to a number|| |Phone|Returns true if the value is a formatted phone number|May not work for all kinds of phone numbers. For reference, the regex used to validate is /^(\+[0-9]+)?\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\3([0-9]{4})$/| |NoSpaces|Returns true if the value contains no spaces|| |Regex|Returns true if the value matches the passed regex|In addition to rule and errorMessage properties, you need to pass the regex using the meta property.| |MinimumLength| Returns true if the value length is >= the one passed| In addition to rule and errorMessage properties, you need to pass the length using the meta property.| |MaximumLength| Returns true if the value length is <= the one passed| In addition to rule and errorMessage properties, you need to pass the length using the meta property.| |Remote|Sometimes you want to write your own validation logic. Use this validation rule for that. You need to pass a function of type (value: string \| number) => Promise<boolean> to the meta property of your rule.| This rule can be useful when you need to validate a value on the server e.g., check if email exists, etc.|