Skip to content

Error related value used in both formatted and raw ways #2

@lulalala

Description

@lulalala

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?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions