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

advance-form-validator

v1.0.1

Published

A npm package that helps to validate form data

Downloads

4

Readme

Advance Form Validator

advance-form-validator is a powerful and flexible library for validating form data in JavaScript. It allows you to define complex validation rules and provides detailed feedback for user input errors.

Installation

To install the package, you can use npm or yarn:

npm install advance-form-validator

Or,

yarn add advance-form-validator

Quick Usage

Here's a basic example of how to use advance-form-validator in a React component:

Step 1: Import and Set Up

Import the validateForm function from the package and use it to validate your form data against defined rules.

Validation Rules

The validateForm function supports the following validation rules:

Type Validation:

  • type: 'string' - Ensures the field is a string.
  • type: 'number' - Ensures the field is a phone number.
  • type: 'email' - Ensures the field is a valid email address.
  • type: 'file' - Ensures the field is a valid file with type and size constraints.

Required Field:

  • required: true - Field must not be empty.

Length Constraints:

  • minLength - Minimum number of characters for a string.

  • maxLength - Maximum number of characters for a string. File Constraints (for file inputs):

  • allowedTypes - Array of allowed MIME types.

  • maxSize - Maximum allowed file size in bytes.

Custom Messages:

  • messages - Custom error messages for each validation rule. For example, messages: { required: 'This field is required.' }.

Example

Here's an example of how to use validateForm with a form that includes email, phone, username, password, full name, profile picture, and cover image fields.

const formData = {
  email: '[email protected]',
  phone: '1234567890',
  username: 'validUser',
  password: 'securePassword',
  fullName: 'John Doe',
  profilePic: fileInput.files[0],
  coverImage: fileInput.files[1]
};

Example Rules

you can pass custom message if you wnat else you don't need to pass message section

const rules = {
  email: { 
    type: 'email', 
    required: true,
    messages: {
      required: 'Email is mandatory.',
      email: 'Please enter a valid email address.'
    }
  },
  phone: { 
    type: 'number', 
    required: true,
    messages: {
      required: 'Phone number is required.',
      number: 'Invalid phone number format.'
    }
  },
  username: { 
    type: 'string', 
    minLength: 3, 
    maxLength: 20, 
    required: true,
    messages: {
      minLength: 'Username must be at least 3 characters long.',
      maxLength: 'Username can be at most 20 characters long.'
    }
  },
  password: { 
    type: 'string', 
    minLength: 8, 
    required: true,
    messages: {
      minLength: 'Password must be at least 8 characters long.'
    }
  },
  fullName: { 
    type: 'string', 
    minLength: 2,
    messages: {
      minLength: 'Full name must be at least 2 characters long.'
    }
  },
  profilePic: { 
    file: true,
    allowedTypes: ['image/jpeg', 'image/png'],
    maxSize: 1000000, // 1 MB
    messages: {
      required: 'Profile picture is required.',
      type: 'Invalid file type. Only JPEG and PNG are allowed.',
      maxSize: 'File size exceeds 1 MB.'
    }
  },
  coverImage: { 
    file: true,
    allowedTypes: ['image/jpeg', 'image/png'],
    maxSize: 2000000, // 2 MB
    messages: {
      required: 'Cover image is required.',
      type: 'Invalid file type. Only JPEG and PNG are allowed.',
      maxSize: 'File size exceeds 2 MB.'
    }
  }
};
const result = await validateForm(formData, rules);
console.log(result.errors);

// Handle valid data (if needed)
console.log('Valid Data:', result.data);

Combined error

const combinedErrors = Object.values(errors).flat();

Displaying Errors

{combinedErrors.length > 0 && (
        <div className="error-container">
          <h3>Form Errors:</h3>
          <ul>
            {combinedErrors.map((error, index) => (
              <li key={index}>{error}</li>
            ))}
          </ul>
        </div>
      )}