Rails 5 issue with nested schema

Hello,

i’m upgrading my 4.2 rails to 5.0. I use dry-validation 0.13.1.

I have a Dry::Validation.Params schema with nested schema like optional(:my_param).schema do .... It is expecting an Array or Hash, but now Rails 5 params/sub-params returns ActionController::Parameters instead of an Hash.

The error must be an Hash is raised.

Am i missing something ?

Thanks a lot !

Julien

You can call #to_hash and feed it into Dry::Validation.

Yes i tried it, but it needs to call .permit first. I cant because my fields are dynamics, i cant know in advance. I cant use permit … so no to_hash

Sorry, right now I have Rails behaviour a bit forgotten. But, if you are sanitizing with Dry::Validation I don’t see why you have to sanitize again with permit. You can just permit everything, call #to_hash and then sanitize with Dry:: Validation.

I belive you can use to_unsafe_hash.

2 Likes

What cpgo wrote is correct, you need to get back a normal hash from the strong params wrapper object, which is exactly what to_unsafe_hash does. This is fine (despite the name), since you’re going to validate/sanitize it using dry-validation anyway.

to_unsafe_hash works. Thanks all :slight_smile:

2 Likes