-
Notifications
You must be signed in to change notification settings - Fork 596
/
validation-utils.ts
70 lines (69 loc) · 2.59 KB
/
validation-utils.ts
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
import { TFunction } from 'i18next';
import * as yup from 'yup';
import { nameRegex } from '@console/shared';
import { EditorType } from '@console/shared/src/components/synced-editor/editor-toggle';
export const hpaValidationSchema = (t: TFunction) =>
yup.object({
editorType: yup.string(),
formData: yup.object().when('editorType', {
is: EditorType.Form,
then: yup.object({
metadata: yup.object({
name: yup
.string()
.matches(nameRegex, {
message: t(
'devconsole~Name must consist of lower case alphanumeric characters, hyphens or dots, and must start and end with an alphanumeric character.',
),
excludeEmptyString: true,
})
.max(253, t('devconsole~Cannot be longer than 253 characters.'))
.required(t('devconsole~Required')),
}),
spec: yup.object({
minReplicas: yup
.number()
.integer(t('devconsole~Minimum Pods must be an integer.'))
.min(1, t('devconsole~Minimum Pods must greater than or equal to 1.'))
.test(
'test-less-than-max',
t('devconsole~Minimum Pods should be less than or equal to Maximum Pods.'),
function(minReplicas) {
const { maxReplicas } = this.parent;
return minReplicas <= maxReplicas;
},
),
maxReplicas: yup
.number()
.integer(t('devconsole~Maximum Pods must be an integer.'))
.test(
'test-greater-than-min',
t('devconsole~Maximum Pods should be greater than or equal to Minimum Pods.'),
function(maxReplicas) {
const { minReplicas } = this.parent;
return minReplicas <= maxReplicas;
},
)
.required(t('devconsole~Max Pods must be defined.')),
metrics: yup.array(
yup.object({
resource: yup.object({
target: yup.object({
averageUtilization: yup
.mixed()
.test(
'test-for-valid-utilization',
t('devconsole~Average utilization must be a positive number.'),
function(avgUtilization) {
if (!avgUtilization) return true;
return /^\d+$/.test(String(avgUtilization));
},
),
}),
}),
}),
),
}),
}),
}),
});