ec-validator-dni
Version:
Validation of Ecuadorian identification documents (ID card and RUC)
268 lines (257 loc) • 9.1 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
TypeIdentification: () => TypeIdentification,
dniValidation: () => dniValidation,
isPossiblyValidRuc: () => isPossiblyValidRuc,
validateRuc: () => validateRuc,
validateRucByType: () => validateRucByType
});
module.exports = __toCommonJS(index_exports);
// src/interfaces/identification.ts
var TypeIdentification = /* @__PURE__ */ ((TypeIdentification2) => {
TypeIdentification2["DNI"] = "DNI";
TypeIdentification2["RUC"] = "RUC";
TypeIdentification2["RUC_PERSON_NATURAL"] = "RUC_PERSON_NATURAL";
TypeIdentification2["RUC_SOCIETY_PRIVATE"] = "RUC_SOCIETY_PRIVATE";
TypeIdentification2["RUC_PUBLIC_SOCIETY"] = "RUC_PUBLIC_SOCIETY";
TypeIdentification2["POSSIBLY_VALID_RUC"] = "POSSIBLY_VALID_RUC";
return TypeIdentification2;
})(TypeIdentification || {});
// src/helpers/initValidate.ts
var initValidate = (identification, typeIdentification) => {
if (!identification || !identification.trim().length) {
throw new Error("Identification not cant be empty");
}
const rule = VALIDATION_RULES[typeIdentification];
if (!rule) {
throw new Error("Identification type not valid");
}
if (!rule.pattern.test(identification)) {
throw new Error(rule.errorMessage);
}
};
var VALIDATION_RULES = {
["DNI" /* DNI */]: {
pattern: /^\d{10}$/,
errorMessage: "Identification not valid min 10 digits, max 10 digits and only numbers"
},
["RUC" /* RUC */]: {
pattern: /^\d{13}$/,
errorMessage: "Identification not valid min 13 digits, max 13 digits and only numbers"
},
["RUC_PERSON_NATURAL" /* RUC_PERSON_NATURAL */]: {
pattern: /^\d{13}$/,
errorMessage: "Identification not valid min 13 digits, max 13 digits and only numbers"
},
["RUC_SOCIETY_PRIVATE" /* RUC_SOCIETY_PRIVATE */]: {
pattern: /^\d{13}$/,
errorMessage: "Identification not valid min 13 digits, max 13 digits and only numbers"
},
["RUC_PUBLIC_SOCIETY" /* RUC_PUBLIC_SOCIETY */]: {
pattern: /^\d{13}$/,
errorMessage: "Identification not valid min 13 digits, max 13 digits and only numbers"
}
};
// src/helpers/validateCodeProvince.ts
var validateCodeProvince = (provinceCode) => {
const code = parseInt(provinceCode);
if (isNaN(code)) {
throw new Error("Invalid province code (first 2 digits) must be a number and between 00 - 24 and 30 for foreigners");
}
if (code === 30) return;
if (code < 0 || code > 24) {
throw new Error("Invalid province code (first 2 digits) must be between 00 - 24 and 30 for foreigners");
}
};
// src/helpers/validateThirdDigit.ts
var validateThirdDigit = (thirdDigit, typeIdentification) => {
const code = parseInt(thirdDigit);
if (isNaN(code)) {
throw new Error("Invalid third digit must be a number");
}
switch (typeIdentification) {
case "DNI" /* DNI */:
case "RUC_PERSON_NATURAL" /* RUC_PERSON_NATURAL */:
if (code < 0 || code > 9) {
throw new Error("Invalid third digit must be between 0 and 9");
}
break;
case "RUC_SOCIETY_PRIVATE" /* RUC_SOCIETY_PRIVATE */:
if (code !== 9) {
throw new Error("Invalid third digit must be 9");
}
break;
case "RUC_PUBLIC_SOCIETY" /* RUC_PUBLIC_SOCIETY */:
if (code !== 6) {
throw new Error("Invalid third digit must be 6");
}
break;
default:
throw new Error("Invalid identification type");
}
};
// src/helpers/validateCodeEstablishment.ts
var validateCodeEstablishment = (code) => {
const codeInt = parseInt(code);
if (isNaN(codeInt)) {
throw new Error("Invalid code establishment must be a number");
}
if (codeInt <= 0) {
throw new Error("Invalid code establishment must be greater than 0");
}
};
// src/helpers/algorithm10.ts
var algorithm10 = (firstDigits, verificationDigitString) => {
const verificationDigit = parseInt(verificationDigitString);
let total = 0;
const listCoefficients = [2, 1, 2, 1, 2, 1, 2, 1, 2];
for (let i = 0; i < listCoefficients.length; i++) {
const coefficient = listCoefficients[i];
const digit = parseInt(firstDigits[i]);
let result = digit * coefficient;
if (result >= 10) {
result -= 9;
}
total += result;
}
const residue = total % 10;
let verificationDigitResult = 0;
if (residue !== 0) {
verificationDigitResult = 10 - residue;
}
if (verificationDigitResult !== verificationDigit) {
throw new Error("Invalid verification digit");
}
return true;
};
// src/helpers/algorithm11.ts
var algorithm11 = (initialDigits, verificationDigitString, typeIdentification) => {
const coefficients = COEFFICIENTS_RUC[typeIdentification];
if (!coefficients) {
throw new Error("Invalid identification type");
}
const verifierNumber = parseInt(verificationDigitString);
const digits = initialDigits.split("").map(Number);
if (digits.length !== coefficients.length) {
throw new Error("Digits length must be equal to coefficients length");
}
const total = digits.reduce((sum, digit, index) => {
return sum + digit * coefficients[index];
}, 0);
const remainder = total % 11;
const result = remainder === 0 ? 0 : 11 - remainder;
if (result !== verifierNumber) {
throw new Error("Invalid verification digit");
}
return true;
};
var COEFFICIENTS_RUC = {
["RUC_SOCIETY_PRIVATE" /* RUC_SOCIETY_PRIVATE */]: [4, 3, 2, 7, 6, 5, 4, 3, 2],
["RUC_PUBLIC_SOCIETY" /* RUC_PUBLIC_SOCIETY */]: [3, 2, 7, 6, 5, 4, 3, 2]
};
// src/validations/dniValidation.ts
var dniValidation = (dni) => {
try {
initValidate(dni, "DNI" /* DNI */);
validateCodeProvince(dni.substring(0, 2));
validateThirdDigit(dni.substring(2, 3), "DNI" /* DNI */);
algorithm10(dni.substring(0, 9), dni.substring(9, 10));
} catch (error) {
return {
isValid: false,
errorMessage: error.message
};
}
return {
isValid: true
};
};
// src/validations/rucValidations.ts
var isPossiblyValidRuc = (ruc) => {
try {
initValidate(ruc, "RUC" /* RUC */);
validateCodeProvince(ruc.substring(0, 2));
return {
isValid: true
};
} catch (error) {
return {
isValid: false,
errorMessage: (error == null ? void 0 : error.message) || "Invalid RUC"
};
}
};
var validateRucByType = (ruc, typeIdentification) => {
try {
initValidate(ruc, "RUC" /* RUC */);
validateCodeProvince(ruc.substring(0, 2));
switch (typeIdentification) {
case "RUC_PERSON_NATURAL" /* RUC_PERSON_NATURAL */:
validateThirdDigit(ruc.substring(2, 3), typeIdentification);
validateCodeEstablishment(ruc.substring(10, 13));
algorithm10(ruc.substring(0, 9), ruc.substring(9, 10));
break;
case "RUC_SOCIETY_PRIVATE" /* RUC_SOCIETY_PRIVATE */:
validateThirdDigit(ruc.substring(2, 3), typeIdentification);
validateCodeEstablishment(ruc.substring(10, 13));
algorithm11(ruc.substring(0, 9), ruc.substring(9, 10), typeIdentification);
break;
case "RUC_PUBLIC_SOCIETY" /* RUC_PUBLIC_SOCIETY */:
validateThirdDigit(ruc.substring(2, 3), typeIdentification);
validateCodeEstablishment(ruc.substring(9, 13));
algorithm11(ruc.substring(0, 8), ruc.substring(8, 9), typeIdentification);
break;
default:
break;
}
} catch (error) {
return {
isValid: false,
errorMessage: (error == null ? void 0 : error.message) || "Invalid RUC"
};
}
return {
isValid: true
};
};
var validateRuc = (ruc) => {
const validRucNatural = validateRucByType(ruc, "RUC_PERSON_NATURAL" /* RUC_PERSON_NATURAL */);
const validRucPrivate = validateRucByType(ruc, "RUC_SOCIETY_PRIVATE" /* RUC_SOCIETY_PRIVATE */);
const validRucPublic = validateRucByType(ruc, "RUC_PUBLIC_SOCIETY" /* RUC_PUBLIC_SOCIETY */);
if (!validRucNatural.isValid && !validRucPrivate.isValid && !validRucPublic.isValid) {
return {
isValid: false,
errorMessage: validRucNatural.errorMessage || validRucPrivate.errorMessage || validRucPublic.errorMessage || "Invalid RUC"
};
}
return {
isValid: true
};
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
TypeIdentification,
dniValidation,
isPossiblyValidRuc,
validateRuc,
validateRucByType
});