type-3-checkmate
v1.0.0
Published
A powerful and flexible Node.js input validation library.
Downloads
3
Maintainers
Readme
type-3-checkmate - Node.js Input Validation Library
Welcome to type-3-checkmate, a powerful, easy-to-use Node.js validation library for ensuring your inputs meet the criteria you specify. This library offers a comprehensive set of validation methods to check various data types and formats, from strings and numbers to emails, URLs, and more.
Table of Contents
Installation
To get started, install type-3-checkmate via npm:
npm install type-3-checkmateBasic Usage
After installation, you can start using type-3-checkmate in your project like so:
const Checkmate = require('type-3-checkmate');
// Create a new instance of Checkmate with a value
const validator = new Checkmate('[email protected]');
// Validate the value with different validation methods
validator.isEmail().isString();
// Check if validation passed
if (validator.isValid()) {
console.log('Validation passed!');
} else {
console.log('Validation failed:', validator.getErrors());
}Validation Methods
Here is a list of available validation methods you can use to validate different types of data:
- isString(): Checks if the value is a string.
- isNumber(): Checks if the value is a number.
- isBoolean(): Checks if the value is a boolean.
- minLength(min): Ensures the string is at least
mincharacters long. - maxLength(max): Ensures the string is no more than
maxcharacters long. - isEmail(): Checks if the value is a valid email.
- isIn(array): Checks if the value is in the specified array.
- isUrl(): Checks if the value is a valid URL.
- isEmpty(): Checks if the value is empty or null.
- isDate(): Checks if the value is a valid date.
- isAlpha(): Checks if the string contains only alphabetic characters.
- isAlphaNumeric(): Checks if the string contains only alphanumeric characters.
- isPhoneNumber(): Checks if the value is a valid phone number.
- isCreditCard(): Checks if the value is a valid credit card number.
- isUUID(): Checks if the value is a valid UUID.
- isLowercase(): Checks if the value is all lowercase.
- isUppercase(): Checks if the value is all uppercase.
- isPostalCode(): Checks if the value is a valid postal code.
- isName(): Checks if the value is a valid name.
- isAlphanumericSpace(): Checks if the value contains alphanumeric characters and spaces only.
- isAlphaSpace(): Checks if the value contains alphabetic characters and spaces only.
- isHexColor(): Checks if the value is a valid hexadecimal color code.
- isStrongPassword(): Checks if the value meets the criteria for a strong password.
Custom Error Messages
You can provide custom error messages for each validation method. Here’s how:
const validator = new Checkmate('12345');
validator.isNumber().minLength(10, 'Your number must have at least 10 digits.');Chaining Validations
The library allows you to chain multiple validation methods together for cleaner code:
const validator = new Checkmate('[email protected]')
.isEmail()
.isString()
.isLowercase();
if (validator.isValid()) {
console.log('Valid input!');
} else {
console.log('Errors:', validator.getErrors());
}Advanced Usage
You can validate objects with nested fields using the validateObject method:
const user = {
email: '[email protected]',
password: 'StrongPass123!',
phoneNumber: '123-456-7890'
};
const schema = {
email: (validator) => validator.isEmail(),
password: (validator) => validator.isStrongPassword(),
phoneNumber: (validator) => validator.isPhoneNumber()
};
const result = Checkmate.validateObject(user, schema);
console.log(result);Validation for Objects
In addition to validating individual values, type-3-checkmate supports validating objects using a schema. This allows for more complex validation scenarios where you define custom validation rules for each field.
const schema = {
name: (validator) => validator.isAlpha().minLength(3),
age: (validator) => validator.isNumber().min(18),
email: (validator) => validator.isEmail(),
};
const userData = {
name: 'John',
age: 25,
email: '[email protected]'
};
const errors = Checkmate.validateObject(userData, schema);
console.log(errors);Error Handling
When a validation fails, you can retrieve the error messages using the getErrors() method:
const validator = new Checkmate('invalid-email');
validator.isEmail();
if (!validator.isValid()) {
console.log(validator.getErrors()); // Prints validation errors
}Example
Here's a full example showcasing how to use the library:
const Checkmate = require('type-3-checkmate');
const formData = {
email: '[email protected]',
password: 'password123!',
age: 25
};
const schema = {
email: (validator) => validator.isEmail(),
password: (validator) => validator.isStrongPassword(),
age: (validator) => validator.isNumber().min(18)
};
const errors = Checkmate.validateObject(formData, schema);
if (errors) {
console.log('Validation failed:', errors);
} else {
console.log('Form is valid!');
}Testing
We recommend writing unit tests for any custom validations you create. You can use a testing framework like Jest to test your validation logic.
npm install jestCreate a test file and add your test cases:
const Checkmate = require('type-3-checkmate');
test('should validate a valid email', () => {
const validator = new Checkmate('[email protected]');
expect(validator.isEmail().isValid()).toBe(true);
});
test('should fail invalid email', () => {
const validator = new Checkmate('invalid-email');
expect(validator.isEmail().isValid()).toBe(false);
});Contributing
We welcome contributions to type-3-checkmate! If you want to report a bug or add a new feature, please follow these steps:
- Fork the repository
- Create a new branch for your feature or fix
- Write tests for your changes
- Submit a pull request with a clear description of your changes
Future Plans
Here are some features we're planning to add in the future:
- Async Validation: Support for async validation methods (e.g., checking if an email already exists in a database).
- Localization: Support for multiple languages for error messages to make the library more globally accessible.
- More Validation Rules: Additional validations like IP addresses, SSNs, etc.
- Custom Validation Functions: Allow users to create custom validators easily.
- Integration with Front-End Frameworks: Easy integration with popular front-end frameworks like React and Angular.
- Performance Enhancements: Further optimize regex checks and improve overall performance for large datasets.
License
type-3-checkmate is licensed under the MIT License.
