Add a numericality validator#2822
Closed
ericproulx wants to merge 1 commit into
Closed
Conversation
ericproulx
force-pushed
the
add_numericality_validator
branch
from
July 26, 2026 14:52
7b119a1 to
8e72ff1
Compare
Danger ReportNo issues found. |
Grape has no built-in way to bound or shape-check a numeric parameter beyond `values:` (a closed inclusive range or list). Users end up reaching for a `values:` lambda or a full `contract_scope` schema for something as simple as "must be a positive integer" or "0..100 inclusive with an exclusive upper bound", both of which are more ceremony than the check deserves and produce weak error messages. Add `NumericalityValidator`, registered as `numericality`, following ActiveModel::Validations::NumericalityValidator's option naming for familiarity: `greater_than`, `greater_than_or_equal_to`, `less_than`, `less_than_or_equal_to`, `equal_to`, `other_than`, `only_integer`, `odd`, `even`. Any combination can be supplied and each has its own i18n message key (mirroring how `length` has separate `length_min`/ `length_max`/`length_is` keys). Design notes: - Non-Numeric values are skipped rather than erroring — numeric-ness is `type:`/coercion's job; this validator only owns bounds/parity, so it composes with `coerce` instead of duplicating it. - Applied to a typed array (`type: [Integer]`), every element is checked individually (`Array.wrap`), matching how `values` already treats arrays — `length` is the one that owns the array's own size. - `only_integer` accepts whole-numbered `Float`/`BigDecimal` too (`val == val.to_i`), not just literal `Integer`. - Obviously contradictory option combinations (`greater_than` + `greater_than_or_equal_to`, `less_than` + `less_than_or_equal_to`, `odd` + `even`, `equal_to` + any other comparison, or a reversed lower/upper bound) raise `ArgumentError` at definition time, same as `length_validator`'s existing `min > max` guard. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
ericproulx
force-pushed
the
add_numericality_validator
branch
from
July 26, 2026 15:07
8e72ff1 to
969e0f2
Compare
Member
|
Supporting open ranges would be cleaner because we already support closed ranges, no? params do
requires :quantity, type: Integer, values: 1.. # numericality: { greater_than: 0 }
requires :discount, type: Float, values: 0..100 # numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100 }
requires :rating, type: Integer, values: 5 # numericality: { equal_to: 5 }
requires :page, type: Integer, values: 1.. # this one has to be integer because of type ... numericality: { greater_than: 0, only_integer: true }
end |
Contributor
Author
|
Yeah, makes sense. I'll drop it. |
Member
Copilot is working ... |
2 tasks
dblock
added a commit
to dblock/grape
that referenced
this pull request
Jul 27, 2026
…idator Instead of adding a new numericality validator (ruby-grape#2822), extend test coverage and documentation for the existing :values option, which already supports open (beginless/endless) Ranges via Range#include?. This covers the same common cases (positive numbers, percentages, exact values, and array element-wise checks) without a new validator. Ref: ruby-grape#2822 (comment) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dblock
added a commit
to dblock/grape
that referenced
this pull request
Jul 27, 2026
…idator Instead of adding a new numericality validator (ruby-grape#2822), extend test coverage and documentation for the existing :values option, which already supports open (beginless/endless) Ranges via Range#include?. This covers the same common cases (positive numbers, percentages, exact values, and array element-wise checks) without a new validator. Ref: ruby-grape#2822 (comment) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dblock
added a commit
to dblock/grape
that referenced
this pull request
Jul 27, 2026
…idator Instead of adding a new numericality validator (ruby-grape#2822), extend test coverage and documentation for the existing :values option, which already supports open (beginless/endless) Ranges via Range#include?. This covers the same common cases (positive numbers, percentages, exact values, and array element-wise checks) without a new validator. Ref: ruby-grape#2822 (comment) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dblock
added a commit
to dblock/grape
that referenced
this pull request
Jul 27, 2026
…idator Instead of adding a new numericality validator (ruby-grape#2822), extend test coverage and documentation for the existing :values option, which already supports open (beginless/endless) Ranges via Range#include?. This covers the same common cases (positive numbers, percentages, exact values, and array element-wise checks) without a new validator. Ref: ruby-grape#2822 (comment) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dblock
added a commit
to dblock/grape
that referenced
this pull request
Jul 27, 2026
…idator Instead of adding a new numericality validator (ruby-grape#2822), extend test coverage and documentation for the existing :values option, which already supports open (beginless/endless) Ranges via Range#include?. This covers the same common cases (positive numbers, percentages, exact values, and array element-wise checks) without a new validator. Ref: ruby-grape#2822 (comment) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dblock
added a commit
to dblock/grape
that referenced
this pull request
Jul 27, 2026
…idator Instead of adding a new numericality validator (ruby-grape#2822), extend test coverage and documentation for the existing :values option, which already supports open (beginless/endless) Ranges via Range#include?. This covers the same common cases (positive numbers, percentages, exact values, and array element-wise checks) without a new validator. Ref: ruby-grape#2822 (comment) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Member
ericproulx
pushed a commit
that referenced
this pull request
Jul 27, 2026
…idator (#2831) Instead of adding a new numericality validator (#2822), extend test coverage and documentation for the existing :values option, which already supports open (beginless/endless) Ranges via Range#include?. This covers the same common cases (positive numbers, percentages, exact values, and array element-wise checks) without a new validator. Ref: #2822 (comment) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Grape currently has no built-in validator for bounding or shape-checking a numeric parameter beyond
values:(a closed inclusive range or an explicit list/predicate). Common cases like "must be a positive integer", "0 to 100 with an exclusive upper bound", or "must be a perfect score of exactly 5" require either abusingvalues:with a lambda (weak error messages, awkward for open-ended bounds) or writing a fullcontract_scopeschema for a single field.This PR adds a
numericalityvalidator, followingActiveModel::Validations::NumericalityValidator's option names for familiarity to anyone coming from Rails:Supported options (any combination):
greater_than,greater_than_or_equal_to,less_than,less_than_or_equal_to,equal_to,other_than,only_integer,odd,even. Each has its own i18n message key (numericality_greater_than, etc.), mirroring howlengthalready has separatelength_min/length_max/length_iskeys. Per-fieldmessage:overrides work the same way as every other validator.Design notes
type:/coercion's job; this validator only owns bounds/parity, so it composes withcoercerather than duplicating its work. A param with notype:/coerce still round-trips through unchanged if it isn't already aNumeric.requires :numbers, type: [Integer], numericality: { greater_than: 0 }validates every element (viaArray.wrap), matching howvaluesalready treats arrays.lengthis the one validator that intentionally owns the array's own size, so no overlap there.only_integeraccepts whole-numberedFloat/BigDecimal, not just literalInteger—val == val.to_i— sotype: Float, numericality: { only_integer: true }accepts5.0and rejects5.5.ArgumentErrorat definition time, not at request time:greater_than+greater_than_or_equal_to,less_than+less_than_or_equal_to,odd+even,equal_to+ any other comparison option, and a reversed lower/upper bound (e.g.greater_than: 10, less_than: 5). This mirrorsLengthValidator's existingmin > maxguard.No existing validator's behavior changes; this is purely additive (new validator class, new locale keys, new README section). No
UPGRADING.mdentry needed.Test plan
spec/grape/validations/validators/numericality_validator_spec.rb(34 examples): every option's pass/fail case, the range combination, theonly_integerFloat case, odd/even, array-element-wise checking, non-Numeric skip behavior,allow_blankinterplay, custommessage:override, and everyArgumentErrorguard.🤖 Generated with Claude Code