@jackiemacklein/nettz-utils
Version:
Serviços de imagem, e-mail, códigos de barras, utilitários numéricos e componentes React para apps Node.js com TypeScript
90 lines (89 loc) • 3.56 kB
JavaScript
;
/**
* @author Jackiê Macklein
* @company Onside tecnologia/Nettz
* @copyright Todos direitos reservados.
* @description Validação de payloads Asaas antes do envio à API.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateAsaasPayload = validateAsaasPayload;
exports.assertValidAsaasPayload = assertValidAsaasPayload;
const date_fns_1 = require("date-fns");
const errors_1 = require("./errors");
const SCHEMAS = {
"CREATE-CUSTOMER": {
name: { required: true, type: "string" },
cpfCnpj: { required: true, type: "string" },
email: { required: false, type: "string", pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/ },
mobilePhone: {
required: false,
type: "string",
pattern: /^\(?\d{2}\)?[\s-]?9?\d{4}[\s-]?\d{4}$/,
},
},
"EDIT-CUSTOMER": {
name: { required: false, type: "string" },
cpfCnpj: { required: false, type: "string" },
email: { required: false, type: "string", pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/ },
mobilePhone: {
required: false,
type: "string",
pattern: /^\(?\d{2}\)?[\s-]?9?\d{4}[\s-]?\d{4}$/,
},
},
"CREATE-CHARGE": {
customer: { required: true, type: "string" },
value: { required: true, type: "number" },
description: { required: true, type: "string" },
dueDate: { required: true, type: "date" },
billingType: { required: true, type: "string", enum: ["BOLETO", "PIX", "CREDIT_CARD"] },
},
"EDIT-CHARGE": {
value: { required: false, type: "number" },
description: { required: false, type: "string" },
dueDate: { required: false, type: "date" },
},
};
function isValidDueDate(value) {
const date = typeof value === "string"
? (0, date_fns_1.parseISO)(value.includes("T") ? value : `${value}T12:00:00`)
: new Date(String(value));
if (!(0, date_fns_1.isValid)(date))
return false;
return !(0, date_fns_1.isBefore)((0, date_fns_1.startOfDay)(date), (0, date_fns_1.startOfDay)(new Date()));
}
function validateAsaasPayload(kind, data) {
const schema = SCHEMAS[kind];
const errors = [];
for (const [field, rules] of Object.entries(schema)) {
const value = data[field];
if (rules.required && (value === undefined || value === null)) {
errors.push(`O campo ${field} é obrigatório.`);
continue;
}
if (value === undefined || value === null)
continue;
if (rules.type === "date") {
if (!isValidDueDate(value)) {
errors.push(`O campo ${field} deve ser uma data válida.`);
}
continue;
}
if (rules.type && typeof value !== rules.type) {
errors.push(`O campo ${field} deve ser do tipo ${rules.type}.`);
}
if (rules.pattern && typeof value === "string" && !rules.pattern.test(value)) {
errors.push(`O campo ${field} está em um formato inválido.`);
}
if (rules.enum && !rules.enum.includes(String(value))) {
errors.push(`O campo ${field} deve ser um dos seguintes valores: ${rules.enum.join(", ")}.`);
}
}
return { isValid: errors.length === 0, errors };
}
function assertValidAsaasPayload(kind, data) {
const result = validateAsaasPayload(kind, data);
if (!result.isValid) {
throw new errors_1.AsaasApiError(result.errors.join(" | "), 400, "", "INVALID_DATA");
}
}