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

form-validation-library-yr

v1.0.6

Published

Validation Library

Downloads

2

Readme

form-validation-library-yr

A simple and customizable form validation library for JavaScript. It provides built-in validators for common form fields and supports custom validation logic.

Table of Contents

Installation

Install the library using npm:

npm install form-validation-library-yr

Or using yarn:

yarn add form-validation-library-yr

Usage

First, import the library and create an instance of FormValidator with your validation rules:

import FormValidator from 'form-validation-library-yr';

const rules = {
  username: { required: true },
  email: { required: true, email: true },
  age: { required: true, integer: true },
  birthdate: { required: true, date: true, allowFuture: false },
};

const validator = new FormValidator(rules);

const formData = {
  username: 'JohnDoe',
  email: '[email protected]',
  age: '25',
  birthdate: '1995-05-15',
};

const errors = validator.validate(formData);
console.log(errors);

API

Constructor

new FormValidator(rules)
  • rules (Object): An object defining the validation rules for each field.

validate

validate(formData)
  • formData (Object): An object containing the form data to be validated.
  • Returns: An object containing validation errors, where the keys are the field names and the values are the error messages.

Custom Validators

You can define custom validation logic by providing a custom validator function in the rules:

const rules = {
  birthdate: { 
    required: true, 
    date: true, 
    custom: { 
      validator: (value) => {
        const dateObj = new Date(value);
        const now = new Date();
        dateObj.setHours(0, 0, 0, 0);
        now.setHours(0, 0, 0, 0);
        return dateObj <= now;
      }, 
      message: 'Birthdate must be a past date'
    } 
  },
};

Built-in Validators

The library includes several built-in validators:

  • required: Ensures the field is not empty.
  • email: Validates an email address.
  • integer: Validates that the input is an integer.
  • string: Validates that the input is a string.
  • date: Validates that the input is a date in the format YYYY-MM-DD.
  • pattern: Validates the input against a regular expression.

Examples

Basic Usage

import FormValidator from 'form-validation-library-yr';

const rules = {
  username: { required: true },
  email: { required: true, email: true },
};

const validator = new FormValidator(rules);

const formData = {
  username: '',
  email: 'invalid-email',
};

const errors = validator.validate(formData);
console.log(errors);
// Output:
// {
//   username: 'username is required',
//   email: 'email is not a valid email address'
// }

Custom Validation

import FormValidator from 'form-validation-library-yr';

const isPastDate = (date) => {
  const dateObj = new Date(date);
  const now = new Date();
  dateObj.setHours(0, 0, 0, 0);
  now.setHours(0, 0, 0, 0);
  return dateObj <= now;
};

const rules = {
  birthdate: { 
    required: true, 
    date: true, 
    custom: { 
      validator: isPastDate, 
      message: 'Birthdate must be a past date'
    } 
  },
};

const validator = new FormValidator(rules);

const formData = {
  birthdate: '2050-01-01',
};

const errors = validator.validate(formData);
console.log(errors);
// Output:
// {
//   birthdate: 'Birthdate must be a past date'
// }

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any bugs or enhancements.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Explanation:

  1. Installation: Instructions for installing the package using npm or yarn.
  2. Usage: Basic example of how to use the library.
  3. API: Detailed explanation of the FormValidator class methods and parameters.
  4. Built-in Validators: List of built-in validators with descriptions.
  5. Examples: Additional examples to demonstrate basic usage and custom validation.
  6. Contributing: Information on how to contribute to the project.
  7. License: License information for the project.

Below shows the gist link for example code:
Example Code Gist