-
Notifications
You must be signed in to change notification settings - Fork 184
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
when using composition, validation fails on aliased properties #438
Comments
From memory, we ignor composition when we pass params to dry-validation purposes. The idea is to validate your input, not the format of the output (composition). Happy to discuss changing the behavior but it would have to be configurable. |
Hi @fran-worley. To clarify your last comment do you mean validating composed forms is not currently possible in Reform? If that is not the case then what is the correct syntax to validate the form in @lastobelus comment? I'm having the same problem and looked at docs and google but to no avail. |
I hit the same problem trying to use composition. {} And the following hash is passed to dry-validation: {:album=>{"id"=>111, "title"=>"bob"}, :song=>{"id"=>nil}} Therefore it doesn't find any The following example works though: context "validate id" do
Song = Struct.new(:id, :title)
Album = Struct.new(:id, :title)
class ComposedForm < Reform::Form
include Composition
property :album_id, on: :album, from: :id
property :title, on: :album
property :song_id, on: :song, from: :id
validation do
required(:album).schema do
required(:id).filled
required(:title).filled
end
end
end
context "composed form" do
it "validates id" do
form = ComposedForm.new(album: Album.new(111, "bob"), song: Song.new)
form.validate({})
expect(form.to_result.errors).to be_empty
end
end
end The trick is to validate a nested hash with dry-validation to match the hash that is passed to it by Reform. |
This is the bit of code that reform uses to transform the input params for dry validation: I'm unsure what your expectation is (confession - I've never needed composition myself). |
Since I was declaring an |
@karouf updating the docs is one thing, but if composition users find it counter-intuitive there might be a better way of doing things no? Can I assume that the reproduction example from @lastobelus is what your expectation is? |
By the way I tested that on Sure I agree that it would benefit from being more intuitive. I was just saying that it's not a show-stopper either, as I was led to believe when I first stumble upon this issue. I would need to have a look at the behavior in |
@karouf cool, glad we are on the same page. There are no formal composition/dry validation test cases, probably why the implementation has changed between versions. I'll use @lastobelus code to put a test case in for us to work towards. Thanks everyone 👍 |
Hi there. From what I'm experiencing today, it's not limited to composition. When creating a simple form that aliases a property with ❤️ |
Complete Description of Issue
When using composition, if you define an "aliased" property using
on:, from:
,required
validation does not work on that property.Steps to reproduce
Expected behavior
The above spec should pass. Inspecting the form verifies
form.album_id
is present.Actual behavior
Spec fails, form has "album_id: is missing" error:
System configuration
Reform version: 2.3.0rc1
The text was updated successfully, but these errors were encountered: