Passed
Push — trunk ( 024f41...cfd10b )
by Christian
15:08 queued 13s
created

validation.mixin.ts ➔ validate   B

Complexity

Conditions 6

Size

Total Lines 34
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 26
dl 0
loc 34
rs 8.3226
c 0
b 0
f 0
1
/* @private */
2
export {};
3
4
/**
5
 * @package admin
6
 *
7
 * @deprecated tag:v6.6.0 - Will be private
8
 * @module app/mixin/validation
9
 */
10
Shopware.Mixin.register('validation', {
11
    inject: ['validationService'],
12
13
    props: {
14
        validation: {
15
            type: [String, Array, Object, Boolean],
16
            required: false,
17
            default: null,
18
        },
19
    },
20
21
    computed: {
22
        isValid(): boolean {
23
            // @ts-expect-error
24
            // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
25
            const value = this.currentValue || this.value || this.selections;
26
27
            return this.validate(value);
28
        },
29
    },
30
31
    methods: {
32
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
        validate(value: any) {
34
            // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
35
            let validation = this.validation;
36
            let valid = true;
37
38
            if (Shopware.Utils.types.isBoolean(validation)) {
39
                return validation;
40
            }
41
42
            if (Shopware.Utils.types.isString(validation)) {
43
                const validationList = validation.split(',');
44
45
                if (validationList.length > 1) {
46
                    validation = validationList;
47
                } else {
48
                    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-argument
49
                    valid = this.validateRule(value, this.validation);
50
                }
51
            }
52
53
            if (Shopware.Utils.types.isArray(validation)) {
54
                valid = validation.every((validationRule) => {
55
                    if (Shopware.Utils.types.isBoolean(validationRule)) {
56
                        return validationRule;
57
                    }
58
59
                    // eslint-disable-next-line max-len
60
                    // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument
61
                    return this.validateRule(value, validationRule.trim());
62
                });
63
            }
64
65
            return valid;
66
        },
67
68
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
69
        validateRule(value: any, rule: string) {
70
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
71
            if (typeof this.validationService[rule] === 'undefined') {
72
                return false;
73
            }
74
75
            // eslint-disable-next-line max-len
76
            // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return
77
            return this.validationService[rule](value);
78
        },
79
    },
80
});
81