Skip to content

Document open ranges as a lighter alternative to a numericality validator#2831

Merged
ericproulx merged 1 commit into
ruby-grape:masterfrom
dblock:support-open-ranges-values
Jul 27, 2026
Merged

Document open ranges as a lighter alternative to a numericality validator#2831
ericproulx merged 1 commit into
ruby-grape:masterfrom
dblock:support-open-ranges-values

Conversation

@dblock

@dblock dblock commented Jul 27, 2026

Copy link
Copy Markdown
Member

Summary

#2822 proposed a new numericality validator for bounding numeric parameters (greater_than, less_than, equal_to, etc.). As discussed on that PR, Grape's existing :values option already supports Range, including open (beginless/endless) ranges, since it validates with Range#include?. This covers the same common use cases without adding a new validator:

params do
  requires :quantity, type: Integer, values: 1..                # must be a positive integer
  requires :discount, type: Float, values: 0.0..100.0           # must be between 0 and 100
  requires :rating,   type: Integer, values: 5..5                # must be exactly 5
  requires :numbers,  type: [Integer], values: 1..                # every element must be positive
end

This PR does not change any validator code (open ranges already work end-to-end). It:

  • Adds a /beginless range test alongside the existing /endless one in values_validator_spec.rb.
  • Ports the bound-checking scenarios from Add a numericality validator #2822's numericality_validator_spec.rb (greater_than, greater_than_or_equal_to/less_than_or_equal_to range, equal_to, array element-wise checking, integer-only via type:) as values: + range equivalents.
  • Documents these patterns in the README's :values section.

Test plan

  • bundle exec rspec spec/grape/validations/validators/values_validator_spec.rb (73 examples, 0 failures)
  • bundle exec rake (full suite + RuboCop) passes locally

@dblock
dblock force-pushed the support-open-ranges-values branch from 27b1b77 to 337239a Compare July 27, 2026 00:23
@github-actions

github-actions Bot commented Jul 27, 2026

Copy link
Copy Markdown

Danger Report

No issues found.

View run

@dblock
dblock force-pushed the support-open-ranges-values branch 4 times, most recently from 2f3191e to 929934b Compare July 27, 2026 00:34
…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
dblock force-pushed the support-open-ranges-values branch from 929934b to b869c9a Compare July 27, 2026 00:35
@dblock
dblock requested a review from ericproulx July 27, 2026 00:36
@dblock dblock mentioned this pull request Jul 27, 2026
4 tasks
@ericproulx
ericproulx merged commit 28d635d into ruby-grape:master Jul 27, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants