initValidate function

void initValidate(
  1. String identification,
  2. TypeIdentification typeIdentification
)

Validates a numbers used for Ecuadorian RUC or DNI numbers.

identification The identification number to validate. typeIdentification The type of identification being validated.

Throws an IdentificationException if the verification digit is invalid, the identification type is not supported, or if any part of the identification is not a number.

Implementation

void initValidate(
  String identification,
  TypeIdentification typeIdentification,
) {
  if (identification.isEmpty || identification.trim().isEmpty) {
    throw IdentificationException(
      ErrorCode.invalidEmpty,
      'Identification cannot be empty',
    );
  }

  final ruler = mapRules[typeIdentification];

  if (ruler == null) {
    throw IdentificationException(
      ErrorCode.invalidType,
      'Invalid identification type',
    );
  }

  final regExp = RegExp(ruler.pattern);

  if (!regExp.hasMatch(identification)) {
    throw IdentificationException(
      ErrorCode.invalidLengthOrFormat,
      ruler.errorMessage,
    );
  }
}