Tiny Validation is a lightweight, chainable input validation library for Node.js. Validate strings, numbers, emails, and more with ease. Example: validate(input).isEmail().isNotEmpty(). No dependencies, simple API, and perfect for quick validation needs w
npm install @nodebysam/tiny-validationbash
npm install @nodebysam/tiny-validation
`
Alternatively, if you are using Yarn:
`bash
yarn add @nodebysam/tiny-validation
`
Usage
Here's how you can get started using Tiny Validation.
$3
`javascript
const TinyValidation = require('@nodebysam/tiny-validation');
const Validator = TinyValidation.Validator;
const userInput = {
email: 'test@example.com',
password: 'Pass1234',
phoneNumber: '123-456-7890'
};
// Email validation
const emailValidator = new Validator(userInput.email);
emailValidator.use(TinyValidation.rules.isEmail);
// Password validation
const passwordValidator = new Validator(userInput.password);
passwordValidator
.use(TinyValidation.rules.isMinLength, 8)
.use(TinyValidation.rules.isStrongPassword);
// Phone number validation
const phoneValidator = new Validator(userInput.phoneNumber);
phoneValidator.use(TinyValidation.rules.isPhoneNumber);
if (emailValidator.isValid() && passwordValidator.isValid() && phoneValidator.isValid()) {
console.log('All inputs are valid!');
} else {
console.error('Validation failed!');
console.log('Errors:', {
emailErrors: emailValidator.getErrors(),
passwordErrors: passwordValidator.getErrors(),
phoneErrors: phoneValidator.getErrors()
});
}
`
$3
* __isEmail:__ Validate that the value is a valid email.
* __isNotEmpty:__ Check if the value is not empty.
* __isStrongPassword:__ Ensure the password is strong.
* __isPhoneNumber:__ Check if the value is a valid phone number.
* __isInRange:__ Check if a number is within a specified range.
You can chain these validation rules using the __.use()__ method, passing the value you want to validate and any neccessary parameters.
$3
Here are the available validation rules in Tiny Validation (more rules to come soon):
* __isEmail__: Validates whether the value is a valid email address.
* __isNotEmpty__: Checks if the value is not empty.
* __isNumber__: Validates whether the value is a number.
* __isObject__: Validates whether the value is an object.
* __isString__: Validates whether the value is a string.
* __isBoolean__: Validates whether the value is a boolean.
* __isArray__: Validates whether the value is an array.
* __isDate__: Validates whether the value is a date.
* __isInteger__: Validates whether the value is an integer.
* __isPositive__: Validates whether the number is positive.
* __isNegative__: Validates whether the number is negative.
* __isLength__: Validates the length of the value.
* __isEqual__: Checks if the value is equal to a specified value.
* __isHot__: Checks if the value is considered "hot" (for specific rules or ranges).
* __isInRange__: Checks if the value is within a given range.
* __isURL__: Validates whether the value is a valid URL.
* __isUUID__: Validates whether the value is a valid UUID.
* __isAlpha__: Validates if the value is alphabetic.
* __isAlphanumeric__: Validates if the value is alphanumeric.
* __isPhoneNumber__: Validates if the value is a valid phone number.
* __isAfterDate__: Checks if the date is after a specified date.
* __isBeforeDate__: Checks if the date is before a specified date.
* __isValidJSON__: Checks if the value is valid JSON.
* __isFibonacci__: Checks if the value is part of the Fibonacci sequence.
* __isLucky__: Checks if the value meets the criteria for being "lucky."
* __isAddress__: Validates if the value is a valid address.
__isFloat__: Validates if the value is a valid floatingpoint number.
* __isError__: Checks if the value is an error.
* __isIn__: Validates whether the value is in a specified list.
* __isNotIn__: Validates whether the value is not in a specified list.
* __isMinLength__: Validates if the value's length is greater than or equal to a specified minimum length.
* __isMaxLength__: Validates if the value's length is less than or equal to a specified maximum length.
* __isInstanceOf__: Checks if the value is an instance of a specific class.
* __isStrictEqual__: Checks if the value strictly equals a specified value.
* __isNotEqual__: Checks if the value does not equal a specified value.
* __isNotStrictEqual__: Checks if the value is not strictly equal to a specified value.
* __isFilePath__: Checks if the value is a valid file path.
* __isIpAddress__: Validates if the value is a valid IP address.
* __isDomainName__: Validates if the value is a valid domain name.
* __isCurrency__: Checks if the value is a valid currency.
* __isStrongPassword__: Ensures the password meets strength criteria (e.g., includes upper case, numbers, symbols).
* __isMediaUrl__: Validates if the value is a URL to a media platform like YouTube, TikTok, etc.
* __hasNumber__: Validates whether the value contains a number.
* __hasSpecialCharacter__: Validates whether the value contains special characters.
Running Tests
Tiny Validation includes unit tests to ensure that everything works as expected. To run the tests, follow these steps:
1. Install the dependencies:
`bash
npm install
`
2. Run the tests:
`bash
npm test
``