-
-
Notifications
You must be signed in to change notification settings - Fork 110
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
Allow Dry::Validation::Contract
rule definitions in Hanami::Action
#434
Conversation
@danhealy FYI, looks like |
@danhealy This patch works exceedingly well for me! Previously I was using this great code from @katafrakt https://github.com/katafrakt/palaver/blob/d1a31fec7dba6638ec798c419cecf35ce826f28e/lib/palaver/action.rb#L18-L25 but I've been able to remove that and all of my |
Dry::Validation::Contract
rule definitions in Hanami::Action
Co-authored-by: Philip Arndt <git@p.arndt.io>
d7adbbc
to
eb1e909
Compare
Thank you for the feedback @parndt ! I removed the extra guard and rebased on |
@danhealy awesome, I pushed a fix for rubocop offenses to your branch so that CI will not fail on that. |
I'll fix the remaining style issues |
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 works really well in my app!
nice, your fixes mirror exactly how I was going to do it. |
Woohoo ✅ CI 🎉 |
Thanks so much for this, @danhealy! Got it done by conference end, amazing stuff 😍 I'm just about to enter into various metal tubes for 24h, so I'll look at this once I'm back early next week. |
@danhealy Thanks again for making this! So it turns out in America you don't have to go through customs before leaving the country, which means I have a bit of bonus computer time! I won't be able to go through the code in detail yet, but do have one thought here for you consideration: After seeing this in totality, I wonder if a better API for this would be (building from your example): params do
required(:start_date).value(:date)
required(:end_date).value(:date)
end
rules do
rule(:start_date, :end_date) do
if start_date > end_date
base.failure('event cannot end before it starts')
end
end
end In this way, we just have two clear, well-contained blocks for action params validation: In such an approach, I think we'd just take the block passed to Now, if we did take this approach, it does make me wonder if contract do
params do
required(:start_date).value(:date)
required(:end_date).value(:date)
end
rule(:start_date, :end_date) do
if start_date > end_date
base.failure('event cannot end before it starts')
end
end
end Having As you know from our earlier discussions, @danhealy, I think it's worth us thoroughly exploring all our API design options here. As someone who's been active in this space now, I'm interested in your thoughts on the above! And thanks again for your code contributions — having these changes be real and concrete and right here in front of our eyes is going to help so much for us to get this much needed feature done :) In the meanwhile, I'll continue to think on this. Will get back to you again when I'm (a) in Australia, and (b) have any further developed thoughts to add. -- @parndt — I love your enthusiasm here. 😉 Technically, we've frozen 2.1, and our attention should be on making sure any fixes to existing features are squared away before considering other things. For this feature, as you can see from my thoughts above, I don't think it's so cut and dry and ready to go that we can just drop this in before the release. I want to make sure we give ourselves the time to arrive at the most helpful design. In addition, @jodosha's attention has been on our assets fixes and I don't want to rush him with this one either. If you have thoughts about my musings above, though, Phil, I'm definitely keen to hear them too! |
Personally, I quite like |
Yeah, I should make this clear: I would like to keep The reason I'm not so keen (at this point, at least!) on having many individual IMO, the fact that this is using dry-validation is not just a minor implementation detail: we should be making this clear to users, for many reasons:
In this way, I like the |
I think I prefer the current implementation for the following reasons:
My complaints with the current implementation:
Maybe the current DSL + an alternative, explicit receiver for a contract instance would be the best balance? I don't have a good conceptual judgement about the balance of the ease of use versus potentially surprising behavior of Also, my opinions here include a caveat that they are based on abstract feelings rather than actual usage. |
Hey @danhealy, thanks for this enhancement. 👏 TL;DR: Here's my proposal:
SemVer compatibilityWe need to keep Why
|
Thanks for checking this out and sharing your preference, @jodosha :) As @danhealy knows from our (in person! so cool) discussions on this, I didn't have one strong opinion about the exact shape of the API for this, so having you share your thoughts here helps a lot. I'm happy to go with your suggestion. It's straightforward and provides a single integration point: "contract" builds or accepts a The only downside is that for actions that don't need a contract rules, we get this double nesting, which isn't the ideal experience:
An option here could be to keep (Another option here could be to rejig dry-validation so that it doesn't need a nested params block like that, but that's an idea for another day!) @danhealy — since I think we have a clear path now, would you be up for adjusting this PR? Thanks again! ❤️ |
Cool - please let me know when you'd like me to test it out with my app 😄 |
Is this still the direction we're headed? |
@parndt Yes indeed, I'd be happy to review a revised solution here. |
Sorry for letting this sit so long! The proposal by @jodosha is reasonable, supporting the syntax will be a bit more substantial of a change because it would first depend on modification to |
For a
Validatable
Hanami::Action
(includinghanami-validations
), calls torule
inside theHanami::Action
are passed through to theDry::Validation::Contract
class via theHanami::Action::Params
class.Reversing the order of
params
andrule
will raise an exception, this is the same constraint thatDry::Validation::Contract
has.#421