Module: Focuslight::Validator

Defined in:
lib/focuslight/validator.rb,
lib/focuslight/validator/rule.rb,
lib/focuslight/validator/result.rb,
lib/focuslight/validator/version.rb

Defined Under Namespace

Classes: Result, Rule

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.rule(type, *args) ⇒ Object


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/focuslight/validator.rb', line 122

def self.rule(type, *args)
  args.flatten!
  case type
  when :not_blank
    Rule.new(->(v){not v.nil? and not v.strip.empty?}, "missing or blank", :strip)
  when :choice
    Rule.new(->(v){args.include?(v)}, "invalid value")
  when :int
    Rule.new(->(v){v =~ /^-?\d+$/}, "invalid integer", :to_i)
  when :uint
    Rule.new(->(v){v =~ /^\d+$/}, "invalid integer (>= 0)", :to_i)
  when :natural
    Rule.new(->(v){v =~ /^\d+$/ && v.to_i >= 1}, "invalid integer (>= 1)", :to_i)
  when :float, :double, :real
    Rule.new(->(v){v =~ /^\-?(\d+\.?\d*|\.\d+)(e[+-]\d+)?$/}, "invalid floating point num", :to_f)
  when :int_range
    Rule.new(->(v){args.first.include?(v.to_i)}, "invalid number in range #{args.first}", :to_i)
  when :bool
    Rule.new(->(v){v =~ /^(0|1|true|false)$/i}, "invalid bool value", ->(v){!!(v =~ /^(1|true)$/i)})
  when :regexp
    Rule.new(->(v){v =~ args.first}, "invalid input for pattern #{args.first.source}")
  when :lambda
    Rule.new(*args)
  else
    raise ArgumentError, "unknown validator rule: #{type}"
  end
end

.validate(params, spec) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/focuslight/validator.rb', line 7

def self.validate(params, spec)
  result = Result.new
  spec.each do |key, specitem|
    if key.is_a?(Array)
      validate_multi_key(result, params, key, specitem)
    elsif specitem[:array]
      validate_array(result, params, key, specitem)
    else
      validate_single(result, params, key, specitem)
    end
  end
  result
end

.validate_array(result, params, key_arg, spec) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/focuslight/validator.rb', line 53

def self.validate_array(result, params, key_arg, spec)
  key = key_arg.to_sym

  values = params[key]
  if spec.has_key?(:default)
    raise ArgumentError, "array parameter cannot have :default"
  end
  if spec[:excludable] && values.nil?
    result[key] = []
    return
  end

  if spec.has_key?(:size)
    if (values.nil? || values.size == 0) && !spec[:size].include?(0)
      result.error(key, "not allowed for empty")
      return
    end
    if !spec[:size].include?(values.size)
      result.error(key, "doesn't have values specified: #{spec[:size]}")
      return
    end
  end

  unless values.is_a?(Array)
    values = [values]
  end

  rules = [spec[:rule]].flatten.compact

  error_values = []
  valid = true
  formatted_values = []

  values.each do |value|
    errors = []
    formatted = nil
    rules.each do |rule|
      if rule.check(value)
        formatted = rule.format(value)
      else
        result.error(key, rule.message)
        valid = false
      end
    end
    error_values += errors
    formatted_values.push(formatted) if formatted
  end

  if valid
    result[key] = formatted_values
  end
end

.validate_multi_key(result, params, keys, spec) ⇒ Object


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/focuslight/validator.rb', line 106

def self.validate_multi_key(result, params, keys, spec)
  values = keys.map{|key| params[key.to_sym]}
  if spec.has_key?(:default)
    raise ArgumentError, "multi key validation spec cannot have :default"
  end

  rules = [spec[:rule]].flatten.compact
  errors = []

  rules.each do |rule|
    unless rule.check(*values)
      result.error(keys.map{|s| s.to_s}.join(','), rule.message)
    end
  end
end

.validate_single(result, params, key_arg, spec) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/focuslight/validator.rb', line 21

def self.validate_single(result, params, key_arg, spec)
  key = key_arg.to_sym

  value = params[key]
  if spec.has_key?(:default) && value.nil?
    value = spec[:default]
  end
  if spec[:excludable] && value.nil?
    result[key] = nil
    return
  end

  rules = [spec[:rule]].flatten.compact

  errors = []
  valid = true
  formatted = value

  rules.each do |rule|
    if rule.check(value)
      formatted = rule.format(value)
    else
      result.error(key, rule.message)
      valid = false
    end
  end

  if valid
    result[key] = formatted
  end
end