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

@mahdi.golzar/validator

v1.0.0

Published

validator

Readme

Validator

Validator is a flexible and customizable input validation tool written in JavaScript. It allows you to define and apply validation rules to various inputs, ensuring they meet specified criteria.

Features

  • Custom Rules: Define custom validation rules for different fields.
  • Dynamic Validation: Validate individual inputs or an entire object with multiple fields.
  • Error Messaging: Customize error messages for each rule.

Installation

No installation is required. Simply include the Validator class in your JavaScript project.

Usage

Basic Setup

Create an instance of Validator:

const validator = new Validator();

Add validation rules:

Use addRule to define validation rules for specific fields. Each rule consists of a function that returns true if the input is valid, and a custom error message.

validator.addRule('email',
value => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value),
'Invalid email format'
);
validator.addRule('password',
value => value.length >= 6,
'Password must be at least 6 characters long'
);
validator.addRule('age',
value => Number.isInteger(value) && value > 0,
'Age must be a positive integer'
);

Validate inputs:

Use the validate method to check if an individual input meets its rules.

console.log(validator.validate('email', 'invalid-email')); // [ 'Invalid email format' ]
console.log(validator.validate('password', 'short')); // [ 'Password must be at least 6 characters long' ]
console.log(validator.validate('age', -5)); // [ 'Age must be a positive integer' ]

Validate an entire object:

Use the validateAll method to validate all fields in an object. This method returns an object where keys are field names and values are arrays of error messages.

const inputs = {
email: '[email protected]',
password: 'validpassword',
age: 25
};
console.log(validator.validateAll(inputs)); // {}

API

constructor(): Initializes a new Validator instance. addRule(field: string, ruleFn: function, message: string): Adds a validation rule for a specific field. ruleFn is a function that returns true if the value is valid and false otherwise. message is the error message to be returned if validation fails. validate(field: string, value: any): Validates a single field's value against its rules. Returns an array of error messages. validateAll(inputs: object): Validates an entire object with multiple fields. Returns an object where keys are field names and values are arrays of error messages.

Example

Here’s a complete example of using the Validator class:

// Create a validator instance
const validator = new Validator();

// Add rules
validator.addRule('email',
value => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value),
'Invalid email format'
);
validator.addRule('password',
value => value.length >= 6,
'Password must be at least 6 characters long'
);
validator.addRule('age',
value => Number.isInteger(value) && value > 0,
'Age must be a positive integer'
);

// Validate individual inputs
console.log(validator.validate('email', 'invalid-email')); // [ 'Invalid email format' ]
console.log(validator.validate('password', 'short')); // [ 'Password must be at least 6 characters long' ]
console.log(validator.validate('age', -5)); // [ 'Age must be a positive integer' ]

// Validate an entire object
const inputs = {
email: '[email protected]',
password: 'validpassword',
age: 25
};
console.log(validator.validateAll(inputs)); // {}