Skip to content

fix: zod boolean coercion should correctly parse "true" and "false" #2049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/supervisor/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { randomUUID } from "crypto";
import { env as stdEnv } from "std-env";
import { z } from "zod";
import { AdditionalEnvVars, BoolEnv } from "./envUtil.js";
import { AdditionalEnvVars, BoolEnv, CoercedBoolean } from "./envUtil.js";

const Env = z.object({
// This will come from `spec.nodeName` in k8s
Expand Down Expand Up @@ -45,7 +45,7 @@ const Env = z.object({
// Used by the workload manager, e.g docker/k8s
DOCKER_NETWORK: z.string().default("host"),
OTEL_EXPORTER_OTLP_ENDPOINT: z.string().url(),
ENFORCE_MACHINE_PRESETS: z.coerce.boolean().default(false),
ENFORCE_MACHINE_PRESETS: CoercedBoolean.default(false),
KUBERNETES_IMAGE_PULL_SECRETS: z.string().optional(), // csv

// Used by the resource monitor
Expand Down
9 changes: 9 additions & 0 deletions apps/supervisor/src/envUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ export const AdditionalEnvVars = z.preprocess((val) => {
return undefined;
}
}, z.record(z.string(), z.string()).optional());

/**
* Zod's `z.coerce.boolean()` doesn't work as _expected_ with "true" and "false" strings.
* as it coerces both to `true`. This type is a workaround for that.
*/
export const CoercedBoolean = z.union([
z.boolean(),
z.enum(["true", "false"]).transform((v) => v === "true"),
]);
3 changes: 2 additions & 1 deletion apps/webapp/app/components/runs/v3/RunFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
TaskRunStatusCombo,
} from "./TaskRunStatus";
import { TaskTriggerSourceIcon } from "./TaskTriggerSource";
import { CoercedBoolean } from "~/utils/zod";

export const TaskAttemptStatus = z.enum(allTaskRunStatuses);

Expand Down Expand Up @@ -83,7 +84,7 @@ export const TaskRunListSearchFilters = z.object({
period: z.preprocess((value) => (value === "all" ? undefined : value), z.string().optional()),
from: z.coerce.number().optional(),
to: z.coerce.number().optional(),
rootOnly: z.coerce.boolean().optional(),
rootOnly: CoercedBoolean.optional(),
batchId: z.string().optional(),
runId: z.string().optional(),
scheduleId: z.string().optional(),
Expand Down
9 changes: 5 additions & 4 deletions apps/webapp/app/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { z } from "zod";
import { SecretStoreOptionsSchema } from "./services/secrets/secretStoreOptionsSchema.server";
import { isValidDatabaseUrl } from "./utils/db";
import { isValidRegex } from "./utils/regex";
import { CoercedBoolean } from "./utils/zod";

const EnvironmentSchema = z.object({
NODE_ENV: z.union([z.literal("development"), z.literal("production"), z.literal("test")]),
Expand Down Expand Up @@ -50,7 +51,7 @@ const EnvironmentSchema = z.object({
RESEND_API_KEY: z.string().optional(),
SMTP_HOST: z.string().optional(),
SMTP_PORT: z.coerce.number().optional(),
SMTP_SECURE: z.coerce.boolean().optional(),
SMTP_SECURE: CoercedBoolean.optional(),
SMTP_USER: z.string().optional(),
SMTP_PASSWORD: z.string().optional(),

Expand Down Expand Up @@ -338,7 +339,7 @@ const EnvironmentSchema = z.object({
ALERT_RESEND_API_KEY: z.string().optional(),
ALERT_SMTP_HOST: z.string().optional(),
ALERT_SMTP_PORT: z.coerce.number().optional(),
ALERT_SMTP_SECURE: z.coerce.boolean().optional(),
ALERT_SMTP_SECURE: CoercedBoolean.optional(),
ALERT_SMTP_USER: z.string().optional(),
ALERT_SMTP_PASSWORD: z.string().optional(),
ALERT_RATE_LIMITER_EMISSION_INTERVAL: z.coerce.number().int().default(2_500),
Expand Down Expand Up @@ -378,7 +379,7 @@ const EnvironmentSchema = z.object({
MAX_SEQUENTIAL_INDEX_FAILURE_COUNT: z.coerce.number().default(96),

LOOPS_API_KEY: z.string().optional(),
MARQS_DISABLE_REBALANCING: z.coerce.boolean().default(false),
MARQS_DISABLE_REBALANCING: CoercedBoolean.default(false),
MARQS_VISIBILITY_TIMEOUT_MS: z.coerce
.number()
.int()
Expand Down Expand Up @@ -452,7 +453,7 @@ const EnvironmentSchema = z.object({
RUN_ENGINE_TIMEOUT_PENDING_CANCEL: z.coerce.number().int().default(60_000),
RUN_ENGINE_TIMEOUT_EXECUTING: z.coerce.number().int().default(60_000),
RUN_ENGINE_TIMEOUT_EXECUTING_WITH_WAITPOINTS: z.coerce.number().int().default(60_000),
RUN_ENGINE_DEBUG_WORKER_NOTIFICATIONS: z.coerce.boolean().default(false),
RUN_ENGINE_DEBUG_WORKER_NOTIFICATIONS: CoercedBoolean.default(false),
RUN_ENGINE_PARENT_QUEUE_LIMIT: z.coerce.number().int().default(1000),
RUN_ENGINE_CONCURRENCY_LIMIT_BIAS: z.coerce.number().default(0.75),
RUN_ENGINE_AVAILABLE_CAPACITY_BIAS: z.coerce.number().default(0.3),
Expand Down
9 changes: 9 additions & 0 deletions apps/webapp/app/utils/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ export const CoercedDate = z.preprocess((arg) => {

return arg;
}, z.date().optional());

/**
* Zod's `z.coerce.boolean()` doesn't work as _expected_ with "true" and "false" strings.
* as it coerces both to `true`. This type is a workaround for that.
*/
export const CoercedBoolean = z.union([
z.boolean(),
z.enum(["true", "false"]).transform((v) => v === "true"),
]);