{"action":"opened","issue":{"url":"https://api.github.com/repos/dry-rb/dry-types/issues/76","repository_url":"https://api.github.com/repos/dry-rb/dry-types","labels_url":"https://api.github.com/repos/dry-rb/dry-types/issues/76/labels{/name}","comments_url":"https://api.github.com/repos/dry-rb/dry-types/issues/76/comments","events_url":"https://api.github.com/repos/dry-rb/dry-types/issues/76/events","html_url":"https://github.com/dry-rb/dry-types/issues/76","id":147910405,"number":76,"title":"Add form coercion for array types when they are ","user":{"login":"timriley","id":3134,"avatar_url":"https://avatars.githubusercontent.com/u/3134?v=3","gravatar_id":"","url":"https://api.github.com/users/timriley","html_url":"https://github.com/timriley","followers_url":"https://api.github.com/users/timriley/followers","following_url":"https://api.github.com/users/timriley/following{/other_user}","gists_url":"https://api.github.com/users/timriley/gists{/gist_id}","starred_url":"https://api.github.com/users/timriley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/timriley/subscriptions","organizations_url":"https://api.github.com/users/timriley/orgs","repos_url":"https://api.github.com/users/timriley/repos","events_url":"https://api.github.com/users/timriley/events{/privacy}","received_events_url":"https://api.github.com/users/timriley/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-04-13T00:20:22Z","updated_at":"2016-04-13T00:20:22Z","closed_at":null,"body":"This relates to https://github.com/dry-rb/dry-validation/issues/102, in which I outlined that there's no simple way to include empty arrays in HTML form submissions. We've been exploring thus further with our [formalist](http://github.com/icelab/formalist)-generated forms, and have come to this arrangement:\r\n\r\nWhen a form is expected to submit an array as one of its fields, but that array is empty, then instead replace it with something like this:\r\n\r\n```html\r\n<input type=\"hidden\" name=\"category_ids\">\r\n```\r\n\r\nThis means that we now get params like this being included in our form submission for an array-type field with nothing included:\r\n\r\n```ruby\r\n{\"category_ids\" => \"\"}\r\n```\r\n\r\nNow, if we had a schema like this:\r\n\r\n```ruby\r\nschema = Dry::Validation.Form do\r\n  key(:category_ids).each(:int?)\r\nend\r\n```\r\n\r\nWhen we run it with that input, right now we get an error, because there's no specific form coercion rules for arrays:\r\n\r\n```ruby\r\nschema.(\"category_ids\" => \"\").messages\r\n# => {:category_ids=>[\"must be an array\"]}\r\n```\r\n\r\nSince the empty string is an HTML form's standard \"empty value\", I'd like to propose that we add a form coercion for this when we expect the field to be an array. In this case, I'd propose that we actually coerce it to `nil`, like we do for other HTML type coercions here, like `int`:\r\n\r\n```ruby\r\ndef self.to_int(input)\r\n  return if empty_str?(input)\r\n\r\n  # ... rest of coercion logic ...\r\nend\r\n```\r\n\r\nBy coercing to `nil`, then we can have the flexibility to use a type in our validation schema with a default value (like an empty array), which is what you suggested in https://github.com/dry-rb/dry-validation/issues/102, @solnic.\r\n\r\nWould you be happy to have this coercion added to dry-types? I'll go ahead and build it if so.\r\n\r\nThanks!"}}