2022-10-18 12:58:41 +00:00
|
|
|
import { useI18n } from 'vue-i18n';
|
|
|
|
import validator from 'validator';
|
2023-09-28 11:45:14 +00:00
|
|
|
import { useValidationsStore } from 'src/stores/useValidationsStore';
|
2022-10-18 12:58:41 +00:00
|
|
|
|
|
|
|
export function useValidator() {
|
2023-09-28 11:45:14 +00:00
|
|
|
const models = useValidationsStore().validations;
|
2022-10-18 12:58:41 +00:00
|
|
|
|
|
|
|
function validate(propertyRule) {
|
2023-09-28 11:45:14 +00:00
|
|
|
const modelInfo = models;
|
2022-10-18 12:58:41 +00:00
|
|
|
if (!modelInfo || !propertyRule) return;
|
|
|
|
|
|
|
|
const rule = propertyRule.split('.');
|
|
|
|
const model = rule[0];
|
|
|
|
const property = rule[1];
|
|
|
|
const modelName = model.charAt(0).toUpperCase() + model.slice(1);
|
|
|
|
|
|
|
|
if (!modelInfo[modelName]) return;
|
|
|
|
|
|
|
|
const modelValidations = modelInfo[modelName].validations;
|
|
|
|
|
|
|
|
if (!modelValidations[property]) return;
|
|
|
|
|
|
|
|
const rules = modelValidations[property].map((validation) => {
|
|
|
|
return validations(validation)[validation.validation];
|
|
|
|
});
|
|
|
|
|
|
|
|
return rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { t } = useI18n();
|
2024-06-21 13:04:38 +00:00
|
|
|
const validations = function (validation = {}) {
|
2022-10-18 12:58:41 +00:00
|
|
|
return {
|
2023-12-22 09:32:57 +00:00
|
|
|
format: (value) => {
|
|
|
|
const { allowNull, with: format, allowBlank } = validation;
|
2023-11-22 06:12:34 +00:00
|
|
|
const message = t(validation.message) || validation.message;
|
2023-12-22 09:32:57 +00:00
|
|
|
if (!allowBlank && value === '') return message;
|
|
|
|
if (!allowNull && value === null) return message;
|
2023-11-22 06:12:34 +00:00
|
|
|
|
2023-12-22 09:32:57 +00:00
|
|
|
const isValid = new RegExp(format).test(value);
|
|
|
|
if (!isValid) return message;
|
2023-11-22 06:12:34 +00:00
|
|
|
},
|
2022-10-20 11:42:49 +00:00
|
|
|
presence: (value) => {
|
2024-06-21 11:09:34 +00:00
|
|
|
let message = t(`globals.valueCantBeEmpty`);
|
2022-10-20 11:42:49 +00:00
|
|
|
if (validation.message)
|
2023-08-24 13:05:31 +00:00
|
|
|
message = t(validation.message) || validation.message;
|
2022-10-20 11:42:49 +00:00
|
|
|
|
2023-08-24 13:05:31 +00:00
|
|
|
return !validator.isEmpty(value ? String(value) : '') || message;
|
2022-10-20 11:42:49 +00:00
|
|
|
},
|
2024-07-16 09:25:06 +00:00
|
|
|
required: (required, value) => {
|
|
|
|
return required ? !!value || t('globals.fieldRequired') : null;
|
|
|
|
},
|
2022-10-18 12:58:41 +00:00
|
|
|
length: (value) => {
|
|
|
|
const options = {
|
|
|
|
min: validation.min || validation.is,
|
2023-08-24 13:05:31 +00:00
|
|
|
max: validation.max || validation.is,
|
2022-10-18 12:58:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
value = String(value);
|
|
|
|
|
|
|
|
if (!value) value = '';
|
|
|
|
|
|
|
|
let message = `Value should have at most ${options.max} characters`;
|
|
|
|
if (validation.is)
|
|
|
|
message = `Value should be ${validation.is} characters long`;
|
|
|
|
if (validation.min)
|
|
|
|
message = `Value should have at least ${validation.min} characters`;
|
|
|
|
if (validation.min && validation.max)
|
|
|
|
message = `Value should have a length between ${validation.min} and ${validation.max}`;
|
|
|
|
|
|
|
|
return validator.isLength(value, options) || message;
|
|
|
|
},
|
|
|
|
numericality: (value) => {
|
|
|
|
if (validation.int)
|
2023-08-24 13:05:31 +00:00
|
|
|
return validator.isInt(value) || 'Value should be integer';
|
|
|
|
return validator.isNumeric(value) || 'Value should be a number';
|
2022-10-18 12:58:41 +00:00
|
|
|
},
|
2024-06-21 13:04:38 +00:00
|
|
|
min: (value, min) => {
|
|
|
|
if (min >= 0)
|
|
|
|
if (Math.floor(value) < min) return t('inputMin', { value: min });
|
|
|
|
},
|
2023-08-24 13:05:31 +00:00
|
|
|
custom: (value) => validation.bindedFunction(value) || 'Invalid value',
|
2022-10-18 12:58:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2023-08-24 13:05:31 +00:00
|
|
|
validate,
|
2024-06-21 13:04:38 +00:00
|
|
|
validations,
|
2023-09-28 11:45:14 +00:00
|
|
|
models,
|
2022-10-18 12:58:41 +00:00
|
|
|
};
|
2023-08-24 13:05:31 +00:00
|
|
|
}
|