useInputRules.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { useI18n } from "vue-i18n";
  2. export const useInputRules = () => {
  3. const { t } = useI18n();
  4. const emailPattern =
  5. /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  6. const passwordPattern = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/;
  7. const cepPattern = /^[0-9]{5}-[0-9]{3}$/;
  8. const inputRules = {
  9. required: (value) => !!value || t("validation.rules.required"),
  10. requiredNumber: (value) => !isNaN(value) || t("validation.rules.required"),
  11. requiredHideMessage: (value) => !!value,
  12. min: (min) => (value) =>
  13. value.length >= min ||
  14. `${t("validation.rules.min")} ${min} ${t("validation.rules.characters")}`,
  15. minTrimmed: (min) => (value) =>
  16. String(value ?? "").trim().length >= min ||
  17. `${t("validation.rules.min")} ${min} ${t("validation.rules.characters")}`,
  18. // Nome completo em um único campo: exige nome e sobrenome, cada um com o mínimo
  19. // de caracteres. Partículas no meio ("da", "de") não entram na verificação.
  20. fullName: (min) => (value) => {
  21. const parts = String(value ?? "")
  22. .trim()
  23. .split(/\s+/)
  24. .filter(Boolean);
  25. return (
  26. (parts.length >= 2 &&
  27. parts[0].length >= min &&
  28. parts[parts.length - 1].length >= min) ||
  29. `${t("validation.rules.full_name")} ${min} ${t("validation.rules.characters")}`
  30. );
  31. },
  32. max: (max) => (value) =>
  33. value.length <= max ||
  34. `${t("validation.rules.max")} ${max} ${t("validation.rules.characters")}`,
  35. minValue: (min) => (value) =>
  36. value >= min || `${t("validation.rules.min")} ${min}`,
  37. maxValue: (max) => (value) =>
  38. value <= max || `${t("validation.rules.max")} ${max}`,
  39. email: (value) =>
  40. !value || emailPattern.test(value) || t("validation.rules.email"),
  41. emails: (value) => {
  42. if (!value) return true;
  43. const emails = value.split(";").map((email) => email.trim());
  44. return (
  45. emails.every((email) => inputRules.email(email) === true) ||
  46. t("validation.rules.email")
  47. );
  48. },
  49. cpf: (value) => !value || isValidCPF(value) || t("validation.rules.cpf"),
  50. cnpj: (value) => !value || isValidCNPJ(value) || t("validation.rules.cnpj"),
  51. samePassword: (otherValue) => (value) =>
  52. value === otherValue || t("validation.rules.same_password"),
  53. password: (value) =>
  54. !value || passwordPattern.test(value) || t("validation.rules.password"),
  55. cep: (value) => {
  56. if (!value) return true;
  57. return cepPattern.test(value) || t("validation.rules.cep");
  58. },
  59. notSameDocument: (allDocuments) => (value) => {
  60. if (!value) return true;
  61. let found = 0;
  62. for (const doc of allDocuments) {
  63. if (doc == value) {
  64. found++;
  65. }
  66. if (found > 1) {
  67. return t("validation.rules.not_same_document");
  68. }
  69. }
  70. return true;
  71. },
  72. };
  73. inputRules.required.$id = 'required'
  74. return {
  75. inputRules,
  76. };
  77. };
  78. function isValidCPF(cpf) {
  79. if (!cpf) return false;
  80. cpf = cpf.replace(/[^\d]+/g, "");
  81. if (cpf.length !== 11) return false;
  82. if (/^(\d)\1+$/.test(cpf)) return false;
  83. let sum = 0;
  84. for (let i = 0; i < 9; i++) sum += parseInt(cpf.charAt(i)) * (10 - i);
  85. let rev = 11 - (sum % 11);
  86. if (rev === 10 || rev === 11) rev = 0;
  87. if (rev !== parseInt(cpf.charAt(9))) return false;
  88. sum = 0;
  89. for (let i = 0; i < 10; i++) sum += parseInt(cpf.charAt(i)) * (11 - i);
  90. rev = 11 - (sum % 11);
  91. if (rev === 10 || rev === 11) rev = 0;
  92. if (rev !== parseInt(cpf.charAt(10))) return false;
  93. return true;
  94. }
  95. function isValidCNPJ(cnpj) {
  96. if (!cnpj) return false;
  97. cnpj = cnpj.replace(/[^\d]+/g, "");
  98. if (cnpj.length !== 14) return false;
  99. if (/^(\d)\1+$/.test(cnpj)) return false;
  100. let length = cnpj.length - 2;
  101. let numbers = cnpj.substring(0, length);
  102. let digits = cnpj.substring(length);
  103. let sum = 0;
  104. let pos = length - 7;
  105. for (let i = length; i >= 1; i--) {
  106. sum += parseInt(numbers.charAt(length - i)) * pos--;
  107. if (pos < 2) pos = 9;
  108. }
  109. let result = sum % 11 < 2 ? 0 : 11 - (sum % 11);
  110. if (result !== parseInt(digits.charAt(0))) return false;
  111. length = length + 1;
  112. numbers = cnpj.substring(0, length);
  113. sum = 0;
  114. pos = length - 7;
  115. for (let i = length; i >= 1; i--) {
  116. sum += parseInt(numbers.charAt(length - i)) * pos--;
  117. if (pos < 2) pos = 9;
  118. }
  119. result = sum % 11 < 2 ? 0 : 11 - (sum % 11);
  120. if (result !== parseInt(digits.charAt(1))) return false;
  121. return true;
  122. }