int-validate
v1.0.9
Published
Validator
Readme
Field Validator
A lightweight, flexible, and chainable validation library for Node.js applications that provides robust validation rules for common data types and formats. Built with safety and usability in mind.
Features
- 🔗 Chainable validation rules
- 🌍 Locale-specific validations
- 🔒 Strong password validation
- 📝 Comprehensive error reporting
- 🛡️ Built-in type safety
- 🧩 Extensible architecture
- 0️⃣ Zero external dependencies
Installation
npm install int-validateQuick Start
const validator = require('int-validate')();
// Define validation schema
const schema = {
email: validator.required().email().set(),
password: validator.required().password().set()
};
// Data to validate
const data = {
email: '[email protected]',
password: 'SecureP@ss123'
};
// Perform validation
const result = validator.validate(schema, data);
console.log(result.valid ? 'Valid!' : result.errors);Validation Rules
Text Validation
| Validator | Description | Example |
|-----------|-------------|---------|
| alpha(locale?) | Letters only | validator.alpha('en-US') |
| alphanumeric(locale?) | Letters and numbers | validator.alphanumeric('en-US') |
| length(min, max) | String length range | validator.length(3, 20) |
| contains(field) | Contains another field's value | validator.contains('description') |
| matches(field) | Exact match with another field | validator.matches('password') |
Number Validation
| Validator | Description | Example |
|-----------|-------------|---------|
| numeric() | Numbers only | validator.numeric() |
| decimal() | Decimal numbers | validator.decimal() |
Special Types
| Validator | Description | Example |
|-----------|-------------|---------|
| email() | Email addresses | validator.email() |
| password() | Strong passwords | validator.password() |
| creditCard() | Credit card numbers | validator.creditCard() |
| phone(locale) | Phone numbers | validator.phone('en-US') |
| postalCode(locale) | Postal codes | validator.postalCode('US') |
Other Validators
| Validator | Description | Example |
|-----------|-------------|---------|
| base64() | Base64 strings | validator.base64() |
| boolean() | Boolean values | validator.boolean() |
| date() | Date strings | validator.date() |
| time() | Time strings | validator.time() |
| required() | Required fields | validator.required() |
Password Validation Rules
The password validator enforces the following security rules:
- Minimum length of 8 characters
- At least one lowercase letter (a-z)
- At least one uppercase letter (A-Z)
- At least one number (0-9)
- At least one special character (~`!#$%^&*+=-[]\',/{}|":<>?)
Common Use Cases
User Registration Form
const registrationSchema = {
username: validator
.required()
.alphanumeric()
.length(3, 20)
.set(),
email: validator
.required()
.email()
.set(),
password: validator
.required()
.password()
.set(),
confirmPassword: validator
.required()
.matches('password')
.set()
};Payment Form
const paymentSchema = {
cardNumber: validator
.required()
.creditCard()
.set(),
expiryDate: validator
.required()
.date()
.set(),
cvv: validator
.required()
.numeric()
.length(3, 4)
.set()
};Address Validation
const addressSchema = {
streetName: validator
.required()
.alpha()
.set(),
houseNumber: validator
.required()
.numeric()
.set(),
postalCode: validator
.required()
.postalCode('US')
.set()
};Validation Response
The validation function returns an object with the following structure:
{
valid: boolean,
errors: [
{
field: string,
message: string
}
]
}Example error response:
{
valid: false,
errors: [
{
field: "password",
message: "Not a strong enough password. Needs 8 characters with at least a number, lowercase, uppercase and special character."
}
]
}Best Practices
- Chain Validators
// Good
const schema = {
username: validator
.required()
.alphanumeric()
.length(3, 20)
.set()
};
// Avoid
const schema = {
username: [...validator.required().set(),
...validator.alphanumeric().set()]
};- Clear Validators After Use
// Validators are automatically cleared after .set()
const commonValidators = validator
.required()
.length(3, 20)
.set();- Handle Validation Results
const result = validator.validate(schema, data);
if (!result.valid) {
result.errors.forEach(error => {
console.log(`${error.field}: ${error.message}`);
});
}Error Messages
Default error messages are provided for each validation type. Examples:
{
required: "Required field",
email: "Not a valid e-mail address",
password: "Not a strong enough password. Needs 8 characters with at least a number, lowercase, uppercase and special character.",
// ... other messages
}Type Safety
The validator includes built-in type safety features:
- Safe string conversion
- Null/undefined handling
- Type checking for inputs
Performance Considerations
- Validators are cleared after each
set()call - Efficient string manipulation
- Optimized regex patterns
- Minimal memory footprint
Browser Support
The validator is primarily designed for Node.js applications but can be used in modern browsers with appropriate bundling.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Author
Gregory - Genie
