Skip to content
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

Dry-validation: Unsupported error format in case (input predicate) #403

Open
pvmeerbe opened this issue Sep 27, 2016 · 5 comments
Open

Dry-validation: Unsupported error format in case (input predicate) #403

pvmeerbe opened this issue Sep 27, 2016 · 5 comments

Comments

@pvmeerbe
Copy link

This ticket was created when using the 'improve-dry-integration' branch created by @fran-worley

2 different error outputs formats are available when using dry-validation (tested with dry-validation 0.10.3)

  • Hash using the related field name as key

    Dry::Validation.Schema {required(:bla).filled}.call({:ff => 'jkhjk'}).messages
    => {:bla=>["is missing"]}

  • Array listing global errors

    Dry::Validation.Schema {input(:hash?)}.call([]).messages
    => ["must be a hash"]

Currently only the Hash format is supported:

Quick fix could be to re-introduce the 'base' keyword similar to AR, for example http://pastebin.com/X9vWAXjC. Perhaps a better solution is available?

@fran-worley
Copy link
Collaborator

This would need to be handled in Representable first as if you try it will attempt to deserialize your input params before running validation and will trigger an error as Representable expects a hash.

Can you remind me why you want to be able to pass anything other than a hash to validate?

@pvmeerbe
Copy link
Author

It's not me who wants to pass anything else, it's dry-validation ;). Apparently in most cases it returns a Hash, but in some cases (like when using the 'input' keywords) it returns an array

Just tested it again with dry-validation version 0.10.3 :

  Dry::Validation.Schema {input(:hash?)}.call([]).messages
   => ["must be a hash"]

Typical example could be validating a REST API parameter set. I want to be sure a Hash is received and not an Array to prevent my code to blow up (which actually happens when using Grape)

@apotonick
Copy link
Member

Could you show your form that triggers this behaviour? I don't understand how that can happen?!?!

@fran-worley
Copy link
Collaborator

@apotonick I stopped working on this because reform requires inputs to be a hash. End of so you won't ever be able to use this type of rule via Reform.

The only way to support this would be

@pvmeerbe
Copy link
Author

@apotonick I don't think a form will trigger this (except if someone is manually constructing form posts of course). However when using reform with a public REST API IMHO it seems better to always validate input data as much as possible, i.e. validate data at the start so it can be trusted to be in the correct format for future use in the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants