1.0.6 • Published 3 years ago

use-validation-react v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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?

PropertyTypeDescription
validationResultRecord<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
isValidatedbooleanA 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() => voidCalling 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) => booleanChecks 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

RuleDescriptionNotes
RequiredReturns true only if the input has non-empty value
EmailReturns true only if the value matches email regexRegex 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,}))$/
NumberReturns true only if the value is a number or can be parsed to a number
PhoneReturns true if the value is a formatted phone numberMay 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})$/
NoSpacesReturns true if the value contains no spaces
RegexReturns true if the value matches the passed regexIn addition to rule and errorMessage properties, you need to pass the regex using the meta property.
MinimumLengthReturns true if the value length is >= the one passedIn addition to rule and errorMessage properties, you need to pass the length using the meta property.
MaximumLengthReturns true if the value length is <= the one passedIn addition to rule and errorMessage properties, you need to pass the length using the meta property.
RemoteSometimes 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.
1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago