Newer
Older
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
import { CustomError } from "../../../error/CustomError";
import { z } from "zod";
export const addShoppingCartSchema = z.object({
name: z
.string()
.min(1, {
message: 'The "name" field is required and must be a non-empty string.',
})
.catch((err) => {
throw new CustomError(
"Invalid 'name' field",
400,
"INVALID_NAME",
err.error.message
);
}),
beschreibung: z
.string()
.min(1, {
message:
'The "beschreibung" field is required and must be a non-empty string.',
})
.catch((err) => {
throw new CustomError(
"Invalid 'beschreibung' field",
400,
"INVALID_BESCHREIBUNG",
err.error.message
);
}),
});
export const deleteShoppingCartSchema = z.object({
id: z
.number()
.min(1, {
message: "The 'id' field must be a valid number and greater than 0.",
})
.catch((err) => {
throw new CustomError(
"Invalid or missing 'id'",
400,
"INVALID_OR_MISSING_ID",
err.error.message
);
}),
});
export const queryIdSchema = z
.object({
id: z
.string()
.refine((data) => /[0-9]+/.test(data) && Number(data) >= 0, {
message: "id must be a non-negative integer.",
})
.catch((err) => {
throw new CustomError(
"Invalid ID",
400,
"INVALID_OR_MISSING_ID",
err.error.message
);
}),
})
.strict()
.catch((err) => {
throw new CustomError(
"Unexpected fields for query",
400,
"UNEXPECTED_FIELDS_IN_QUERY",
"Details: Only 'id' should be present in the query parameters."
);
});
export const queryNameSchema = z
.object({
name: z
.string()
.min(1, { message: "name must be a non-empty string." })
.catch((err) => {
throw new CustomError(
"Invalid Name",
400,
"INVALID_OR_MISSING_NAME",
err.error.message
);
}),
})
.strict()
.catch((err: any) => {
throw new CustomError(
"Unexpected fields for query",
400,
"UNEXPECTED_FIELDS_IN_QUERY",
err.error.message
);
});
export const updateShoppingCartSchema = z.object({
id: z
.number()
.min(1, {
message: "The 'id' field must be a valid number and greater than 0.",
})
.catch((err) => {
throw new CustomError(
"Invalid or missing 'id'",
400,
"INVALID_OR_MISSING_ID",
err.error.message
);
}),
name: z
.string()
.optional()
.catch((err) => {
throw new CustomError(
"Invalid value for 'name'",
400,
"INVALID_NAME_TYPE",
err.error.message
);
}),
beschreibung: z
.string()
.optional()
.catch((err) => {
throw new CustomError(
"Invalid value for 'beschreibung'",
400,
"INVALID_BESCHREIBUNG_TYPE",
err.error.message
);
}),
});
export const queryShoppingartParamsSchema = z
.object({
name: z.string({ message: "name must be a string" }).optional(),
beschreibung: z.string().optional()
})
.refine((data) => Object.keys(data).length === 1, {
message: "only one key (name, description) is allowed at a time",
})
.catch((err) => {
throw new CustomError(
"wrong prarams ",
422,
"Unaccesable_entity",
err.error.message
);
});