3.2.3 • Published 6 months ago
regex-validify v3.2.3
regex-validify
A comprehensive, type-safe regular expression validation library for Node.js and browser environments. Supports common validation patterns, complex validations, and custom regex testing with TypeScript support.
Features
- 🚀 25+ built-in validation patterns
- 💪 TypeScript support with full type definitions
- 🔒 Complex validation patterns (passwords, postal codes, crypto addresses)
- 🌍 International format support
- ⚡ Zero dependencies
- 📚 Comprehensive documentation
- ✅ Fully tested with Jest
Installation
$ npm install regex-validify
# or
$ yarn add regex-validify
# or
$ pnpm add regex-validify
Quick Start
import RegExpValidator from 'regex-validify';
// Initialize the validator
const validator = new RegExpValidator();
// Basic email validation
const emailResult = validator.checkEmail('user@example.com');
console.log(emailResult); // true
// Password validation with complexity level
const passwordResult = validator.checkPassword('MyP@ssw0rd', 'complex');
console.log(passwordResult); // true
// Custom validation
const customResult = validator.customValidation('test123', /^[a-z0-9]+$/);
console.log(customResult); // true
Available Validators
Basic Validators
checkEmail(email: string)
- Email addressescheckIPv4(ip: string)
- IPv4 addressescheckIPv6(ip: string)
- IPv6 addressescheckMacAddress(mac: string)
- MAC addressescheckUrl(url: string)
- URLscheckDomain(domain: string)
- Domain namescheckUsername(name: string)
- Usernames (3-16 characters)checkHexColor(color: string)
- Hex color codescheckOnlyIntegers(value: string)
- Integer numberscheckOnlyAlpha(value: string)
- Alphabetic characterscheckEmoji(value: string)
- Emoji characterscheckAnsiArt(value: string)
- ANSI art characterscheckJwt(jwt: string)
- JWT tokenscheckSemver(version: string)
- Semantic versionscheckPhoneNumber(phone: string)
- Phone numberscheckCreditCard(card: string)
- Credit card numberscheckSSN(ssn: string)
- Social Security NumberscheckISBN(isbn: string)
- ISBN numberscheckTime(time: string)
- Time strings (12/24 hour format)
Complex Validators
checkPassword(password: string, level: ValidationLevel)
- Password strength with three levels:simple
: Minimum 6 charactersmedium
: Minimum 8 characterscomplex
: 8+ chars with uppercase, lowercase, number, and special char
checkPostalCode(code: string, country: CountryCode)
- Country-specific postal codes:- Supports: US, UK, CA, and generic format
checkCryptoAddress(address: string, type: CryptoType)
- Cryptocurrency addresses:- Supports: BTC, ETH
checkCoordinates(lat: string, lng: string)
- Geographic coordinates
And Ton of other useful validators
Configuration
You can customize the validator behavior with options:
const validator = new RegExpValidator({
timeout: 5000, // Maximum validation time (ms)
caseSensitive: true, // Case-sensitive validation
multiline: false, // Multiline mode
unicode: true, // Unicode support
});
Validation Result
All validation methods return a ValidationResult
object:
interface ValidationResult {
isValid: boolean; // Validation status
message?: string; // Optional status message
matches?: RegExpMatchArray | null; // Optional regex matches
}
Custom Validation
Create custom validation patterns:
const customValidator = new RegExpValidator();
// Define custom pattern
const urlWithProtocol = /^https:\/\/[a-zA-Z0-9-]+\.[a-zA-Z]{2,}$/;
// Use custom validation
const result = customValidator.customValidation(
'https://example.com',
urlWithProtocol,
);
Type Definitions
type ValidationLevel = 'simple' | 'medium' | 'complex';
type CountryCode = 'US' | 'UK' | 'CA' | 'ANY';
type CryptoType = 'BTC' | 'ETH';
type ValidatorMode = 'verbose' | undefined;
interface ValidatorOptions {
timeout?: number;
caseSensitive?: boolean;
multiline?: boolean;
unicode?: boolean;
}
Documentation
Detailed documentation is available in the /docs
directory. To generate the
documentation:
$ npm run docs
Then open docs/index.html
in your browser.
Testing
Run the test suite:
describe('RegExpValidator', () => {
let validator: RegExpValidator;
beforeAll(() => {
validator = new RegExpValidator();
});
// Test Email Validation
test('should validate a valid email', () => {
const result = validator.checkEmail('test@example.com');
expect(result).toBe(true);
});
test('should invalidate an invalid email', () => {
const result = validator.checkEmail('invalid-email');
expect(result).toBe(false);
});
// Test IPv4 Validation
test('should validate a valid IPv4 address', () => {
const result = validator.checkIPv4('192.168.0.1');
expect(result).toBe(true);
});
test('should invalidate an invalid IPv4 address', () => {
const result = validator.checkIPv4('999.999.999.999');
expect(result).toBe(false);
});
// Test Password Validation (Simple)
test('should validate a simple password', () => {
const result = validator.checkPassword('simple123', 'simple');
expect(result).toBe(true);
});
test('should invalidate a simple password if too short', () => {
const result = validator.checkPassword('short', 'simple');
expect(result).toBe(false);
});
// Test Custom Validation
test('should pass custom regex validation', () => {
const result = validator.customValidation('12345', /^\d+$/);
expect(result).toBe(true);
});
test('should fail custom regex validation', () => {
const result = validator.customValidation('abcde', /^\d+$/);
expect(result).toBe(false);
});
// Test Hex color
test('should invalidate an inappropriate hex color', () => {
const result = validator.checkHexColor('@t7841');
expect(result).toBe(false);
});
test('should validate a hex color', () => {
const result = validator.checkHexColor('#ffffff');
expect(result).toBe(true);
});
});
npm run test
Output
> regex-validify@3.2.0 test
> jest
PASS src/tests/regExp.test.ts
RegExpValidator
√ should validate a valid email (5 ms)
√ should invalidate an invalid email (1 ms)
√ should validate a valid IPv4 address (1 ms)
√ should invalidate an invalid IPv4 address (1 ms)
√ should validate a simple password (1 ms)
√ should invalidate a simple password if too short (1 ms)
√ should pass custom regex validation (1 ms)
√ should fail custom regex validation (1 ms)
√ should invalidate an inappropriate hex color (1 ms)
√ should validate a hex color (1 ms)
Test Suites: 1 passed, 1 total
Tests: 10 passed, 10 total
Snapshots: 0 total
Time: 4.825 s, estimated 6 s
Ran all test suites.
License
This project is licensed under the ISC License.
Acknowledgments
Special thanks to all contributors who have helped make this project better.
Made with ❤️ by Massachusetts