[Proposal] Numerical formatting in validation error messages

Hello everyone,

When using validators such as numericality, length, and comparison, error messages often contain raw numerical values. However, these numbers aren’t presented in a human-readable format. e.g.:

validates :attr, numericality: { less_than: 1_000_000_000 }
# -> "must be less than 1000000000"

To address this, it’s possible to use the message option but it seems unnecessary to add it just for formatting numbers. Instead, I thought that adding a value_format option to each validator would address this issue. e.g.:

validates :my_attribute, numericality: {
  less_than: 1_000_000_000,
  value_format: -> (value) { value.to_fs(:delimited) }
} # -> "must be less than 1,000,000,000"

To address this issue, I’ve created a PR here: Add `value_format` option to `numericality`, `length` and `comparison` validators by techkato · Pull Request #51208 · rails/rails · GitHub

Looking forward to your feedback.

1 Like

Seems good, I’d like this option.

1 Like