Validation
Comprehensive suite of validators for strings, numbers, arrays, objects, dates, patterns, and more. Includes type guards, range checks, format checks, and multi-validator helpers. All methods are fully typed.
API Summary
isPort(str: string | number): boolean- Checks if a value is a valid port number (1-65535).isEmail(str: string): boolean- Checks if a string is a valid email address.isUUID(str: string): boolean- Checks if a string is a valid UUID (versions 1-5).isURL(str: string, allowedProtocols = ['http', 'https', 'ws', 'wss']): boolean- Checks if a string is a valid URL.isPhone(str: string): boolean- Checks if a string is a valid international phone number.isAlphanumeric(str: string): boolean- Checks if a string contains only letters and numbers.isNumeric(value: string | number): boolean- Checks if a value is numeric.isHexColor(str: string): boolean- Checks if a string is a valid hex color code.isISODate(str: string): boolean- Checks if a string is a valid ISO date.isLengthBetween(str: string, min: number, max: number): boolean- Checks if a string's length is within a range.isNumberBetween(value: number, min: number, max: number): boolean- Checks if a number is within a range.isAlpha(str: string): boolean- Checks if a string contains only alphabetic characters.isStrongPassword(str: string): boolean- Checks if a string meets password complexity requirements.isIPv4(str: string): boolean- Checks if a string is a valid IPv4 address.isIPv6(str: string): boolean- Checks if a string is a valid IPv6 address.isCreditCard(str: string): boolean- Checks if a string is a valid credit card number.isValidJSON(str: string): boolean- Checks if a string is valid JSON.isArray<T>(value: unknown, itemGuard?: (item: unknown) => item is T): value is T[]- Checks if a value is an array.isBase64(str: string): boolean- Checks if a string is a valid base64 encoded string.hasRequiredProps(obj: Record<string, unknown>, requiredProps: string[]): boolean- Checks if an object has all required properties.isDateInRange(date: Date, minDate?: Date, maxDate?: Date): boolean- Checks if a date is within a range.matchesPattern(str: string, pattern: RegExp): boolean- Checks if a string matches a regex pattern.validateAll(value: unknown, validators: Array<(value: unknown) => boolean>): boolean- Validates a value against multiple validators.
Function Documentation & Usage Examples
isPort()
Checks if a value is a valid port number (1-65535).
Method Signature:
function isPort(value: string | number): boolean;
Parameters:
value: The value to check (string or number).
Returns:
trueif the value is a valid port number, otherwisefalse.
Example:
import { isPort } from '@catbee/utils/validation';
isPort(8080); // true
isPort('65536'); // false
isEmail()
Checks if a string is a valid email address.
Method Signature:
function isEmail(value: string): boolean;
Parameters:
value: The value to check (string).
Returns:
trueif the value is a valid email address, otherwisefalse.
Example:
import { isEmail } from '@catbee/utils/validation';
isEmail('user@example.com'); // true
isUUID()
Checks if a string is a valid UUID (versions 1-5).
Method Signature:
function isURL(str: string, allowedProtocols: string[] = ['http', 'https', 'ws', 'wss']): boolean;
Parameters:
str: The value to check (string).allowedProtocols: Array of allowed URL protocols (default:['http', 'https', 'ws', 'wss']).
Returns:
trueif the value is a valid UUID, otherwisefalse.
Example:
import { isUUID } from '@catbee/utils/validation';
isUUID('550e8400-e29b-41d4-a716-446655440000'); // true
isURL()
Checks if a string is a valid URL.
Method Signature:
function isURL(str: string, allowedProtocols: string[] = ['http', 'https', 'ws', 'wss']): boolean;
Parameters:
str: The value to check (string).allowedProtocols: Array of allowed URL protocols (default:['http', 'https', 'ws', 'wss']).
Returns:
trueif the value is a valid URL with an allowed protocol, otherwisefalse.
Example:
import { isURL } from '@catbee/utils/validation';
isURL('https://example.com'); // true
isURL('ftp://example.com'); // false (ftp not in default allowed protocols)
isURL('ftp://example.com', ['ftp']); // true (ftp is allowed)
isPhone()
Checks if a string is a valid international phone number.
Method Signature:
function isPhone(value: string): boolean;
Parameters:
value: The value to check (string).
Returns:
trueif the value is a valid international phone number, otherwisefalse.
Example:
import { isPhone } from '@catbee/utils/validation';
isPhone('+1-800-555-1234'); // true
isAlphanumeric()
Checks if a string contains only letters and numbers.
Method Signature:
function isAlphanumeric(value: string): boolean;
Parameters:
value: The value to check (string).
Returns:
trueif the value is alphanumeric, otherwisefalse.
Example:
import { isAlphanumeric } from '@catbee/utils/validation';
isAlphanumeric('abc123'); // true
isNumeric()
Checks if a value can be safely parsed to a number.
Method Signature:
function isNumeric(value: string | number): boolean;
Parameters:
value: The value to check (string or number).
Returns:
trueif the value can be safely parsed to a number, otherwisefalse.
Example:
import { isNumeric } from '@catbee/utils/validation';
isNumeric('42'); // true
isNumeric('abc'); // false
isHexColor()
Checks if a string is a valid hex color code.
Method Signature:
function isHexColor(value: string): boolean;
Parameters:
value: The value to check (string).
Returns:
trueif the value is a valid hex color code, otherwisefalse.
Example:
import { isHexColor } from '@catbee/utils/validation';
isHexColor('#fff'); // true
isHexColor('#123abc'); // true
isISODate()
Checks if a string is a valid ISO date.
Method Signature:
function isISODate(value: string): boolean;
Parameters:
value: The value to check (string).
Returns:
trueif the value is a valid ISO date, otherwisefalse.
Example:
import { isISODate } from '@catbee/utils/validation';
isISODate('2023-01-01T12:00:00Z'); // true
isLengthBetween()
Checks if a string's length is within a range.
Method Signature:
function isLengthBetween(str: string, min: number, max: number): boolean;
Parameters:
str: The string to check.min: Minimum length (inclusive).max: Maximum length (inclusive).
Returns:
trueif the string's length is within the range, otherwisefalse.
Example:
import { isLengthBetween } from '@catbee/utils/validation';
isLengthBetween('abc', 2, 5); // true
isNumberBetween()
Checks if a number is within a range.
Method Signature:
function isNumberBetween(value: number, min: number, max: number): boolean;
Parameters:
value: The number to check.min: Minimum value (inclusive).max: Maximum value (inclusive).
Returns:
trueif the number is within the range, otherwisefalse.
Example:
import { isNumberBetween } from '@catbee/utils/validation';
isNumberBetween(5, 1, 10); // true
isAlpha()
Checks if a string contains only alphabetic characters.
Method Signature:
function isAlpha(value: string): boolean;
Parameters:
value: The value to check (string).
Returns:
trueif the value contains only alphabetic characters, otherwisefalse.
Example:
import { isAlpha } from '@catbee/utils/validation';
isAlpha('abcDEF'); // true
isStrongPassword()
Checks if a string meets password complexity requirements.
Method Signature:
function isStrongPassword(value: string): boolean;
Parameters:
value: The value to check (string).
Returns:
trueif the value meets password complexity requirements, otherwisefalse.
Example:
import { isStrongPassword } from '@catbee/utils/validation';
isStrongPassword('Abc123!@#'); // true
isIPv4()
Checks if a string is a valid IPv4 address.
Method Signature:
function isIPv4(value: string): boolean;
Parameters:
value: The value to check (string).
Returns:
trueif the value is a valid IPv4 address, otherwisefalse.
Example:
import { isIPv4 } from '@catbee/utils/validation';
isIPv4('192.168.1.1'); // true
isIPv6()
Checks if a string is a valid IPv6 address.
Method Signature:
function isIPv6(value: string): boolean;
Parameters:
value: The value to check (string).
Returns:
trueif the value is a valid IPv6 address, otherwisefalse.
Example:
import { isIPv6 } from '@catbee/utils/validation';
isIPv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'); // true
isCreditCard()
Checks if a string is a valid credit card number (Luhn algorithm).
Method Signature:
function isCreditCard(value: string): boolean;
Parameters:
value: The value to check (string).
Returns:
trueif the value is a valid credit card number, otherwisefalse.
Example:
import { isCreditCard } from '@catbee/utils/validation';
isCreditCard('4111111111111111'); // true
isValidJSON()
Checks if a string is valid JSON.
Method Signature:
function isValidJSON(value: string): boolean;
Parameters:
value: The value to check (string).
Returns:
trueif the value is valid JSON, otherwisefalse.
Example:
import { isValidJSON } from '@catbee/utils/validation';
isValidJSON('{"a":1}'); // true
isArray()
Type guard for arrays, optionally checks element type.
Method Signature:
function isArray<T>(value: unknown, itemGuard?: (item: unknown) => item is T): value is T[];
Parameters:
value: The value to check (unknown).itemGuard: Optional type guard function for array elements.
Returns:
trueif the value is an array (and elements pass the guard if provided), otherwisefalse.
Example:
import { isArray } from '@catbee/utils/validation';
isArray([1, 2, 3], (x): x is number => typeof x === 'number'); // true
isBase64()
Checks if a string is a valid base64 encoded string.
Method Signature:
function isBase64(str: string): boolean;
Parameters:
str: The value to check (string).
Returns:
trueif the value is a valid base64 encoded string, otherwisefalse.
Example:
import { isBase64 } from '@catbee/utils/validation';
isBase64('SGVsbG8gV29ybGQ='); // true
isBase64('Hello World'); // false
hasRequiredProps()
Checks if an object has all required properties.
Method Signature:
function hasRequiredProps(obj: Record<string, unknown>, requiredProps: string[]): boolean;
Parameters:
obj: The object to check (Record<string, unknown>).requiredProps: An array of required property names (string[]).
Returns:
trueif the object has all required properties, otherwisefalse.
Example:
import { hasRequiredProps } from '@catbee/utils/validation';
hasRequiredProps({ a: 1, b: 2 }, ['a', 'b']); // true
isDateInRange()
Checks if a date is within a specified range.
Method Signature:
function isDateInRange(date: Date, minDate?: Date, maxDate?: Date): boolean;
Parameters:
date: The date to check (Date).minDate: The minimum date (Date, optional).maxDate: The maximum date (Date, optional).
Returns:
trueif the date is within the specified range, otherwisefalse.
Example:
import { isDateInRange } from '@catbee/utils/validation';
isDateInRange(new Date(), new Date('2020-01-01'), new Date('2030-01-01')); // true
matchesPattern()
Checks if a string matches a regular expression.
Method Signature:
function matchesPattern(str: string, pattern: RegExp): boolean;
Parameters:
str: The string to check (string).pattern: The regular expression pattern to match against (RegExp).
Returns:
trueif the string matches the pattern, otherwisefalse.
Example:
import { matchesPattern } from '@catbee/utils/validation';
matchesPattern('abc123', /^[a-z]+\d+$/); // true
validateAll()
Checks if a value passes all provided validators.
Method Signature:
function validateAll(value: unknown, validators: Array<(value: unknown) => boolean>): boolean;
Parameters:
value: The value to validate.validators: An array of validator functions to apply.
Returns:
trueif the value passes all validators, otherwisefalse.
Example:
import { validateAll, isAlpha } from '@catbee/utils/validation';
validateAll('abc', [isAlpha, str => str.length > 2]); // true