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
Apply constructor fn before checking validity of input #277
Apply constructor fn before checking validity of input #277
Conversation
type.valid?(value) | ||
type.valid?(fn[value]) | ||
rescue NoMethodError, TypeError | ||
false |
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.
This rescue
clause is not a good idea, because it will be hiding bugs in code, so please remove
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.
Hey, thanks for the review.
My line of reasoning for this is as follows:
- The spec for the
#valid
method asserts that it returns a boolean. - If the constructor block can't be applied to the input, the type is not valid, therefore returning
false
. In order for this to happen, I need to rescue the error. - Letting the error bubble up makes sense when trying to construct the type but it shouldn't happen when checking whether it's valid or not.
I think this is a design decision that needs to be made and you know the project much better than I do. If you're certain that this is the way to go I will gladly change it, but I will need to change some of the specs as well because they will be broken.
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.
Maybe a cleaner option would be to have the #valid?
method defined as such:
def valid?(value)
constructed_value = fn[value]
rescue NoMethodError, TypeError
false
else
type.valid?(constructed_value)
end
This way we only rescue errors that happen when trying to apply the block to the value.
WDYT?
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.
So, I think it's worth looking into all the places where valid?
is used and making sure that this is what we want. I wouldn't just look at the spec because it doesn't give enough explanation why this method exists and how/when it's used.
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.
One thing to keep in mind. As of now, valid?
is aliased ===
. ===
in turn shouldn't raise exceptions, I'm pretty much sure...
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.
As far as I can tell after checking all occurrences in the repo, valid?
is only used for control flow, so a boolean should be fine. I also checked dry-struct since it's based on dry-types but it doesn't use it either. If there's anything else worth checking, let me know.
Meanwhile, I'm going to push the cleaner version of the method as described above.
@solnic Hi Piotr, is there anything else I can do regarding this PR? I also tested Let me know if I should rebase it as well. |
@flash-gordon are you OK with merging this? |
This PR addresses the following incorrect behavior for
Constructor#valid?
, where a type with a constructorfn
does not apply it to the value before checking its validity:After the changes, the call to
#valid?
above will returntrue
.Since it's just a check, a rescue clause was added in order to always return a boolean.