-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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(cards): validate card security code and expiration #874
Conversation
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.
Could you please move the logic in the constructors to the TryFrom
trait implementations for CardSecurityCode
, CardExpirationMonth
and CardExpirationYear
?
So:
CardSecurityCode::new()
would becomeCardSecurityCode::try_from(u16)
CardExpirationMonth::new()
would becomeCardExpirationMonth::try_from(u8)
CardExpirationYear::new()
would becomeCardExpirationYear::try_from(u16)
And regarding the Serialize
and Deserialize
implementations, would you want to take them up in this PR or a separate one? Let us know if you have any questions.
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.
Also look into adding serde::{Serialize, Deserialize}
implementations with appropriate error reporting. Thanks for taking this up!
@SanchithHegde should I remove the new implementation and just implement a TryFrom instance or keep both ? |
Yes, remove the |
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.
Other than that, looks good to me! Thanks again for the PR!
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.
Other than that, looks good to me!
let valid_card_security_code = CardSecurityCode::try_from(1234).unwrap(); | ||
|
||
// will panic on unwrap | ||
let invalid_card_security_code = CardSecurityCode::try_from(12); | ||
|
||
assert_eq!(*valid_card_security_code.peek(), 1234); | ||
assert!(invalid_card_security_code.is_err()); | ||
|
||
let serialized = serde_json::to_string(&valid_card_security_code).unwrap(); | ||
assert_eq!(serialized, "1234"); | ||
|
||
let derialized = serde_json::from_str::<CardSecurityCode>(&serialized).unwrap(); |
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.
Not a blocker but prefer .expect("what was expected")
over .unwrap()
since it makes finding the cause of panic easier.
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.
LGTM
@divinenaman Please address CI checks. |
Type of Change
Description
Add basic validations for card security codes (CVC/CVV/CID code) and card expiration month and year. This logic resides in a new crate called cards. Validations included (as of now):
Additional Changes
Motivation and Context
This fixes #607
How did you test it?
run
cargo test --cards
Checklist
cargo +nightly fmt --all
cargo clippy