@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.
@@ -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
- * 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
+ * A non-empty string. Empty strings produce a validation error.
25
+ * Chain with .optional() to also accept undefined.
30
26
  */
31
- export declare function zOptionalDate(message?: string): z.ZodType<Date | undefined>;
27
+ export declare function zNonEmptyString(message?: string): z.ZodString;
@@ -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
  */
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
- * Transforms an empty string to undefined. Useful for optional query parameters
31
- * that may arrive as empty strings.
16
+ * Parses a string to a number. Empty strings are treated as undefined.
32
17
  */
33
- export const zEmptyStringToUndefined = z.literal("").transform(() => undefined);
34
- /**
35
- * Optional string query parameter. Empty strings are treated as undefined.
36
- */
37
- export function zOptionalString() {
38
- return zEmptyStringToUndefined.or(z.string().optional());
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
- * Optional number query parameter parsed from a string. Empty strings are treated as undefined.
31
+ * Parses a string to a Date. Empty strings are treated as undefined.
42
32
  */
43
- export function zOptionalNumber(message) {
44
- return zEmptyStringToUndefined.or(zStringToNumber(message).optional());
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
- * Optional date query parameter parsed from a string. Empty strings are treated as undefined.
49
+ * A non-empty string. Empty strings produce a validation error.
50
+ * Chain with .optional() to also accept undefined.
48
51
  */
49
- export function zOptionalDate(message) {
50
- return zEmptyStringToUndefined.or(zStringToDate(message).optional());
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
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-4",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "repository": {