3.2.3 • Published 6 months ago

regex-validify v3.2.3

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

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 addresses
  • checkIPv4(ip: string) - IPv4 addresses
  • checkIPv6(ip: string) - IPv6 addresses
  • checkMacAddress(mac: string) - MAC addresses
  • checkUrl(url: string) - URLs
  • checkDomain(domain: string) - Domain names
  • checkUsername(name: string) - Usernames (3-16 characters)
  • checkHexColor(color: string) - Hex color codes
  • checkOnlyIntegers(value: string) - Integer numbers
  • checkOnlyAlpha(value: string) - Alphabetic characters
  • checkEmoji(value: string) - Emoji characters
  • checkAnsiArt(value: string) - ANSI art characters
  • checkJwt(jwt: string) - JWT tokens
  • checkSemver(version: string) - Semantic versions
  • checkPhoneNumber(phone: string) - Phone numbers
  • checkCreditCard(card: string) - Credit card numbers
  • checkSSN(ssn: string) - Social Security Numbers
  • checkISBN(isbn: string) - ISBN numbers
  • checkTime(time: string) - Time strings (12/24 hour format)

Complex Validators

  • checkPassword(password: string, level: ValidationLevel) - Password strength with three levels:
    • simple: Minimum 6 characters
    • medium: Minimum 8 characters
    • complex: 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

3.2.2

6 months ago

3.0.4

7 months ago

3.2.1

6 months ago

3.0.3

8 months ago

3.2.0

6 months ago

3.1.1

6 months ago

3.0.2

8 months ago

3.1.0

7 months ago

3.0.1

8 months ago

3.1.7

6 months ago

3.1.6

6 months ago

3.1.5

6 months ago

3.2.3

6 months ago

3.0.0

9 months ago

2.0.0

9 months ago

1.1.0

9 months ago

1.0.0

10 months ago