Skip to content

Commit cfbc156

Browse files
committed
fix: issues with number parsing
1 parent 434d7f5 commit cfbc156

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

apps/api/src/configuration/configuration.schema.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isNumberLike, parseNumber } from '@douglasneuroinformatics/libjs';
12
import { $BooleanString } from '@opendatacapture/schemas/core';
23
import { z } from 'zod';
34

@@ -10,18 +11,27 @@ const $OptionalURL = z.preprocess(
1011
.transform((arg) => (arg ? new URL(arg) : undefined))
1112
);
1213

14+
const $ParsedNumber = <TSchema extends z.ZodTypeAny>(schema: TSchema) => {
15+
return z.preprocess((arg) => {
16+
if (!isNumberLike(arg)) {
17+
return undefined;
18+
}
19+
return parseNumber(arg);
20+
}, schema);
21+
};
22+
1323
export const $Configuration = z
1424
.object({
15-
API_DEV_SERVER_PORT: z.coerce.number().positive().int().optional(),
16-
API_PROD_SERVER_PORT: z.coerce.number().positive().int().default(80),
17-
API_RESPONSE_DELAY: z.coerce.number().positive().int().optional(),
25+
API_DEV_SERVER_PORT: $ParsedNumber(z.number().positive().int().optional()),
26+
API_PROD_SERVER_PORT: $ParsedNumber(z.number().positive().int().default(80)),
27+
API_RESPONSE_DELAY: $ParsedNumber(z.number().positive().int().optional()),
1828
DANGEROUSLY_DISABLE_PBKDF2_ITERATION: $BooleanString.default(false),
1929
DEBUG: $BooleanString,
2030
GATEWAY_API_KEY: z.string().min(32),
21-
GATEWAY_DEV_SERVER_PORT: z.coerce.number().positive().int().optional(),
31+
GATEWAY_DEV_SERVER_PORT: $ParsedNumber(z.number().positive().int().optional()),
2232
GATEWAY_ENABLED: $BooleanString,
2333
GATEWAY_INTERNAL_NETWORK_URL: $OptionalURL,
24-
GATEWAY_REFRESH_INTERVAL: z.coerce.number().positive().int(),
34+
GATEWAY_REFRESH_INTERVAL: $ParsedNumber(z.number().positive().int()),
2535
GATEWAY_SITE_ADDRESS: $OptionalURL,
2636
MONGO_DIRECT_CONNECTION: z.string().optional(),
2737
MONGO_REPLICA_SET: z.string().optional(),

0 commit comments

Comments
 (0)