dry-types icon indicating copy to clipboard operation
dry-types copied to clipboard

Support for comma as decimal separator

Open dush opened this issue 6 years ago • 3 comments

Types::Params::Float and Types::Params::Decimal should coerce number parameters with comma as decimal separatotor (eg "12,34") ISO8601 allows using comma "," or full stop "." as decimal separator. Of these, the comma is the preferred sign

dush avatar Mar 14 '19 12:03 dush

We could add support for this as either a separate type or based on additional configuration. We need to use regexps to check the format and that is very slow, that's why this should be an opt-in behavior.

solnic avatar Mar 18 '19 11:03 solnic

I know that I can define my own type that does it, but as I understand Params category of dry-types is intended for HTTP params processing which is often user input. I think it could be supported by default as roughly half of the world will write decimal/float numbers wit comma as separator and it is even in ISO standard. If you need performance than you can just use Types::Coercible::Decimal or Float. May be you don't even need regexp, you might just try Float(input.tr(',', '.')) if input is a String.

dush avatar Mar 18 '19 14:03 dush

It's not that simple 😄 Check out this table to get an idea: https://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html

One of the reasons why dry-types was created was to remove generic coercions. We want coercions to be as narrow as possible in order to avoid situations where something is coerced by a mistake or incorrectly. I think people should be 100% clear regarding the number format they want to support and we should have types that make supporting various formats as simple as possible. For instance I would define validation rules for formats that my system is supposed to support and have corresponding coercion types (if needed). This way you remove any ambiguity from your coercion/validation layer.

We can really make this quite simple for the users. ie we could add predefined coercion types that we could access by country identifiers or some other identifiers that could be understood by people. What we should not do is blindly assuming that the comma is used in one way or the other.

solnic avatar Mar 19 '19 10:03 solnic