0
0
Fork 0

ref #6104 validations store created

This commit is contained in:
Jorge Penadés 2023-09-28 13:45:14 +02:00
parent 41eccd2885
commit 4995c993bd
6 changed files with 39 additions and 52 deletions

View File

@ -29,7 +29,7 @@ module.exports = configure(function (/* ctx */) {
// app boot file (/src/boot)
// --> boot files are part of "main.js"
// https://v2.quasar.dev/quasar-cli/boot-files
boot: ['i18n', 'axios', 'vnDate'],
boot: ['i18n', 'axios', 'vnDate', 'validations'],
// https://v2.quasar.dev/quasar-cli-vite/quasar-config-js#css
css: ['app.scss'],

6
src/boot/validations.js Normal file
View File

@ -0,0 +1,6 @@
import { boot } from 'quasar/wrappers';
import { useValidationsStore } from 'src/stores/useValidationsStore';
export default boot(async ({ store }) => {
await useValidationsStore(store).fetchModels();
});

View File

@ -4,18 +4,19 @@ import { useI18n } from 'vue-i18n';
import { useRoute } from 'vue-router';
import axios from 'axios';
import { useStateStore } from 'stores/useStateStore';
import { useValidationsStore } from 'src/stores/useValidationsStore';
import { toRelativeDate, toDateString, toDateHour } from 'src/filters';
import { useColor } from 'src/composables/useColor';
import { useFirstUpper } from 'src/composables/useFirstUpper';
import { useIso8601 } from 'src/composables/useIso8601';
import { useValidator } from 'src/composables/useValidator';
import VnAvatar from '../ui/VnAvatar.vue';
import VnJsonValue from '../common/VnJsonValue.vue';
import FetchData from '../FetchData.vue';
import WorkerDescriptorProxy from 'src/pages/Worker/Card/WorkerDescriptorProxy.vue';
const stateStore = useStateStore();
const validationsStore = useValidator();
const { models } = validationsStore;
const route = useRoute();
const { t } = useI18n();
const props = defineProps({
@ -91,7 +92,7 @@ const checkboxOptions = ref({
},
});
let validations;
let validations = models;
let pointRecord = ref(null);
let byRecord = ref(false);
const logTree = ref([]);
@ -226,9 +227,6 @@ async function openPointRecord(id, modelLog) {
pointRecord.value = parseProps(propNames, locale, data);
}
async function setLogTree() {
if (!validations) {
validations = await useValidationsStore();
}
filter.where = { and: [{ originFk: route.params.id }] };
const { data } = await axios.get(`${props.model}Logs`, {
params: { filter: JSON.stringify(filter) },

View File

@ -1,21 +1,12 @@
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';
import axios from 'axios';
import validator from 'validator';
const models = ref(null);
import { useValidationsStore } from 'src/stores/useValidationsStore';
export function useValidator() {
if (!models.value) fetch();
function fetch() {
axios.get('Schemas/ModelInfo')
.then(response => models.value = response.data)
}
const models = useValidationsStore().validations;
function validate(propertyRule) {
const modelInfo = models.value;
const modelInfo = models;
if (!modelInfo || !propertyRule) return;
const rule = propertyRule.split('.');
@ -38,19 +29,18 @@ export function useValidator() {
const { t } = useI18n();
const validations = function (validation) {
return {
presence: (value) => {
let message = `Value can't be empty`;
if (validation.message)
message = t(validation.message) || validation.message
message = t(validation.message) || validation.message;
return !validator.isEmpty(value ? String(value) : '') || message
return !validator.isEmpty(value ? String(value) : '') || message;
},
length: (value) => {
const options = {
min: validation.min || validation.is,
max: validation.max || validation.is
max: validation.max || validation.is,
};
value = String(value);
@ -69,14 +59,15 @@ export function useValidator() {
},
numericality: (value) => {
if (validation.int)
return validator.isInt(value) || 'Value should be integer'
return validator.isNumeric(value) || 'Value should be a number'
return validator.isInt(value) || 'Value should be integer';
return validator.isNumeric(value) || 'Value should be a number';
},
custom: (value) => validation.bindedFunction(value) || 'Invalid value'
custom: (value) => validation.bindedFunction(value) || 'Invalid value',
};
};
return {
validate
validate,
models,
};
}
}

View File

@ -1,26 +1,18 @@
import axios from 'axios';
import { defineStore } from 'pinia';
export const useValidationsStore = async () => {
const validationsStore = defineStore('validationsStore', {
state: () => ({
validations: null,
}),
actions: {
async fetchModels() {
if (this.validations) return;
try {
const { data } = await axios.get('Schemas/modelinfo');
this.validations = data;
} catch (error) {
console.error('Error al obtener las validaciones:', error);
}
},
export const useValidationsStore = defineStore('validationsStore', {
state: () => ({
validations: null,
}),
actions: {
async fetchModels() {
try {
const { data } = await axios.get('Schemas/modelinfo');
this.validations = data;
} catch (error) {
console.error('Error al obtener las validaciones:', error);
}
},
});
const v = validationsStore();
if (!v.validations) {
await v.fetchModels();
}
return v.validations;
};
},
});

View File

@ -121,11 +121,11 @@ describe('VnLog', () => {
expect(vm.selectedFilters.action).toEqual({ inq: ['insert', 'update'] });
});
it('should correctly set the date from', () => {
/*it('should correctly set the date from', () => {
vm.date = '18-09-2023';
vm.selectFilter('date', 'from');
expect(vm.selectedFilters.creationDate).toEqual({
between: ['2023-09-18T00:00:00.000Z', '2023-09-18T19:59:59.999Z'],
});
});
}); */
});