UNPKG

@lacussoft/cpf-val

Version:

Utility to validate CPF (Brazilian Individual's Taxpayer ID)

89 lines (83 loc) 2.97 kB
/** * Lacus Solutions :: cpf-val v3.0.0 * * @author Julio L. Muller. * @license MIT - 2021-2026 */ /** * Valid input types for CPF validation. * * A CPF may be given as: * * - A string of numeric characters (with or without formatting). * - An array of strings, each representing one or more numeric characters and/or * punctuation. */ type CpfInput = readonly string[] | string; /** * Helper function to simplify the usage of the {@link CpfValidator} class. * * @throws {CpfValidatorInputTypeError} If input is not string or string[]. */ declare function cpfVal(cpfInput: CpfInput): boolean; /** * The standard length of a CPF (Cadastro de Pessoa Física) identifier (11 * digits). */ declare const CPF_LENGTH = 11; /** * Validator for CPF (Cadastro de Pessoa Física) identifiers. Validates CPF * strings according to the Brazilian CPF validation algorithm. */ declare class CpfValidator { /** * Validates a CPF input. * * @throws {CpfValidatorInputTypeError} If input is not string or string[]. */ isValid(cpfInput: CpfInput): boolean; /** * Normalizes the input to a string. * * @throws {CpfValidatorInputTypeError} If the input is not a string or array * of strings. */ private _toStringInput; } /** * Base error class for all `cpf-val` type-related errors. * * This abstract class extends the native `TypeError` and serves as the base for * all type validation errors in the CPF validator. It ensures proper prototype * chain setup and automatically sets the error name from the constructor. */ declare abstract class CpfValidatorTypeError extends TypeError { readonly name: string; readonly actualInput: unknown; readonly actualType: string; readonly expectedType: string; constructor(actualInput: unknown, actualType: string, expectedType: string, message: string); } /** * Error raised when the input provided to the CPF validator is not of the * expected type {@link CpfInput}. The error message includes both the actual * input type and the expected type. */ declare class CpfValidatorInputTypeError extends CpfValidatorTypeError { constructor(actualInput: unknown, expectedType: string); } /** * Base exception for all `cpf-val` rules-related errors. * * This abstract class extends the native `Error` and serves as the base for all * non-type-related errors in the `CpfValidator` and its dependencies. It is * suitable for validation errors, range errors, and other business logic * exceptions that are not strictly type-related. It ensures proper prototype * chain setup and automatically sets the error name from the constructor. */ declare abstract class CpfValidatorException extends Error { readonly name: string; constructor(message: string); } export { CPF_LENGTH, CpfValidator, CpfValidatorException, CpfValidatorInputTypeError, CpfValidatorTypeError, cpfVal, cpfVal as default }; export type { CpfInput };