facturacionelectronicapy-ts-xmlgen
Version:
Genera el contenido del archivo XML del Documento electrónico exigido por la SET
57 lines (56 loc) • 2.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const zod_1 = require("zod");
const Path_1 = require("../Path");
/** ZodValidator */
class ZodValidator {
ctx;
object;
constructor(ctx, object) {
this.ctx = ctx;
this.object = object;
}
getPathString(fieldNameOrPath) {
const startPath = this.ctx.path.length ? this.ctx.path.join('.') + '.' : '';
const pathStr = fieldNameOrPath.toString();
return startPath + pathStr;
}
getPath(fieldNameOrPath) {
let pathStr;
if (fieldNameOrPath instanceof String) {
pathStr = fieldNameOrPath.split('.');
}
else if (fieldNameOrPath instanceof Path_1.Path) {
pathStr = fieldNameOrPath.path;
}
else {
pathStr = [fieldNameOrPath.toString()];
}
return this.ctx.path.concat(pathStr);
}
completeMessage(message, fieldNameOrPath) {
return message.replace('$path', `'${this.getPathString(fieldNameOrPath)}'`);
}
validate(fieldNameOrPath, errorCondition, message) {
if (errorCondition) {
this.ctx.addIssue({
code: zod_1.z.ZodIssueCode.custom,
message: this.completeMessage(message, fieldNameOrPath),
path: this.getPath(fieldNameOrPath),
});
}
}
undesiredField(fieldNameOrPath, customMessage) {
const value = fieldNameOrPath instanceof Path_1.Path
? fieldNameOrPath.getValueFromPath(this.object)
: this.object[fieldNameOrPath];
this.validate(fieldNameOrPath, value != undefined, customMessage ?? `El campo $path no es requerido`);
}
requiredField(fieldNameOrPath, customMessage) {
const value = fieldNameOrPath instanceof Path_1.Path
? fieldNameOrPath.getValueFromPath(this.object)
: this.object[fieldNameOrPath];
this.validate(fieldNameOrPath, value == undefined, customMessage ?? `El campo $path es requerido`);
}
}
exports.default = ZodValidator;