@digitraffic/common 2026.2.17-2 → 2026.2.17-4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/zod-utils.d.ts +10 -14
- package/dist/utils/zod-utils.js +36 -31
- package/package.json +1 -1
|
@@ -2,30 +2,26 @@ import { z } from "zod";
|
|
|
2
2
|
/**
|
|
3
3
|
* Functions for validating and parsing e.g. path and query parameters with zod.
|
|
4
4
|
* The value of the message parameter will appear in the zod error object resulting from failed validation.
|
|
5
|
+
* Empty strings are treated as undefined.
|
|
5
6
|
*
|
|
6
7
|
* Usage:
|
|
7
8
|
* const mySchema = z
|
|
8
9
|
* .object({
|
|
9
|
-
*
|
|
10
|
+
* requiredParam: zStringToDate("Invalid value of myParameter"),
|
|
11
|
+
* optionalParam: zStringToDate().optional(),
|
|
10
12
|
* });
|
|
11
13
|
*
|
|
12
14
|
*/
|
|
13
|
-
export declare function zStringToNumber(message?: string): z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>;
|
|
14
|
-
export declare function zStringToDate(message?: string): z.ZodPipe<z.ZodString, z.ZodTransform<Date | undefined, string>>;
|
|
15
|
-
/**
|
|
16
|
-
* Transforms an empty string to undefined. Useful for optional query parameters
|
|
17
|
-
* that may arrive as empty strings.
|
|
18
|
-
*/
|
|
19
|
-
export declare const zEmptyStringToUndefined: z.ZodPipe<z.ZodLiteral<"">, z.ZodTransform<undefined, "">>;
|
|
20
15
|
/**
|
|
21
|
-
*
|
|
16
|
+
* Parses a string to a number. Empty strings are treated as undefined.
|
|
22
17
|
*/
|
|
23
|
-
export declare function
|
|
18
|
+
export declare function zStringToNumber(message?: string): z.ZodPipe<z.ZodString, z.ZodTransform<number | undefined, string>>;
|
|
24
19
|
/**
|
|
25
|
-
*
|
|
20
|
+
* Parses a string to a Date. Empty strings are treated as undefined.
|
|
26
21
|
*/
|
|
27
|
-
export declare function
|
|
22
|
+
export declare function zStringToDate(message?: string): z.ZodPipe<z.ZodString, z.ZodTransform<Date | undefined, string>>;
|
|
28
23
|
/**
|
|
29
|
-
*
|
|
24
|
+
* A non-empty string. Empty strings produce a validation error.
|
|
25
|
+
* Chain with .optional() to also accept undefined.
|
|
30
26
|
*/
|
|
31
|
-
export declare function
|
|
27
|
+
export declare function zNonEmptyString(message?: string): z.ZodString;
|
package/dist/utils/zod-utils.js
CHANGED
|
@@ -2,51 +2,56 @@ import { z } from "zod";
|
|
|
2
2
|
/**
|
|
3
3
|
* Functions for validating and parsing e.g. path and query parameters with zod.
|
|
4
4
|
* The value of the message parameter will appear in the zod error object resulting from failed validation.
|
|
5
|
+
* Empty strings are treated as undefined.
|
|
5
6
|
*
|
|
6
7
|
* Usage:
|
|
7
8
|
* const mySchema = z
|
|
8
9
|
* .object({
|
|
9
|
-
*
|
|
10
|
+
* requiredParam: zStringToDate("Invalid value of myParameter"),
|
|
11
|
+
* optionalParam: zStringToDate().optional(),
|
|
10
12
|
* });
|
|
11
13
|
*
|
|
12
14
|
*/
|
|
13
|
-
export function zStringToNumber(message) {
|
|
14
|
-
return z
|
|
15
|
-
.string()
|
|
16
|
-
.transform(Number)
|
|
17
|
-
.refine((number) => !Number.isNaN(number), {
|
|
18
|
-
message: message ? message : "Not a number",
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
export function zStringToDate(message) {
|
|
22
|
-
return z
|
|
23
|
-
.string()
|
|
24
|
-
.transform((val) => (val ? new Date(val) : undefined))
|
|
25
|
-
.refine((date) => !date || !Number.isNaN(date.getTime()), {
|
|
26
|
-
message: message ? message : "Invalid date format",
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
15
|
/**
|
|
30
|
-
*
|
|
31
|
-
* that may arrive as empty strings.
|
|
16
|
+
* Parses a string to a number. Empty strings are treated as undefined.
|
|
32
17
|
*/
|
|
33
|
-
export
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
18
|
+
export function zStringToNumber(message) {
|
|
19
|
+
return z.string().transform((val, ctx) => {
|
|
20
|
+
if (!val)
|
|
21
|
+
return undefined;
|
|
22
|
+
const num = Number(val);
|
|
23
|
+
if (Number.isNaN(num)) {
|
|
24
|
+
ctx.addIssue({ code: "custom", message: message ?? "Not a number" });
|
|
25
|
+
return z.NEVER;
|
|
26
|
+
}
|
|
27
|
+
return num;
|
|
28
|
+
});
|
|
39
29
|
}
|
|
40
30
|
/**
|
|
41
|
-
*
|
|
31
|
+
* Parses a string to a Date. Empty strings are treated as undefined.
|
|
42
32
|
*/
|
|
43
|
-
export function
|
|
44
|
-
return
|
|
33
|
+
export function zStringToDate(message) {
|
|
34
|
+
return z.string().transform((val, ctx) => {
|
|
35
|
+
if (!val)
|
|
36
|
+
return undefined;
|
|
37
|
+
const date = new Date(val);
|
|
38
|
+
if (Number.isNaN(date.getTime())) {
|
|
39
|
+
ctx.addIssue({
|
|
40
|
+
code: "custom",
|
|
41
|
+
message: message ?? "Invalid date format",
|
|
42
|
+
});
|
|
43
|
+
return z.NEVER;
|
|
44
|
+
}
|
|
45
|
+
return date;
|
|
46
|
+
});
|
|
45
47
|
}
|
|
46
48
|
/**
|
|
47
|
-
*
|
|
49
|
+
* A non-empty string. Empty strings produce a validation error.
|
|
50
|
+
* Chain with .optional() to also accept undefined.
|
|
48
51
|
*/
|
|
49
|
-
export function
|
|
50
|
-
return
|
|
52
|
+
export function zNonEmptyString(message) {
|
|
53
|
+
return z.string().refine((val) => val !== "", {
|
|
54
|
+
message: message ?? "String must not be empty",
|
|
55
|
+
});
|
|
51
56
|
}
|
|
52
57
|
//# sourceMappingURL=zod-utils.js.map
|