UNPKG

ajt-validator

Version:

Validation library for JavaScript and TypeScript

128 lines (127 loc) 4.5 kB
import { BaseValidator } from "../base"; import { ValidationResult } from "../../interfaces"; /** * Standard passport issuing countries/authorities * ISO 3166-1 alpha-3 country codes for most common passport issuers */ export declare enum PassportAuthority { USA = "USA", GBR = "GBR", CAN = "CAN", AUS = "AUS", NZL = "NZL", DEU = "DEU",// Germany FRA = "FRA", ESP = "ESP",// Spain ITA = "ITA", JPN = "JPN", CHN = "CHN", IND = "IND", RUS = "RUS", BRA = "BRA", ZAF = "ZAF" } /** * Common passport formats by country/authority * Maps country codes to regex patterns for their passport numbers */ export declare const PASSPORT_FORMATS: Record<string, RegExp>; /** * Options for configuring passport validation */ export interface PassportValidatorOptions { /** List of allowed issuing authorities (countries) */ allowedAuthorities?: string[]; /** Whether passport numbers should be normalized (e.g., uppercase, trimmed) */ normalize?: boolean; /** Whether to validate expiration date */ validateExpiration?: boolean; /** Minimum validity period (in days) if checking expiration */ minimumValidityDays?: number; /** Additional formats for passport validation */ additionalFormats?: Record<string, RegExp>; /** Whether to check for checksums/validation digits (when supported) */ validateChecksums?: boolean; /** Maximum age of passport (in years) */ maxPassportAge?: number; /** Whether to allow unknown authorities (using generic validation) */ allowUnknownAuthorities?: boolean; } /** * Result of passport validation with additional metadata */ export interface PassportValidationResult extends ValidationResult<string> { /** The recognized issuing authority/country if identified */ authority?: string; /** Whether the passport has sufficient remaining validity */ hasValidExpiration?: boolean; /** Days remaining until expiration (if validateExpiration is true) */ daysToExpiration?: number; /** The normalized passport number (if normalization is enabled) */ normalizedValue?: string; /** Whether the passport passed checksum validation */ checksumValid?: boolean; } /** * Validator for passport number fields * Supports various passport formats and validation rules by country */ export declare class PassportValidator extends BaseValidator<string, string> { private options; private formats; /** * Create a new passport validator * @param options Configuration options */ constructor(options?: PassportValidatorOptions); /** * Validate a passport number and optional expiration date * @param value Passport number or object with number and expiration * @returns Validation result with additional metadata */ validate(value: any): PassportValidationResult; /** * Attempt to detect the issuing authority from passport number format * @param passportNumber Normalized passport number * @returns Detected authority code or undefined */ private detectAuthority; /** * Validate passport number format against country-specific pattern * @param passportNumber Normalized passport number * @param authority Authority code to validate against * @returns Whether the format is valid */ private validateFormat; /** * Validate passport number checksum (when supported) * Note: Actual checksum validation varies by country and may require * complex algorithms that are only available to official systems * * @param passportNumber Normalized passport number * @param authority Authority code * @returns Boolean if supported, undefined if not supported */ private validateChecksum; /** * Example checksum validation method (simplified) * @param value String value to check * @returns Whether the checksum is valid */ private checkAlphanumericSum; /** * Example checksum for numeric values (simplified) * @param value String of digits * @returns Whether the checksum is valid */ private checkDigitSum; /** * Get all supported passport authorities * @returns List of supported authority codes */ getSupportedAuthorities(): string[]; /** * Create an error result for passport validation */ protected createError(code: string, message: string): PassportValidationResult; }