@digitraffic/common 2026.2.17-2 → 2026.2.17-3

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.
@@ -2,30 +2,25 @@ 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
- * myParameter: zStringToDate("Invalid value of myParameter").optional()
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
- * Optional string query parameter. Empty strings are treated as undefined.
16
+ * Parses a string to a number. Empty strings are treated as undefined.
22
17
  */
23
- export declare function zOptionalString(): z.ZodType<string | undefined>;
18
+ export declare function zStringToNumber(message?: string): z.ZodPipe<z.ZodString, z.ZodTransform<number | undefined, string>>;
24
19
  /**
25
- * Optional number query parameter parsed from a string. Empty strings are treated as undefined.
20
+ * Parses a string to a Date. Empty strings are treated as undefined.
26
21
  */
27
- export declare function zOptionalNumber(message?: string): z.ZodType<number | undefined>;
22
+ export declare function zStringToDate(message?: string): z.ZodPipe<z.ZodString, z.ZodTransform<Date | undefined, string>>;
28
23
  /**
29
- * Optional date query parameter parsed from a string. Empty strings are treated as undefined.
24
+ * Optional string. Empty strings are treated as undefined.
30
25
  */
31
- export declare function zOptionalDate(message?: string): z.ZodType<Date | undefined>;
26
+ export declare function zOptionalString(): z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
@@ -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
- * myParameter: zStringToDate("Invalid value of myParameter").optional()
10
+ * requiredParam: zStringToDate("Invalid value of myParameter"),
11
+ * optionalParam: zStringToDate().optional(),
10
12
  * });
11
13
  *
12
14
  */
15
+ /**
16
+ * Parses a string to a number. Empty strings are treated as undefined.
17
+ */
13
18
  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
+ 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;
19
28
  });
20
29
  }
30
+ /**
31
+ * Parses a string to a Date. Empty strings are treated as undefined.
32
+ */
21
33
  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",
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;
27
46
  });
28
47
  }
29
48
  /**
30
- * Transforms an empty string to undefined. Useful for optional query parameters
31
- * that may arrive as empty strings.
32
- */
33
- export const zEmptyStringToUndefined = z.literal("").transform(() => undefined);
34
- /**
35
- * Optional string query parameter. Empty strings are treated as undefined.
49
+ * Optional string. Empty strings are treated as undefined.
36
50
  */
37
51
  export function zOptionalString() {
38
- return zEmptyStringToUndefined.or(z.string().optional());
39
- }
40
- /**
41
- * Optional number query parameter parsed from a string. Empty strings are treated as undefined.
42
- */
43
- export function zOptionalNumber(message) {
44
- return zEmptyStringToUndefined.or(zStringToNumber(message).optional());
45
- }
46
- /**
47
- * Optional date query parameter parsed from a string. Empty strings are treated as undefined.
48
- */
49
- export function zOptionalDate(message) {
50
- return zEmptyStringToUndefined.or(zStringToDate(message).optional());
52
+ return z
53
+ .string()
54
+ .optional()
55
+ .transform((val) => (val === "" ? undefined : val));
51
56
  }
52
57
  //# sourceMappingURL=zod-utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitraffic/common",
3
- "version": "2026.2.17-2",
3
+ "version": "2026.2.17-3",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "repository": {