-
Notifications
You must be signed in to change notification settings - Fork 41
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
Conversation
modified: z.coerce.date(), | ||
owner: AddressSchema, | ||
signature: HexSchema, | ||
signatureType: z.nativeEnum(SignatureType), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Pull Request Test Coverage Report for Build 8387571407Details
💛 - Coveralls |
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); | ||
}, | ||
); | ||
}); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks!
Summary
This migrates the validation of
Message
tozod
.Changes
MessageValidator
and associated schemas.MessageSchema
/MessageConfirmationSchema
and infer types from them.