UNPKG

ajt-validator

Version:

Validation library for JavaScript and TypeScript

100 lines (99 loc) 2.94 kB
/** * URL Validator module * A comprehensive tool for validating, parsing, and analyzing URLs */ /** * Configuration options for URLValidator */ export interface URLValidatorOptions { /** List of allowed protocols (e.g., ['http:', 'https:']) */ allowedProtocols?: string[]; /** List of allowed domains. If null, all domains are allowed */ allowedDomains?: string[] | null; /** Whether to allow subdomains of allowed domains */ allowSubdomains?: boolean; /** Whether to allow IP addresses */ allowIPAddresses?: boolean; /** Whether to allow authentication in URLs */ allowAuth?: boolean; /** Whether to allow URL fragments */ allowFragment?: boolean; /** Whether a path is required */ requirePath?: boolean; /** Regular expression pattern that the path must match */ pathPattern?: RegExp | null; /** List of query parameters that must be present */ requiredQueryParams?: string[]; /** List of allowed query parameters. If null, all are allowed */ allowedQueryParams?: string[] | null; /** List of disallowed port numbers */ disallowPorts?: number[]; /** Default error message */ errorMessage?: string; } /** * Interface for parsed URL components */ export interface ParsedURL { protocol: string; username?: string; password?: string; hostname: string; port: number | null; path: string; query: string; fragment: string; isIPAddress: boolean; queryParams: [string, string][]; } /** * Class for validating and parsing URLs */ export declare class URLValidator { private options; private lastError; /** * Constructor for URLValidator * @param options - Configuration options */ constructor(options?: URLValidatorOptions); /** * Validate a URL string against configured rules * @param url - The URL to validate * @returns Whether the URL is valid */ validate(url: string): boolean; /** * Get the error message from the last validation * @returns The error message or null if no error */ getErrorMessage(): string; /** * Parse a URL into its components * @param url - The URL to parse * @returns Parsed URL components or null if invalid */ parseURL(url: string): ParsedURL | null; /** * Normalize a URL * @param url - URL to normalize * @returns Normalized URL */ normalizeURL(url: string): string; /** * Parse query string into array of key-value pairs * @param queryString - The query string to parse * @returns Array of key-value pairs */ private _parseQueryParams; /** * Check if a string is an IP address * @param str - String to check * @returns Whether the string is an IP address */ private _isIpAddress; } declare const _default: { URLValidator: typeof URLValidator; }; export default _default;