UNPKG

nigerian-mobile-validator

Version:

The most rigorous, up-to-date library for validating Nigerian mobile numbers. Fully NCC-compliant, and security-focused, with enterprise-grade features to prevent the business risks of validation failures in regulated industries.

94 lines (93 loc) 3.19 kB
import { MobileNumberValidationResult } from './mobile-number-validation-result'; import { ILogger } from '../logging/i-logger'; import { TypingDirection } from './typing-direction'; /** * Options for the Nigerian Mobile Number Validator */ export interface ValidatorOptions { /** * Logger instance to use for logging */ logger?: ILogger; /** * Maximum number of validations per minute (0 = unlimited) */ rateLimit?: number; } /** * This class validates Nigerian mobile numbers in strict compliance with the * official Nigerian National Numbering Plan. * * - Numbering plan last updated: March 2025 */ export declare class NigerianMobileNumberValidator { private readonly emitter; private readonly mobileNumberingPlan; private readonly validationTriggeringFlags; private readonly logger; private readonly rateLimiter?; private currentTypingDirection; /** * Create a new Nigerian Mobile Number Validator * * @param options Validator options */ constructor(options?: ValidatorOptions); /** * Register a listener for validation results * * @param callback Function to call with validation results * @returns Function to remove the listener */ onValidationResult(callback: (result: MobileNumberValidationResult) => void): () => void; /** * Publish a validation result and emit it to listeners */ private publishValidationResult; /** * Updates internal flags after validation to help determine * when to trigger validation on future input. */ private updateValidationTriggeringFlags; /** * Check if validation is allowed based on rate limiting */ private checkHasExceededRateLimit; /** * Determines if there are enough digits to perform validation */ private areThereEnoughDigitsToValidate; /** * Sanitize user input by removing spaces, plus signs, and fixing common errors */ static sanitizeUserProvidedMobileNumber(userProvidedDigits: string): string; private set usersTypingDirection(value); /** * Gets an enum representing the direction in which the user is currently typing. */ get usersTypingDirection(): TypingDirection; /** * Truthy method to quickly check if a phone number is not a Nigerian mobile number. * This is a fast pre-validation step to reject obviously foreign numbers. * * @param sanitizedUserInput The phone number to check * @returns true if the number looks foreign, false if it could be Nigerian */ private isForeignNumber; /** * Validates a Nigerian mobile number in strict compliance with the official * Nigerian National Numbering Plan. * * @param userProvidedDigits The characters input by the user representing their mobile number * @returns A MobileNumberValidationResult representing the validation result */ validate(userProvidedDigits?: string): MobileNumberValidationResult; /** * Clean up resources */ dispose(): void; /** * Represents a mobile phone number. */ private static readonly MobileNumber; }