|
|
@@ -1,4 +1,9 @@
|
|
|
-import { format, isValid, parse } from "date-fns";
|
|
|
+import {
|
|
|
+ differenceInCalendarDays,
|
|
|
+ format,
|
|
|
+ isValid,
|
|
|
+ parse,
|
|
|
+} from "date-fns";
|
|
|
import { useI18n } from "vue-i18n";
|
|
|
|
|
|
export const useInputRules = () => {
|
|
|
@@ -45,20 +50,72 @@ export const useInputRules = () => {
|
|
|
date: (value) => {
|
|
|
if (!hasValue(value)) return true;
|
|
|
|
|
|
- const formats = ["dd/MM/yyyy", "yyyy-MM-dd"];
|
|
|
+ return parseInputDate(value) || t("validation.rules.date");
|
|
|
+ },
|
|
|
+
|
|
|
+ dateMaxDaysFromToday:
|
|
|
+ (maxDays = 365) =>
|
|
|
+ (value) => {
|
|
|
+ if (!hasValue(value)) return true;
|
|
|
+
|
|
|
+ const date = parseInputDate(value);
|
|
|
|
|
|
- const valid = formats.some((dateFormat) => {
|
|
|
- const parsedDate = parse(value, dateFormat, new Date());
|
|
|
+ if (!date) return true;
|
|
|
|
|
|
return (
|
|
|
- isValid(parsedDate) &&
|
|
|
- format(parsedDate, dateFormat) === value
|
|
|
+ differenceInCalendarDays(date, new Date()) <= maxDays ||
|
|
|
+ t("validation.rules.date_max_days_from_today", { maxDays })
|
|
|
);
|
|
|
- });
|
|
|
+ },
|
|
|
+
|
|
|
+ dateNotBefore: (getMinimumDate) => (value) => {
|
|
|
+ if (!hasValue(value)) return true;
|
|
|
+
|
|
|
+ const minimumValue =
|
|
|
+ typeof getMinimumDate === "function"
|
|
|
+ ? getMinimumDate()
|
|
|
+ : getMinimumDate;
|
|
|
|
|
|
- return valid || t("validation.rules.date");
|
|
|
+ if (!hasValue(minimumValue)) return true;
|
|
|
+
|
|
|
+ const date = parseInputDate(value);
|
|
|
+ const minimumDate = parseInputDate(minimumValue);
|
|
|
+
|
|
|
+ if (!date || !minimumDate) return true;
|
|
|
+
|
|
|
+ return (
|
|
|
+ date >= minimumDate ||
|
|
|
+ t("validation.rules.date_not_before", {
|
|
|
+ minimumDate: minimumValue,
|
|
|
+ })
|
|
|
+ );
|
|
|
},
|
|
|
|
|
|
+ dateRangeDays:
|
|
|
+ (getStartDate, maxDays = 365) =>
|
|
|
+ (value) => {
|
|
|
+ if (!hasValue(value)) return true;
|
|
|
+
|
|
|
+ const startValue =
|
|
|
+ typeof getStartDate === "function" ? getStartDate() : getStartDate;
|
|
|
+
|
|
|
+ if (!hasValue(startValue)) return true;
|
|
|
+
|
|
|
+ const startDate = parseInputDate(startValue);
|
|
|
+ const endDate = parseInputDate(value);
|
|
|
+
|
|
|
+ if (!startDate || !endDate) return true;
|
|
|
+
|
|
|
+ if (endDate < startDate) {
|
|
|
+ return t("validation.rules.date_before_start");
|
|
|
+ }
|
|
|
+
|
|
|
+ return (
|
|
|
+ differenceInCalendarDays(endDate, startDate) <= maxDays ||
|
|
|
+ t("validation.rules.date_range_days", { maxDays })
|
|
|
+ );
|
|
|
+ },
|
|
|
+
|
|
|
durationHours: (value) => {
|
|
|
const minutes = Number(value) * 60;
|
|
|
|
|
|
@@ -153,8 +210,7 @@ export const useInputRules = () => {
|
|
|
password: (value) =>
|
|
|
!value || passwordPattern.test(value) || t("validation.rules.password"),
|
|
|
|
|
|
- positive: (value) =>
|
|
|
- Number(value) > 0 || t("validation.rules.positive"),
|
|
|
+ positive: (value) => Number(value) > 0 || t("validation.rules.positive"),
|
|
|
|
|
|
samePassword: (otherValue) => (value) =>
|
|
|
value === otherValue || t("validation.rules.same_password"),
|
|
|
@@ -201,6 +257,20 @@ export const useInputRules = () => {
|
|
|
};
|
|
|
};
|
|
|
|
|
|
+const parseInputDate = (value) => {
|
|
|
+ const formats = ["dd/MM/yyyy", "yyyy-MM-dd"];
|
|
|
+
|
|
|
+ for (const dateFormat of formats) {
|
|
|
+ const parsedDate = parse(value, dateFormat, new Date());
|
|
|
+
|
|
|
+ if (isValid(parsedDate) && format(parsedDate, dateFormat) === value) {
|
|
|
+ return parsedDate;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+};
|
|
|
+
|
|
|
const isValidCNPJ = (cnpj) => {
|
|
|
if (!cnpj) return false;
|
|
|
|
|
|
@@ -274,4 +344,4 @@ const isValidCPF = (cpf) => {
|
|
|
if (rev !== parseInt(cpf.charAt(10))) return false;
|
|
|
|
|
|
return true;
|
|
|
-};
|
|
|
+};
|