infinity-forge
Version:
206 lines • 8.55 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.schema = void 0;
var yup = __importStar(require("yup"));
var validations_1 = require("./validations/index.js");
exports.schema = {
required: function (params) {
return yup.mixed().nullable()
.test("required", "Campo requerido", function (value, context) {
var _a;
var isEmpty = value === undefined || value === null || value === "" || value === false || (Array.isArray(value) && value.length === 0);
if (isEmpty) {
return context.createError({
message: ((_a = params === null || params === void 0 ? void 0 : params.message) === null || _a === void 0 ? void 0 : _a.call(params, { value: value, context: context })) || "Campo requerido",
});
}
return true;
});
},
email: function (params) {
return yup
.string()
.test("email", "", function (value, context) {
var _a;
var isEmpty = !value;
var isValidEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value || "");
if (isEmpty) {
return context.createError({
message: ((_a = params === null || params === void 0 ? void 0 : params.message) === null || _a === void 0 ? void 0 : _a.call(params, { value: value, context: context })) || "Campo requerido",
});
}
if (!isValidEmail) {
return context.createError({ message: "Email inválido" });
}
return true;
});
},
phone: function (params) {
return yup
.string()
.test("phone", "", function (value, context) {
var _a;
var isValid = (0, validations_1.validatePhone)({ phoneNumber: value, fixedPhone: true });
if (!isValid) {
return context.createError({
message: ((_a = params === null || params === void 0 ? void 0 : params.message) === null || _a === void 0 ? void 0 : _a.call(params, { value: value, context: context })) || "Telefone inválido",
});
}
return true;
});
},
cpf: function (params) {
return yup
.string()
.test("cpf", "", function (value, context) {
var _a;
var isValid = (0, validations_1.validateCPF)(value);
if (!isValid) {
return context.createError({
message: ((_a = params === null || params === void 0 ? void 0 : params.message) === null || _a === void 0 ? void 0 : _a.call(params, { value: value, context: context })) || "Documento inválido",
});
}
return true;
});
},
cnpj: function (params) {
return yup
.string()
.test("cnpj", "", function (value, context) {
var _a;
var isValid = (0, validations_1.validateCNPJ)(value);
if (!isValid) {
return context.createError({
message: ((_a = params === null || params === void 0 ? void 0 : params.message) === null || _a === void 0 ? void 0 : _a.call(params, { value: value, context: context })) || "CNPJ inválido",
});
}
return true;
});
},
password: function (params) {
return yup
.string()
.test("password", "", function (value, context) {
var _a;
var val = value || "";
var tests = [
{
check: val.length >= 8,
message: "A senha deve ter pelo menos 8 caracteres",
},
{
check: /[A-Z]/.test(val),
message: "A senha deve conter ao menos uma letra maiúscula",
},
{
check: /[a-z]/.test(val),
message: "A senha deve conter ao menos uma letra minúscula",
},
{
check: /[0-9]/.test(val),
message: "A senha deve conter ao menos um número",
},
{
check: /[@$!%*#?&]/.test(val),
message: "A senha deve conter ao menos um caractere especial",
},
];
for (var _i = 0, tests_1 = tests; _i < tests_1.length; _i++) {
var test = tests_1[_i];
if (!test.check) {
return context.createError({ message: test.message });
}
}
if (!val) {
return context.createError({
message: ((_a = params === null || params === void 0 ? void 0 : params.message) === null || _a === void 0 ? void 0 : _a.call(params, { value: value, context: context })) || "Campo requerido",
});
}
return true;
});
},
confirmPassword: function (params) {
return yup
.string()
.test("confirmPassword", "", function (value, context) {
var _a;
var refValue = context.resolve(yup.ref(params === null || params === void 0 ? void 0 : params.refField));
if (value !== refValue) {
return context.createError({
message: ((_a = params === null || params === void 0 ? void 0 : params.message) === null || _a === void 0 ? void 0 : _a.call(params, { value: value, context: context })) || "As senhas não coincidem",
});
}
return true;
});
},
number: function (_a) {
var operator = _a.operator, value = _a.value, message = _a.message;
return yup
.number()
.test("compare_number", "", function (input, context) {
if (typeof value !== "number" || isNaN(value)) {
return context.createError({
message: (message === null || message === void 0 ? void 0 : message({ value: value, context: context })) || "Valor inválido",
});
}
var isValid = false;
switch (operator) {
case "greater":
isValid = input > value;
break;
case "less":
isValid = input < value;
break;
case "greaterOrEqual":
isValid = input >= value;
break;
case "lessOrEqual":
isValid = input <= value;
break;
case "equal":
isValid = input === value;
break;
}
if (!isValid) {
return context.createError({
message: (message === null || message === void 0 ? void 0 : message({ value: input, context: context })) || "Valor inválido",
});
}
return true;
});
},
};
//# sourceMappingURL=validator.js.map