-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_validation.ts
122 lines (118 loc) · 3.57 KB
/
02_validation.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
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
import { z as zod } from 'zod';
import { _NAME_MAX_SIZE } from 'config';
import { type Vocab } from 'app/i18n/types';
const channelVarNames: string[] = [
'first',
'...others',
];
const systemVarNames: string[] = [
'first',
'...others',
];
export const getMemorySchema = (
tLocal: Vocab,
): zod.ZodType<{
name: string;
fieldList: Array<{ key: string; value: string }>;
}> => {
return zod.object({
name: zod
.string()
.trim()
.min(1, { message: tLocal.errorEmptyName })
.max(_NAME_MAX_SIZE, { message: tLocal.errorNameMaxLength }),
fieldList: zod
.array(
zod.object({
key: zod.string(),
value: zod.string(),
}),
)
.superRefine((data, ctx) => {
const allEmpty = data.every(({ key, value }) => key === '' && value === '');
if (allEmpty) {
data.forEach((__, index) => {
ctx.addIssue({
path: [index, 'key'],
message: tLocal.errorEmptyContextVariable,
code: 'custom',
});
ctx.addIssue({
path: [index, 'value'],
message: tLocal.errorEmptyContextVariableValue,
code: 'custom',
});
});
}
})
.superRefine((data, ctx) => {
data.forEach(({ key, value }, index) => {
key = key.trim();
value = value.trim();
if (!(key === '' && value === '')) {
if (key === '') {
ctx.addIssue({
path: [index, 'key'],
message: tLocal.errorEmptyContextVariable,
code: 'custom',
});
}
if (value === '') {
ctx.addIssue({
path: [index, 'value'],
message: tLocal.errorEmptyContextVariableValue,
code: 'custom',
});
}
if (key.length > 128) {
ctx.addIssue({
path: [index, 'key'],
message: tLocal.errorNotValidContextVariableName,
code: 'custom',
});
}
if (!/^[a-zA-Z][a-zA-Z0-9_]{0,127}$/g.test(key)) {
ctx.addIssue({
path: [index, 'key'],
message: tLocal.errorNotValidContextVariableName,
code: 'custom',
});
}
if (channelVarNames.includes(key) || systemVarNames.includes(key)) {
ctx.addIssue({
path: [index, 'key'],
message: tLocal.errorContextVariableNameNotLegal,
code: 'custom',
});
}
if (value.length > 1000) {
ctx.addIssue({
path: [index, 'value'],
message: tLocal.errorNotValidValue,
code: 'custom',
});
}
}
});
})
.superRefine((data, ctx) => {
const keys = data.map(({ key }) => key).filter((key) => key !== '');
const duplicates = keys.filter((key, index, array) => array.indexOf(key) !== index);
if (duplicates.length > 0) {
data.forEach(({ key }, index) => {
if (duplicates.includes(key)) {
ctx.addIssue({
path: [index, 'key'],
message: tLocal.errorDuplicateKey,
code: 'custom',
});
}
});
}
}),
});
};
export type errorsMemory = {
name: { _errors: string[] };
fieldList: Record<number, { key?: { _errors: string[] }; value?: { _errors: string[] } }>;
} | null;