aboutsummaryrefslogtreecommitdiffstats
path: root/deprecated-workflow-designer/sdc-workflow-designer-ui/src/app/shared/input/validators.ts
blob: 137d5444ddbe944367446d022141772267851dc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import {AbstractControl, ValidationErrors} from '@angular/forms';

export function inRangeValidator(in_range: number[]): ValidationErrors|null {
    return (control: AbstractControl): ValidationErrors => {
        const value = parseFloat(control.value);
        if (isNaN(value) || value > in_range[1] || value < in_range[0]) {
            control.setErrors({
                in_range: true
            });
            return {
                in_range: true
            }
        } else {
            return null;
        }
    }
}

export function greaterOrEqualValidator(max: string): ValidationErrors|null {
    return (control: AbstractControl): ValidationErrors => {
        const value = parseFloat(control.value);
        const maxValue: any = parseFloat(max);
        if (!isNaN(maxValue) && (isNaN(value) || value < maxValue)) {
            control.setErrors({
                greater_or_equal: true
            });
            return {
                greater_or_equal: true
            }
        } else {
            return null;
        }
    }
}

export function lessOrEqualValidator(min: string): ValidationErrors|null {
    return (control: AbstractControl): ValidationErrors => {
        const value = parseFloat(control.value);
        const minValue: any = parseFloat(min);
        if (!isNaN(minValue) && (isNaN(value) || value > minValue)) {
            control.setErrors({
                less_or_equal: true
            });
            return {
                less_or_equal: true
            }
        } else {
            return null;
        }
    }
}

export function greaterThanValidator(max: string): ValidationErrors|null {
    return (control: AbstractControl): ValidationErrors => {
        const value = parseFloat(control.value);
        const maxValue: any = parseFloat(max);
        if (!isNaN(maxValue) && (isNaN(value) || value <= maxValue)) {
            control.setErrors({
                greater_than: true
            });
            return {
                greater_than: true
            }
        } else {
            return null;
        }
    }
}

export function lessThanValidator(min: string): ValidationErrors|null {
    return (control: AbstractControl): ValidationErrors => {
        const value = parseFloat(control.value);
        const minValue: any = parseFloat(min);
        if (!isNaN(minValue) && (isNaN(value) || value >= minValue)) {
            control.setErrors({
                less_than: true
            });
            return {
                less_than: true
            }
        } else {
            return null;
        }
    }
}

export function equalValidator(value: any): ValidationErrors|null {
    return (control: AbstractControl): ValidationErrors => {
        if (control.value != value) {
            control.setErrors({
                equal: true
            });
            return {
                equal: true
            }
        } else {
            return null;
        }
    }
}

export function lengthValidator(length: number): ValidationErrors|null {
    return (control: AbstractControl): ValidationErrors => {
        if (control.value && control.value.length !== length) {
            control.setErrors({
                length: true
            });
            return {
                length: true
            }
        } else {
            return null;
        }
    }
}

export function floatValidator(): ValidationErrors|null {
    return (control: AbstractControl): ValidationErrors => {
        let floatPattern = /^(-?\d+)(\.\d+)?$/;
        if (control.value && !floatPattern.test(control.value)) {
            control.setErrors({
                float: true
            });
            return {
                float: true
            }
        } else {
            return null;
        }
    }
}

export function integerValidator(): ValidationErrors|null {
    return (control: AbstractControl): ValidationErrors => {
        let integerPattern = /^-?\d+$/;
        if (control.value && !integerPattern.test(control.value)) {
            control.setErrors({
                integer: true
            });
            return {
                integer: true
            }
        } else {
            return null;
        }
    }
}