Skip to content
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

feat: convert last zod usages to typebox #1232

Merged
merged 3 commits into from
Mar 24, 2025
Merged

feat: convert last zod usages to typebox #1232

merged 3 commits into from
Mar 24, 2025

Conversation

roderik
Copy link
Member

@roderik roderik commented Mar 24, 2025

Summary by Sourcery

Migrates from Zod to TypeBox for schema validation in environment configuration and form components.

Enhancements:

  • Replaces Zod with TypeBox for schema validation in environment configuration files and form components.
  • Improves type safety and validation consistency by adopting TypeBox.

Copy link

sourcery-ai bot commented Mar 24, 2025

Reviewer's Guide by Sourcery

This pull request replaces Zod with TypeBox for schema validation in the dapp. The changes include updating the environment configuration, pincode form, and create API key form to use TypeBox schemas and resolvers. Zod has also been removed as a dependency.

Updated class diagram for ServerEnvironment

classDiagram
  class ServerEnvironment {
    <<Type>>
    +BETTER_AUTH_URL: string | undefined
    +NEXT_PUBLIC_APP_URL: string | undefined
    +NEXTAUTH_URL: string | undefined
    +SETTLEMINT_HASURA_ADMIN_SECRET: string
    +RESEND_API_KEY: string | undefined
    +GOOGLE_CLIENT_ID: string | undefined
    +GOOGLE_CLIENT_SECRET: string | undefined
    +GITHUB_CLIENT_ID: string | undefined
    +GITHUB_CLIENT_SECRET: string | undefined
    +SETTLEMINT_HD_PRIVATE_KEY: string
    +APP_URL: string | undefined
  }
Loading

Updated class diagram for ClientEnvironment

classDiagram
  class ClientEnvironment {
    <<Type>>
    +NEXT_PUBLIC_EXPLORER_URL: string | undefined
  }
Loading

Updated class diagram for CreateApiKeyFormValues

classDiagram
  class CreateApiKeyFormValues {
    <<Type>>
    +name: string
    +expiresIn: string
  }
Loading

File-Level Changes

Change Details Files
Replaced Zod with TypeBox for environment variable validation.
  • Replaced zod import with typebox and utility functions.
  • Updated serverEnvironmentSchema and clientEnvironmentSchema to use TypeBox's t.Object and t.String for defining the schema.
  • Replaced z.string().url() with t.String({ format: "uri" }) for URL validation.
  • Replaced z.string().min(1) with t.String({ minLength: 1 }) for minimum length validation.
  • Replaced z.string().regex() with t.String({ pattern: ... }) for regular expression validation.
  • Replaced z.infer with StaticDecode to infer the types from the TypeBox schema.
  • Replaced serverEnvironmentSchema.parse with safeParse for parsing the environment variables.
  • Added $id to both serverEnvironmentSchema and clientEnvironmentSchema.
  • Removed the transform function from the schema and instead added the APP_URL property after parsing.
kit/dapp/src/lib/config/environment.ts
Replaced Zod with TypeBox for the pincode form schema.
  • Replaced zod import with typebox and utility functions.
  • Updated pincodeSchema to use TypeBox's t.Object and t.String for defining the schema.
  • Replaced z.string().min(1, ...) with t.String({ minLength: 1, error: ... }) for minimum length validation with error message.
  • Replaced z.string().length(6, ...) with t.String({ length: 6, error: ... }) for exact length validation with error message.
  • Replaced z.infer with StaticDecode to infer the types from the TypeBox schema.
  • Replaced zodResolver with typeboxResolver for integrating with react-hook-form.
kit/dapp/src/components/blocks/auth/pincode-form.tsx
Replaced Zod with TypeBox for the create API key form schema.
  • Replaced zod import with typebox and utility functions.
  • Updated createApiKeySchema to use TypeBox's t.Object and t.String for defining the schema.
  • Replaced z.string().min(1, ...) with t.String({ minLength: 1, error: ... }) for minimum length validation with error message.
  • Replaced z.infer with StaticDecode to infer the types from the TypeBox schema.
  • Replaced zodResolver with typeboxResolver for integrating with react-hook-form.
kit/dapp/src/app/[locale]/(private)/portfolio/settings/api/_components/create-api-key-form.tsx
Removed Zod as a dependency.
  • Removed zod from the list of dependencies in package.json.
kit/dapp/package.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @roderik - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider creating a shared utility function for constructing TypeBox schemas with consistent metadata like $id.
  • It might be worth adding a changeset to document the removal of zod.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟡 Review instructions: 2 issues found
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

// TypeBox schema for the pincode form
const pincodeSchema = t.Object(
{
pincodeName: t.String({ minLength: 1, error: "Name is required" }),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (testing): Double-check that typebox custom error messages render as intended.

Since you are migrating from zod, ensure that the provided 'error' messages (e.g. for 'pincodeName' and 'pincode') propagate correctly through the typeboxResolver and are displayed appropriately in the UI.

GOOGLE_CLIENT_SECRET: z.string().optional(),
GITHUB_CLIENT_ID: z.string().optional(),
GITHUB_CLIENT_SECRET: z.string().optional(),
import { safeParse, t, type StaticDecode } from "@/lib/utils/typebox";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (review_instructions): The code is switching from Zod to TypeBox for schema validation, which is a good modernization, but there's a potential issue with error handling in the safeParse function.

The new implementation uses safeParse but doesn't appear to handle validation errors explicitly like Zod's .parse() method would throw. Make sure the safeParse function properly handles validation failures and throws appropriate errors to maintain the same level of validation safety that was present in the original code.

Review instructions:

Path patterns: **/*.ts

Instructions:
Always write correct, up to date, bug free, fully functional and working, secure, performant and efficient code.

}

export function getClientEnvironment(): ClientEnvironment {
return clientEnvironmentSchema.parse(process.env);
return safeParse(clientEnvironmentSchema, process.env);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (review_instructions): The getClientEnvironment function doesn't handle potential validation errors from safeParse.

Unlike Zod's parse method which would throw on validation errors, it's unclear if safeParse throws or returns a result object with validation status. If it returns an object with success/error properties (similar to Zod's safeParse), you should check for validation success before returning the result to maintain the same error behavior as the original code.

Review instructions:

Path patterns: **/*.ts

Instructions:
Always write correct, up to date, bug free, fully functional and working, secure, performant and efficient code.

@roderik roderik merged commit 692498e into main Mar 24, 2025
5 checks passed
@roderik roderik deleted the fix/zod-removal branch March 24, 2025 14:08
snigdha920 added a commit that referenced this pull request Mar 24, 2025
…rice

* main:
  chore(deps): update dependency @types/node to v22.13.13 (#1244)
  chore(deps): update github/codeql-action digest to 1b549b9 (#1241)
  feat: add input suggestions based on history (#1240)
  feat: add issue button to all table tabes (#1239)
  fix: type the validated fields array (#1238)
  fix: user balances in popup (#1237)
  fix: page titles and graphs (#1235)
  fix: rtl issues (#1233)
  feat: convert last zod usages to typebox (#1232)
  fix: collateral proof validity (#1231)
  fix: translated errors (#1229)
  fix: missing event details (#1230)
  fix: formfield postfix ui (#1228)
  fix: asset details and actions (#1227)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant