A minimalistic Node.js data validation library that provides a collection of rules for validating different types of values. This library allows developers to easily chain multiple validation rules, ensuring that the input data is valid according to your custom needs.
- Chaining Validation: Apply multiple rules to a single value.
- Comprehensive Rules: Includes rules for validating emails, phone numbers, dates, URLs, and more.
- Easy Error Handling: Gather validation errors and handle them as needed.
- Flexible Custom Rules: Add custom validation rules to suit your specific needs.
- Lightweight: Simple, focused, and easy to use.
To install Tiny Validation in your project, run the following command:
npm install @nodebysam/tiny-validation
Alternatively, if you are using Yarn:
yarn add @nodebysam/tiny-validation
Here's how you can get started using Tiny Validation.
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()
});
}
- 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.
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 floating*point 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.
Tiny Validation includes unit tests to ensure that everything works as expected. To run the tests, follow these steps:
- Install the dependencies:
npm install
- Run the tests:
npm test
This will run the unit tests using Ava. If everything is set up correctly, you should see the test results printed in the console.
We welcome contributions to Tiny Validation! Here's how you can help:
- Fork the repository to your GitHub account.
- Create a new branch for your feature or bug fix.
- Make your changes and write tests if neccessary.
- Commit your changes and push them to your fork.
- Open a Pull Request describing your changes.
We will review your pull request, and if everything looks good, we'll merge it!
If you find a bug or have an idea for a new feature, feel free to open an issue on GitHub.
Tiny Validation is released under the GNU v3.0 License.