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

node-easy-validatorjs

v1.0.0

Published

make validation easy

Downloads

8

Readme

EasyValidator

EasyValidator is a lightweight and flexible Node.js library for data validation. It allows you to validate data objects against predefined rules with ease and customization.

Installation

You can install EasyValidator via npm:

npm install node-easy-validatorjs

Usage

const EasyValidator = require('node-easy-validatorjs');

// Create an instance of EasyValidator
const validator = new EasyValidator();

Adding Custom Validation Rules

You can add custom validation rules using the addRule method:

validator.addRule('customRule', async (value, ruleValue) => {
    // Custom validation logic
    return value === ruleValue;
});

Performing Validation

To perform validation, use the validate method:

const validationRules = {
    username: 'required|string|minLength:3|maxLength:20',
    email: 'required|string|format:^\\S+@\\S+\\.\\S+$',
    age: 'required|integer|min:18|max:100',
    // Add more validation rules as needed
};

const userData = {
    username: 'john_doe123',
    email: '[email protected]',
    age: 25,
    // Add more data as needed
};

validator.validate(userData, validationRules)
    .then(validationErrors => {
        if (validationErrors) {
            console.log('Validation errors:', validationErrors);
        } else {
            console.log('Data is valid!');
        }
    })
    .catch(error => {
        console.error('Error during validation:', error);
    });

Validation Rules

EasyValidator supports various validation rules, including:

  • required: Ensures that a field is not empty or null.
  • string: Validates that the value is a string.
  • minLength: Validates the minimum length of a string.
  • maxLength: Validates the maximum length of a string.
  • alpha: Validates if a string contains only alphabetic characters.
  • alphaNumeric: Validates if a string contains only alphanumeric characters.
  • format: Validates if a string matches a specific format using regular expressions.
  • number: Validates if the value is a number.
  • min: Validates if a number is greater than or equal to a specified minimum value.
  • max: Validates if a number is less than or equal to a specified maximum value.
  • integer: Validates if the value is an integer.
  • float: Validates if the value is a floating-point number.
  • positive: Validates if a number is positive.
  • negative: Validates if a number is negative.
  • date: Validates if the value is a date in the format YYYY-MM-DD.
  • past: Validates if a date is in the past.
  • future: Validates if a date is in the future.
  • minLetters: Validates if a string contains at least 3 letters.
  • Custom rules: Developers can define custom validation rules as needed.

Custom Messages

You can provide custom error messages for validation rules using the customMessages parameter:

const customMessages = {
    username: {
        required: 'Username is required!',
        minLength: 'Username must be at least 3 characters long!',
        // Add custom messages for other rules
    },
    // Add custom messages for other fields
};

validator.validate(userData, validationRules, customMessages)
    .then(validationErrors => {
        // Handle validation errors
    });

Conclusion

EasyValidator simplifies the process of data validation in Node.js applications, offering flexibility, customization, and ease of use. With support for custom rules, various built-in validation functions, and customizable error messages, EasyValidator makes data validation straightforward and efficient.