Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Skip some validations when param is nil #6

Merged
merged 1 commit into from about 2 years ago

2 participants

Mattt Thompson

I was running into some cases where I'd have an optional parameter that also had a :in validation:

param :foo, String, in: %w[a b c]

If I left off the parameter (because it was optional), the validation would fail. I had changed it to this line to make it work:

param :foo, String, in: %w[a, b, c] + [nil]

But in the end I think it makes more sense for the validation to only run if the param is non-nil.

Mattt Thompson
Owner
mattt commented

This is a nice addition. Thanks!

Mattt Thompson mattt merged commit 54d92d3 into from
Mattt Thompson mattt closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jun 14, 2012
rlipman Skip some validations when param is nil 85f7a32
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 3 additions and 3 deletions. Show diff stats Hide diff stats

  1. +3 3 lib/sinatra/param.rb
6 lib/sinatra/param.rb
@@ -51,16 +51,16 @@ def validate!(param, options)
51 51 when :is
52 52 raise InvalidParameterError unless value === param
53 53 when :in, :within, :range
54   - raise InvalidParameterError unless case value
  54 + raise InvalidParameterError unless param.nil? || case value
55 55 when Range
56 56 value.include?(param)
57 57 else
58 58 Array(value).include?(param)
59 59 end
60 60 when :min
61   - raise InvalidParameterError unless value <= param
  61 + raise InvalidParameterError unless param.nil? || value <= param
62 62 when :max
63   - raise InvalidParameterError unless value >= param
  63 + raise InvalidParameterError unless param.nil? || value >= param
64 64 end
65 65 end
66 66 end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.