lulalala/adequate_errors

Error related value used in both formatted and raw ways

lulalala opened this issue · 1 comments

I have a validation on an amount value. It is checked against a dynamic minimum value (think it as some currency exchange). So if the amount is not over the minimum value, the error message will say "You still need X more in order to reach the minimum".

The X here is the difference between user entered amount and the minimum. There is a requirement to have it formatted as currency, with dividers, e.g. $1,234.56.

Usually I can just say errors.add(:foo, :not_enough, lacking: format_currency(num)) to have the formatted text in the error messages.

However I also have a json api, which needs this value as float, for some branching logic. I can't just use the lacking value in details, because it would be a string. I have to add the raw value errors.add(:foo, :not_enough, lacking: num, lacking_formatted: format_currency(num)).

If I want to avoid adding two keys, one for raw value and one for formatted valu, what kind of API would help?

@lulalala I would think its better to still have the separate keys as the values are different and it helps to be explicit. Although thinking it through, if the error is for an attribute, the attribute would have the raw value to begin with meaning that its already provided to the error message translation as a %{value} param.