Dry-v feature suggestion: returning an array, not a hash, from errors

In ActiveModel if you call errors.full_messages, you get an Array, e.g.:

model.errors.full_messages # => ["Age must be present", "Age must be at least 18", "Name must be 20 characters or less"]

This means I can do something simple like this in my ERB:

<ul>
  <% @model.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
</ul>

Whereas dry-v’s (errors: true) returns a hash, something like:

result.errors(full: true)
# => {age: ["age must be present", "age must be at least 18"], name: ["name must be 20 characters or less"]}

Which means my ERB has to get more complicated:

<ul>
  <% @form.errors(full: true).each do |_, msgs| %>
    <% msgs.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
  <% end %>
</ul>

Life would be easier (and it would ease my transition from ActiveModel to dry-v) if I could pass an option to errors which makes it return an array of full messages:

result.errors(full: true, flat: true)
# => ["age must be present", "age must be at least 18", "name must be 20 characters or less"]

Would you accept this feature if I added it?

Yes this sounds like a good addition. I gotta be honest that I don’t remember why it was decided to use a hash for full messages :confused:

Well in that case, if you don’t mind the breaking change how about changing it so that (full: true) simply returns an array?

I can’t think of any use case where I’d want to get “full” messages as a hash.

Yes I’m OK with that. Please report an issue and I’ll schedule it for 1.0.0 release.

Done: https://github.com/dry-rb/dry-validation/issues/286

1 Like