nazar
v1.0.1
Published
nazartwo
Downloads
6
Readme
Data Validation Library
Data Validation Library is a lightweight JavaScript library for validating data inputs. It provides functions to validate strings, numbers, and emails with customizable options.
Installation
You can install the library via npm:
npm install data-validator
Usage
const DataValidator = require('data-validator');
// Validate a string
try {
DataValidator.validateString('Hello World', { minLength: 5 });
console.log('String is valid');
} catch (error) {
console.error(error.message);
}
// Validate a number
try {
DataValidator.validateNumber(42, { minValue: 0 });
console.log('Number is valid');
} catch (error) {
console.error(error.message);
}
// Validate an email
try {
DataValidator.validateEmail('[email protected]');
console.log('Email is valid');
} catch (error) {
console.error(error.message);
}
API
validateString(value, options)
Validates a string value with optional options:
minLength
: Minimum length of the stringmaxLength
: Maximum length of the stringpattern
: Regular expression pattern to match the string against
validateNumber(value, options)
Validates a numeric value with optional options:
minValue
: Minimum value of the numbermaxValue
: Maximum value of the number
validateEmail(value)
Validates an email address.
validate(value, type, options)
Convenience method to validate values of different types:
value
: The value to validatetype
: The type of data to validate ('string', 'number', or 'email')options
: Optional options object for additional validation rules
License
This project is licensed under the MIT License - see the LICENSE file for details.