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

Migrate Message/MessageConfirmation to zod #1315

Merged
merged 4 commits into from
Mar 22, 2024
Merged

Migrate Message/MessageConfirmation to zod #1315

merged 4 commits into from
Mar 22, 2024

Conversation

iamacook
Copy link
Member

Summary

This migrates the validation of Message to zod.

Changes

  • Remove MessageValidator and associated schemas.
  • Add test-covered MessageSchema/MessageConfirmationSchema and infer types from them.
  • Propagate type requirements.
  • Update tests accordingly.

@iamacook iamacook self-assigned this Mar 18, 2024
modified: z.coerce.date(),
owner: AddressSchema,
signature: HexSchema,
signatureType: z.nativeEnum(SignatureType),
Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't add a fallback here as we control this type and would otherwise need to extend SignatureType with UNKNOWN, for example. Would appreciate your thoughts.

Copy link
Member

Choose a reason for hiding this comment

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

I think it's OK to not have a fallback value, as you said we own this type internally and it haven't changed for years. And if it does and we don't notice, we should be able to catch the related errors in development environments.

@coveralls
Copy link

coveralls commented Mar 18, 2024

Pull Request Test Coverage Report for Build 8387571407

Details

  • 17 of 17 (100.0%) changed or added relevant lines in 6 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.01%) to 93.287%

Totals Coverage Status
Change from base Build 8387467140: 0.01%
Covered Lines: 6429
Relevant Lines: 6647

💛 - Coveralls

@iamacook iamacook marked this pull request as ready for review March 18, 2024 12:59
@iamacook iamacook requested a review from a team as a code owner March 18, 2024 12:59
Comment on lines 222 to 246
it('should allow empty pages', () => {
const messagePage = pageBuilder().with('results', []).build();

const result = MessagePageSchema.safeParse(messagePage);

expect(result.success).toBe(true);
});

it.each<keyof Page<Message>>(['count', 'next', 'previous', 'results'])(
'should not allow %s to be undefined',
(key) => {
const messagePage = pageBuilder().with('results', []).build();
delete messagePage[key];

const result = MessagePageSchema.safeParse(messagePage);

expect(
!result.success &&
result.error.issues.length === 1 &&
result.error.issues[0].path.length === 1 &&
result.error.issues[0].path[0] === key,
).toBe(true);
},
);
});
Copy link
Member

Choose a reason for hiding this comment

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

Nit: these two tests are independent of the type of the items in the Page, what about extracting them to a page.schema.spec.ts? Otherwise we would be repeating them for all the Page<T> types.

This is not an strong opinion btw, and it would also require to export PageSchema somewhere as well, so I think this can be tackled in a separate PR if you agree with the suggestion.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a valid point, which I agree with. I removed them in 1a939c7 and will create a follow up PR for the PageSchema.

Copy link
Member

Choose a reason for hiding this comment

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

Great, thanks!

@iamacook iamacook merged commit edc276d into main Mar 22, 2024
16 checks passed
@iamacook iamacook deleted the message-zod branch March 22, 2024 14:13
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.

None yet

3 participants