UNPKG

react-native-country-documents-validator

Version:

Comprehensive global country document validator for React Native with 155+ countries

105 lines (104 loc) 3.95 kB
/** * Country Validator Utility - Scalable Rule-Based System * * A robust, highly accurate validator for national IDs, passports, and alien cards * across different countries. Uses a rule-based approach for maximum scalability. * * Features: * - High accuracy with official formatting standards * - Rule-based system for easy country additions * - TypeScript support with strong typing * - Lightweight with no external dependencies * - Comprehensive validation including checksums where applicable * - All African countries supported */ export interface ValidationResult { isValid: boolean; errorMessage?: string; normalizedValue?: string; } export interface CountryCode { readonly alpha2: string; readonly alpha3: string; readonly name: string; } export type DocumentType = "NATIONAL_ID" | "PASSPORT" | "ALIEN_CARD"; export interface DocumentRule { pattern: RegExp; minLength?: number; maxLength?: number; exactLength?: number; prefix?: string[]; description: string; format?: (value: string) => string; customValidator?: (value: string) => ValidationResult | null; } export interface CountryRules { nationalId?: DocumentRule; passport?: DocumentRule; alienCard?: DocumentRule; } export interface CountryValidationConfig { country: CountryCode; rules: CountryRules; } export declare const COUNTRIES: Record<string, CountryCode>; /** * Get available countries */ export declare function getSupportedCountries(): CountryCode[]; /** * Get supported document types for a country */ export declare function getSupportedDocumentTypes(countryCode: string): DocumentType[]; /** * Validate a passport number for a specific country */ export declare function validatePassport(countryCode: string, value: string): ValidationResult; /** * Validate a national ID for a specific country */ export declare function validateNationalId(countryCode: string, value: string): ValidationResult; /** * Validate an alien card for a specific country */ export declare function validateAlienCard(countryCode: string, value: string): ValidationResult; /** * Generic document validation function */ export declare function validateDocument(countryCode: string, documentType: DocumentType, value: string): ValidationResult; /** * Format a document number according to country standards */ export declare function formatDocument(countryCode: string, documentType: DocumentType, value: string): string; /** * Get validation description for a document type in a country */ export declare function getValidationDescription(countryCode: string, documentType: DocumentType): string | null; /** * Register a new country validator (for extensibility) */ export declare function registerCountryValidator(countryCode: string, config: CountryValidationConfig): void; /** * Utility function to check if a country is supported */ export declare function isCountrySupported(countryCode: string): boolean; /** * Legacy compatibility function * @deprecated Use the new validateDocument function instead */ export declare function validationInputPair(value: string, idType: "ID" | "PASSPORT" | "ALIEN_CARD", issuingCountry?: string): boolean; declare const _default: { validatePassport: typeof validatePassport; validateNationalId: typeof validateNationalId; validateAlienCard: typeof validateAlienCard; validateDocument: typeof validateDocument; formatDocument: typeof formatDocument; getSupportedCountries: typeof getSupportedCountries; getSupportedDocumentTypes: typeof getSupportedDocumentTypes; getValidationDescription: typeof getValidationDescription; isCountrySupported: typeof isCountrySupported; registerCountryValidator: typeof registerCountryValidator; COUNTRIES: Record<string, CountryCode>; }; export default _default;