Module: Goliath::Rack::Validation::Required

Included in:
Param
Defined in:
lib/goliath/rack/validation/required.rb

Constant Summary collapse

NON_WHITESPACE_REGEXP =
%r![^[:space:]]!

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
# File 'lib/goliath/rack/validation/required.rb', line 13

def call(env)
  unless @optional
    return validation_error(400, "#{@type} #{@message}") unless key_valid?(env['params'])
  end
  super if defined?(super)
end

#key_valid?(params) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/goliath/rack/validation/required.rb', line 20

def key_valid?(params)
  val = fetch_key(params)

  case val
  when nil
    return false

  when String
    # if val is a string it must not be empty
    return false if val !~ NON_WHITESPACE_REGEXP

  when Array
    unless val.compact.empty?
      val.each do |k|
        return true unless k.is_a?(String)
        return true unless k !~ NON_WHITESPACE_REGEXP
      end
    end

    return false
  end

  true
end

#required_setup!(opts = {}) ⇒ Object



7
8
9
10
11
# File 'lib/goliath/rack/validation/required.rb', line 7

def required_setup!(opts={})
  if @key.is_a?(String) && @key.include?('.')
    @key = @key.split('.')
  end
end