UNPKG

houser-js-utils

Version:

A comprehensive collection of TypeScript utility functions for common development tasks including array manipulation, string processing, date handling, random number generation, validation, and much more.

248 lines (247 loc) 9.93 kB
/** * @module ValidationUtils * @description A comprehensive collection of validation functions for common data types and formats. * @example * ```typescript * import { ValidationUtils } from 'houser-js-utils'; * * // Validate email address * const isValid = ValidationUtils.isEmail('user@example.com'); // true * * // Validate credit card number * const isValidCard = ValidationUtils.isCreditCard('4111111111111111'); // true * * // Validate password strength * const isStrongPassword = ValidationUtils.isPassword('MyP@ssw0rd123'); // true * ``` */ /** * Validation utility functions for common data types and formats */ export declare const ValidationUtils: { /** * Checks if a value is a valid base64 encoded string. * @param base64 - The base64 string to validate * @returns True if the string is valid base64, false otherwise * @example * ```typescript * ValidationUtils.isBase64('SGVsbG8gV29ybGQ='); // Returns true * ValidationUtils.isBase64('invalid-base64'); // Returns false * ``` */ isBase64(base64: string): boolean; /** * Validates a credit card number using the Luhn algorithm. * @param number - The credit card number to validate (can include spaces, dashes, etc.) * @returns True if the credit card number is valid, false otherwise * @example * ```typescript * ValidationUtils.isCreditCard('4111111111111111'); // Returns true (Visa test number) * ValidationUtils.isCreditCard('4111-1111-1111-1111'); // Returns true (with dashes) * ValidationUtils.isCreditCard('1234567890123456'); // Returns false (invalid) * ``` */ isCreditCard(number: string): boolean; /** * Validates a credit card CVV (Card Verification Value). * @param cvv - The CVV to validate (3 or 4 digits) * @returns True if the CVV format is valid, false otherwise * @example * ```typescript * ValidationUtils.isCreditCardCvv('123'); // Returns true * ValidationUtils.isCreditCardCvv('1234'); // Returns true (Amex) * ValidationUtils.isCreditCardCvv('12'); // Returns false (too short) * ``` */ isCreditCardCvv(cvv: string): boolean; /** * Validates a credit card expiry date in MM/YY format. * @param expiry - The expiry date to validate in MM/YY format * @returns True if the expiry date is valid and not in the past, false otherwise * @example * ```typescript * ValidationUtils.isCreditCardExpiry('12/25'); // Returns true (if current date is before Dec 2025) * ValidationUtils.isCreditCardExpiry('01/20'); // Returns false (expired) * ValidationUtils.isCreditCardExpiry('13/25'); // Returns false (invalid month) * ``` */ isCreditCardExpiry(expiry: string): boolean; /** * Validates a domain name format. * @param domain - The domain name to validate * @returns True if the domain name format is valid, false otherwise * @example * ```typescript * ValidationUtils.isDomain('example.com'); // Returns true * ValidationUtils.isDomain('sub.example.org'); // Returns true * ValidationUtils.isDomain('invalid..domain'); // Returns false * ``` */ isDomain(domain: string): boolean; /** * Validates an email address format. * @param email - The email address to validate * @returns True if the email format is valid, false otherwise * @example * ```typescript * ValidationUtils.isEmail('user@example.com'); // Returns true * ValidationUtils.isEmail('test.email+tag@domain.co.uk'); // Returns true * ValidationUtils.isEmail('invalid-email'); // Returns false * ``` */ isEmail(email: string): boolean; /** * Validates a hostname format. * @param hostname - The hostname to validate * @returns True if the hostname format is valid, false otherwise * @example * ```typescript * ValidationUtils.isHostname('localhost'); // Returns true * ValidationUtils.isHostname('web-server-01'); // Returns true * ValidationUtils.isHostname('invalid_hostname'); // Returns false * ``` */ isHostname(hostname: string): boolean; /** * Checks if a value is empty (null, undefined, empty string, empty array, or empty object). * @param value - The value to check for emptiness * @returns True if the value is considered empty, false otherwise * @example * ```typescript * ValidationUtils.isEmpty(null); // Returns true * ValidationUtils.isEmpty(''); // Returns true * ValidationUtils.isEmpty([]); // Returns true * ValidationUtils.isEmpty({}); // Returns true * ValidationUtils.isEmpty('hello'); // Returns false * ``` */ isEmpty(value: unknown): boolean; /** * Validates an IP address (IPv4 or IPv6). * @param ip - The IP address to validate * @returns True if the IP address format is valid, false otherwise * @example * ```typescript * ValidationUtils.isIpAddress('192.168.1.1'); // Returns true (IPv4) * ValidationUtils.isIpAddress('2001:0db8:85a3:0000:0000:8a2e:0370:7334'); // Returns true (IPv6) * ValidationUtils.isIpAddress('256.1.1.1'); // Returns false (invalid IPv4) * ``` */ isIpAddress(ip: string): boolean; /** * Validates if a string is valid JSON. * @param json - The JSON string to validate * @returns True if the string is valid JSON, false otherwise * @example * ```typescript * ValidationUtils.isJson('{"name": "John", "age": 30}'); // Returns true * ValidationUtils.isJson('[1, 2, 3]'); // Returns true * ValidationUtils.isJson('invalid json'); // Returns false * ``` */ isJson(json: string): boolean; /** * Validates a MIME type format. * @param mimeType - The MIME type to validate * @returns True if the MIME type format is valid, false otherwise * @example * ```typescript * ValidationUtils.isMimeType('text/html'); // Returns true * ValidationUtils.isMimeType('application/json'); // Returns true * ValidationUtils.isMimeType('invalid-mime'); // Returns false * ``` */ isMimeType(mimeType: string): boolean; /** * Validates password strength based on configurable criteria. * @param password - The password to validate * @param options - Password validation options * @param options.minLength - Minimum password length (default: 8) * @param options.requireUppercase - Require uppercase letters (default: true) * @param options.requireLowercase - Require lowercase letters (default: true) * @param options.requireNumbers - Require numbers (default: true) * @param options.requireSpecialChars - Require special characters (default: true) * @returns True if the password meets all criteria, false otherwise * @example * ```typescript * ValidationUtils.isPassword('MyP@ssw0rd123'); // Returns true * ValidationUtils.isPassword('weak', { minLength: 4, requireSpecialChars: false }); // Returns false * ValidationUtils.isPassword('StrongPass123!'); // Returns true * ``` */ isPassword(password: string, options?: { minLength?: number; requireUppercase?: boolean; requireLowercase?: boolean; requireNumbers?: boolean; requireSpecialChars?: boolean; }): boolean; /** * Validates a phone number format. * @param phone - The phone number to validate * @returns True if the phone number format is valid, false otherwise * @example * ```typescript * ValidationUtils.isPhoneNumber('+1 (555) 123-4567'); // Returns true * ValidationUtils.isPhoneNumber('555-123-4567'); // Returns true * ValidationUtils.isPhoneNumber('123'); // Returns false (too short) * ``` */ isPhoneNumber(phone: string): boolean; /** * Checks if a value is a valid port number * @param port - Port number to validate * @returns True if valid, false otherwise */ isPort(port: number): boolean; /** * Checks if a value is a valid postal code * @param postalCode - Postal code to validate * @param country - Country code (ISO 3166-1 alpha-2) * @returns True if valid, false otherwise */ isPostalCode(postalCode: string, country: string): boolean; /** * Checks if a value is a valid semver version * @param version - Version string to validate * @returns True if valid, false otherwise */ isSemver(version: string): boolean; /** * Checks if a value is a valid social security number * @param ssn - Social security number to validate * @param country - Country code (ISO 3166-1 alpha-2) * @returns True if valid, false otherwise */ isSocialSecurityNumber(ssn: string, country: string): boolean; /** * Checks if a value is a valid tax identification number * @param tin - Tax identification number to validate * @param country - Country code (ISO 3166-1 alpha-2) * @returns True if valid, false otherwise */ isTaxIdentificationNumber(tin: string, country: string): boolean; /** * Checks if a value is a valid URL * @param url - URL to validate * @returns True if valid, false otherwise */ isUrl(url: string): boolean; /** * Checks if a value is a valid username * @param username - Username to validate * @param options - Username validation options * @returns True if valid, false otherwise */ isUsername(username: string, options?: { minLength?: number; maxLength?: number; allowSpecialChars?: boolean; }): boolean; /** * Checks if a value is a valid UUID * @param uuid - UUID to validate * @returns True if valid, false otherwise */ isUuid(uuid: string): boolean; };