Skip to content

Commit

Permalink
23.12.50-2: fixed useValidation work with reactive() v object [WTEL-3…
Browse files Browse the repository at this point in the history
…902]
  • Loading branch information
dlohvinov committed Nov 29, 2023
1 parent e58c301 commit 9fe3f46
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@webitel/ui-sdk",
"version": "23.12.49",
"version": "23.12.50-2",
"private": false,
"scripts": {
"dev": "vite",
Expand Down
21 changes: 17 additions & 4 deletions src/mixins/validationMixin/useValidation.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { computed } from 'vue';
import { computed, isReactive, isRef } from 'vue';
import { useI18n } from 'vue-i18n';

export function useValidation({ v, customValidators }) {
export function useValidation({ v: inputV, customValidators: inputCustomValidators }) {
const { t } = useI18n();

// support vue options api, where v is a reactive, not ref
let v = inputV;
let customValidators = inputCustomValidators;

if (isReactive(v)) {
v = computed(() => inputV);
customValidators = computed(() => inputCustomValidators);
}
// end

console.info(v, inputV, isRef(v), isReactive(inputV));

const isValidation = computed(() => !!v && !!v.value && !!Object.keys(v.value).length);
const invalid = computed(() => isValidation.value && v.value.$error);

const validationText = computed(() => {
let validationText = '';
if (isValidation.value && invalid) {
if (isValidation.value && invalid.value) {
if (v.value.required?.$invalid) validationText = t('validation.required');
else if (v.value.numeric?.$invalid) validationText = t('validation.numeric');
else if (v.value.email?.$invalid) validationText = t('validation.email');
Expand All @@ -28,7 +41,7 @@ export function useValidation({ v, customValidators }) {
else if (v.value.integer?.$invalid) validationText = `${t('validation.integer')}`;
}
// eslint-disable-next-line no-restricted-syntax
for (const { name, text } of customValidators) {
for (const { name, text } of customValidators.value) {
if (v.value[name]?.$invalid) validationText = text;
}
return validationText;
Expand Down

0 comments on commit 9fe3f46

Please sign in to comment.