facturacionelectronicapy-ts-xmlgen
Version:
Genera el contenido del archivo XML del Documento electrónico exigido por la SET
75 lines (74 loc) • 2.35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const zod_1 = require("zod");
class NumberLength {
ctx;
intPart;
decimalPart;
path;
constructor(value, ctx) {
this.ctx = ctx;
const [intPart, decimalPart] = value.toString().split('.');
if (value < 0) {
this.addError('No se admiten números negativos');
}
this.path = ctx.path.join('.');
this.intPart = intPart;
this.decimalPart = decimalPart ?? '';
}
addError(message) {
this.ctx.addIssue({
code: zod_1.z.ZodIssueCode.custom,
message,
path: this.ctx.path,
});
}
/** No es necesario si el mínimo es menor o igual a 1 */
int(message) {
if (!this.intPart.length || this.decimalPart.length) {
this.addError(message ?? `El campo '${this.path}' debe ser un número entero`);
}
return this;
}
min(min, message) {
if (this.intPart.length < min) {
this.addError(message ??
`El campo '${this.path}' debe tener al menos ${min} dígitos enteros`);
}
return this;
}
max(max, message) {
if (this.intPart.length > max) {
this.addError(message ??
`El campo '${this.path}' no puede tener más de ${max} dígitos enteros`);
}
return this;
}
length(len, message) {
if (this.intPart.length != len) {
this.addError(message ?? `El campo '${this.path}' debe tener ${len} dígitos enteros`);
}
return this;
}
minDecimals(min, message) {
if (this.decimalPart.length < min) {
this.addError(message ??
`El campo '${this.path}' debe tener al menos ${min} decimales`);
}
return this;
}
maxDecimals(max, message) {
if (this.decimalPart.length > max) {
this.addError(message ??
`El campo '${this.path}' no puede tener más de ${max} decimales`);
}
return this;
}
decimalsLength(length, message) {
if (this.decimalPart.length != length) {
this.addError(message ?? `El campo '${this.path}' debe tener ${length} decimales`);
}
return this;
}
}
exports.default = NumberLength;