1.0.0 • Published 1 year ago

@mahdi.golzar/validator v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

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: 'test@example.com',
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: 'test@example.com',
password: 'validpassword',
age: 25
};
console.log(validator.validateAll(inputs)); // {}
1.0.0

1 year ago