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

DEC-623: Switch from joi to zod #117

Merged
merged 1 commit into from
Nov 23, 2022
Merged

DEC-623: Switch from joi to zod #117

merged 1 commit into from
Nov 23, 2022

Conversation

luixo
Copy link
Contributor

@luixo luixo commented Oct 22, 2022

That's a big one with a lot of actual code migration, not only types.
Please look through this carefully.

@luixo luixo self-assigned this Oct 22, 2022
@vercel
Copy link

vercel bot commented Oct 22, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
developer-console-framework-prod ✅ Ready (Inspect) Visit Preview Nov 23, 2022 at 2:51PM (UTC)

@mpeterdev
Copy link
Collaborator

mpeterdev commented Oct 27, 2022

was hoping to get started reviewing this while waiting on updates to the previous PR, but I am unable to run the backend. Getting the following error
Screen Shot 2022-10-27 at 12 42 43 PM

}),
};

export const outputs = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there a need to use Zod for our outpout types? It is valuable for input checking but for outputs I think we could just return normal types based on Prisma

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no direct need for that.
I decided to do that to keep input and output types inference unified.
When constructing a new handler or changing an old one - it is way easier to wrap head around the same type structure.
Also, it will help keep flavored types strict.

Comment on lines +1 to +10
export type Flavored<
T extends string,
R extends string | number = string,
> = R & {
__flavor?: T;
};

export const flavored = <T extends string, R extends string | number>(
x: R,
): x is Flavored<T, R> => true;
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you explain the flavored types and their value please?

Copy link
Collaborator

Choose a reason for hiding this comment

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

found some context in this issue but would like to hear the motivation from your perspective

colinhacks/zod#678

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Flavored types catches error when we pass a string value that is a key (say, project slug) to the function that consumes a string, but of different type (say, contract slug).
I chose flavored over branded because we would have to wrap around a lot of actual branding to run those while flavored are lightweight and there is no need in heavy lifting.
I suggest to check out if migrating to branding types is going to actually be easy, it could be a more strict check on passing around string'd types.

@luixo luixo changed the base branch from refactor/unify-types to refactor/pct-num-account-balance November 8, 2022 13:17
@luixo
Copy link
Contributor Author

luixo commented Nov 8, 2022

I factored out a lot of PRs from here so this one is now only about actual validating library switch

@luixo
Copy link
Contributor Author

luixo commented Nov 16, 2022

I'll merge this as soon as the last prerequisite is merged, there is our last chance to overview the migration!

@@ -152,7 +153,7 @@ export class TriggeredAlertsService {
slug,
name: alert.name,
alertId: alert.id,
type: this.alertsService.toAlertType(rule),
type: this.alertsService.toAlertType(rule) as Alerts.RuleType,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No idea why I had to add this though toAlertType returns exactly this type :(

// { from: number, to: number } |
// { from?: number, to: number } |
// { from: number, to?: number }
// to validate this instead?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a valid question

| CreateEmailDestinationConfig
| CreateTelegramDestinationConfig;
};
export const rule = z.union([
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should be discriminatedUnion (it would help debugging the 400s), but refined objects can no longer be used in discriminatedUnion (it was fixed here, but not released yet).

};
const enabledDestination = databaseDestination
.pick({ id: true, name: true })
.merge(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Generally, you want to use merge to have an intersection of two objects, because and is "dumb", but intersection may also work.

Be cautious with these.

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.

2 participants