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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ghostdevv
Copy link
Contributor

@ghostdevv ghostdevv commented May 14, 2025

Closes #

✅ Checklist

  • I have followed every step in the contributing guide
  • The PR title follows the convention.
  • I ran and tested the code works

Testing

I was trying to setup SMTP with my self-hosted trigger.dev instance, and I kept running into the following error:

Failed to send email to [...], Magic sign-in link for Trigger.dev. Error Error: 00C83842D37D0000:error:0A00010B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:354:

After some debugging I realised it was because SMTP_SECURE=false or SMTP_SECURE="false" both get parsed by z.coerce.boolean() as true. The only way to succesfully get this to work, without this patch, is to completely omit the SMTP_SECURE variable such that the result is undefined (presumably) thanks to the .optional() modifier on the field.

https://zod.dev/?id=coercion-for-primitives
image

The fix I propose for this is the CoercedBoolean zod util I added, which accepts either a boolean or a string "true" or "false". We could also expand this to accept "1" and "0" but considering I couldn't see a use of this in the docs, I omitted it here.

This could be considered breaking, if someone was relying on the previous behaviour of any non-empty string equalling true.

Zod Playground


Changelog

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


Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of boolean values in environment variables and filters to ensure correct interpretation of "true" and "false" string inputs.
  • New Features
    • Introduced enhanced boolean coercion for filters and environment settings, providing more reliable validation and parsing of boolean fields.

Copy link

changeset-bot bot commented May 14, 2025

⚠️ No Changeset found

Latest commit: ca09724

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

coderabbitai bot commented May 14, 2025

Walkthrough

A custom Zod schema type, CoercedBoolean, was introduced to correctly coerce string representations of booleans ("true", "false") to their respective boolean values. This new type replaces the use of z.coerce.boolean() in several environment schemas and filter definitions across both backend and frontend code, ensuring accurate boolean coercion.

Changes

File(s) Change Summary
apps/supervisor/src/envUtil.ts
apps/webapp/app/utils/zod.ts
Added and exported a CoercedBoolean Zod schema that accurately coerces "true"/"false" strings to booleans.
apps/supervisor/src/env.ts Updated import to use CoercedBoolean and replaced z.coerce.boolean() with CoercedBoolean in environment schema.
apps/webapp/app/env.server.ts Replaced all uses of z.coerce.boolean() with CoercedBoolean in the environment variable schema definitions.
apps/webapp/app/components/runs/v3/RunFilters.tsx Changed the rootOnly filter in a Zod schema to use CoercedBoolean.optional() instead of z.coerce.boolean().optional().

Sequence Diagram(s)

sequenceDiagram
    participant EnvVarInput as Env Var Input
    participant CoercedBoolean as CoercedBoolean Schema
    participant AppLogic as Application Logic

    EnvVarInput->>CoercedBoolean: Provide input ("true", "false", true, false)
    CoercedBoolean->>CoercedBoolean: Coerce value to boolean
    CoercedBoolean-->>AppLogic: Return correct boolean (true/false)
    AppLogic->>AppLogic: Use coerced value in logic
Loading

Poem

In a warren of code, a bug did dwell,
Where "true" and "false" both rang the same bell.
With CoercedBoolean, we now discern—
The truth from the lies, for which we yearn!
Hopping along, our schemas are keen,
Booleans now clear, crisp, and clean.
🐇✨

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0661ee5 and ca09724.

📒 Files selected for processing (5)
  • apps/supervisor/src/env.ts (2 hunks)
  • apps/supervisor/src/envUtil.ts (1 hunks)
  • apps/webapp/app/components/runs/v3/RunFilters.tsx (2 hunks)
  • apps/webapp/app/env.server.ts (5 hunks)
  • apps/webapp/app/utils/zod.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (5)
apps/webapp/app/utils/zod.ts (1)
apps/supervisor/src/envUtil.ts (1)
  • CoercedBoolean (45-48)
apps/supervisor/src/env.ts (1)
apps/supervisor/src/envUtil.ts (1)
  • CoercedBoolean (45-48)
apps/supervisor/src/envUtil.ts (1)
apps/webapp/app/utils/zod.ts (1)
  • CoercedBoolean (28-31)
apps/webapp/app/components/runs/v3/RunFilters.tsx (1)
apps/webapp/app/utils/zod.ts (1)
  • CoercedBoolean (28-31)
apps/webapp/app/env.server.ts (1)
apps/webapp/app/utils/zod.ts (1)
  • CoercedBoolean (28-31)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - npm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - pnpm)
  • GitHub Check: typecheck / typecheck
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - npm)
  • GitHub Check: units / 🧪 Unit Tests
🔇 Additional comments (9)
apps/supervisor/src/env.ts (1)

4-4: Good usage of the new CoercedBoolean schema

The import and application of the new CoercedBoolean schema for ENFORCE_MACHINE_PRESETS ensures correct boolean coercion from string values. This addresses the issue where z.coerce.boolean() would incorrectly coerce the string "false" to true.

Also applies to: 48-48

apps/webapp/app/utils/zod.ts (1)

24-31: Well-implemented boolean coercion fix

This new CoercedBoolean schema correctly addresses the issue with Zod's default boolean coercion. The implementation properly handles both boolean values and the string literals "true" and "false", ensuring they're transformed to their correct boolean equivalents.

The explanatory comment is clear and helpful for future developers who might wonder why this custom implementation is necessary.

apps/webapp/app/components/runs/v3/RunFilters.tsx (1)

56-56: Good application of the CoercedBoolean schema

The import and usage of CoercedBoolean for the rootOnly field in the search filters schema is appropriate. This will ensure correct boolean parsing from URL search parameters, fixing potential issues where string representations like "false" were incorrectly interpreted as true.

Also applies to: 87-87

apps/supervisor/src/envUtil.ts (1)

41-48: Good implementation of the boolean coercion fix

This implementation of CoercedBoolean properly addresses the issue with Zod's default boolean coercion. The union of a boolean and an enum with a transform function ensures that string representations of booleans are correctly converted to their boolean equivalents.

The approach matches the implementation in the webapp, maintaining consistency across the codebase.

apps/webapp/app/env.server.ts (5)

5-5: Verify import of CoercedBoolean
Importing the new CoercedBoolean schema here is correct and aligns with the changes in ./utils/zod.ts. Confirm that this path is accurate and that CoercedBoolean is exported properly.


54-54: Replace z.coerce.boolean() with CoercedBoolean.optional() for SMTP_SECURE
This update ensures "false" is parsed correctly to false and prevents any non‐boolean string from being coerced to true.


342-342: Apply CoercedBoolean.optional() to ALERT_SMTP_SECURE
Consistent with SMTP_SECURE, using CoercedBoolean here will correctly coerce "true"/"false" strings into booleans.


382-382: Use CoercedBoolean.default(false) for MARQS_DISABLE_REBALANCING
Defaulting to false with the custom coercion avoids misinterpretation of string values (e.g., "false").


456-456: Set RUN_ENGINE_DEBUG_WORKER_NOTIFICATIONS with CoercedBoolean.default(false)
This ensures proper boolean parsing and default fallback for the debug notifications flag.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant