Class: Miscellany::ParamValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/miscellany/param_validator.rb

Defined Under Namespace

Classes: ErrorStore, UnsupportedTypeError

Constant Summary collapse

UNSUPPORTED_TYPE =

Returned by coerce_single_type when no coercion rule matched. Distinct from nil, which is a legitimate coerced value.

Object.new.freeze
DEFAULT_PRECISION =

Significant digits used when coercing a non-String to BigDecimal, where BigDecimal() requires an explicit precision. Strings pass 0 instead and are parsed exactly.

Float::DIG + 1
TIME_TYPES =
[Date, DateTime, Time].freeze
CHECKS =
%i[type specified present default transform in block items pattern].freeze
NON_PREFIXED =
%i[default transform type message timezone].freeze
PREFIXES =
%i[all onem onep one none].freeze
PREFIX_ALIASES =
{ any: :onep, not: :none, one_or_less: :onem, one_or_more: :onem }.freeze
ALL_PREFIXES =
(PREFIXES + PREFIX_ALIASES.keys).freeze
VALID_FLAGS =
%i[present specified].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block, context, parameters = nil) ⇒ ParamValidator

Returns a new instance of ParamValidator.



39
40
41
42
43
44
# File 'lib/miscellany/param_validator.rb', line 39

def initialize(block, context, parameters = nil)
  @block = block
  @context = context
  @params = parameters || context.params
  @errors = ErrorStore.new
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



3
4
5
# File 'lib/miscellany/param_validator.rb', line 3

def context
  @context
end

#errorsObject

Returns the value of attribute errors.



3
4
5
# File 'lib/miscellany/param_validator.rb', line 3

def errors
  @errors
end

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/miscellany/param_validator.rb', line 4

def params
  @params
end

Class Method Details

.assert(params, context: nil, handle:, &blk) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/miscellany/param_validator.rb', line 52

def self.assert(params, context: nil, handle:, &blk)
  errors = check(params, context: context, &blk)
  if errors.present?
    handle.call(errors)
  else
    params
  end
end

.check(params, context: nil, &blk) ⇒ Object



46
47
48
49
50
# File 'lib/miscellany/param_validator.rb', line 46

def self.check(params, context: nil, &blk)
  pv = new(blk, context, params)
  pv.apply_checks
  pv.errors
end

.record_type(model, key: :id) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/miscellany/param_validator.rb', line 31

def self.record_type(model, key: :id)
  Proc.new do |param, *args|
    model.find_by!(key => param)
  rescue ActiveRecord::RecordNotFound
    raise ArgumentError
  end
end

Instance Method Details

#apply_checks(&blk) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/miscellany/param_validator.rb', line 61

def apply_checks(&blk)
  blk ||= @block
  args = trim_arguments(blk, [@params, :TODO])

  dresult = instance_exec(*args, &blk)
  dresult = "failed validation #{check}" if dresult == false
  if dresult.present? && dresult != true
    @errors.push(dresult)
  end
end

#parameter(param_keys, *args, **kwargs, &blk) ⇒ Object Also known as: p



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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/miscellany/param_validator.rb', line 72

def parameter(param_keys, *args, **kwargs, &blk)
  param_keys = Array(param_keys)
  opts = normalize_opts(*args, **kwargs, &blk)

  checks = {}
  PREFIXES.each do |pfx|
    pfx_keys = opts[pfx]&.keys&.select { |k| opts[pfx][k] } || []
    pfx_keys.each do |k|
      checks[k] = pfx # TODO: Support filters connected to multiple prefixes
    end
    # TODO: warn if pfx != :all && param_keys.length == 1
  end
  NON_PREFIXED.each do |k|
    checks[k] = nil
  end

  all_results = {}
  param_keys.each do |pk|
    check_results = all_results[pk] = {}
    run_check = ->(check, &blk) { exec_check(check_results, check, checks, options: opts, &blk) }

    exec_check(check_results, :type) { coerce_type(params, pk, opts) } || next

    run_check[:specified] { params.key?(pk) || 'must be specified' } || next
    run_check[:present] { params[pk].present? || 'must be present' } || next

    # Set Default
    if params[pk].nil? && !opts[:default].nil?
      params[pk] ||= opts[:default].respond_to?(:call) ? opts[:default].call : opts[:default]
      next # We can assume that the default value is allowed
    end

    # Apply Transform
    params[pk] = opts[:transform].to_proc.call(params[pk]) if params.include?(pk) && opts[:transform]

    next if params[pk].nil?

    run_check[:pattern] do |pattern|
      # `next`, not `return` - a `return` here would exit `parameter` entirely,
      # skipping the remaining checks and the final `@errors.merge!`.
      next true if params[pk].to_s.match?(pattern)

      "must match pattern: #{pattern.inspect}"
    end

    run_check[:in] do |one_of|
      next true if one_of.include?(params[pk])

      if one_of.is_a?(Range)
        "must be between: #{one_of.begin}..#{one_of.end}"
      else
        "must be one of: #{one_of.to_a.join(', ')}"
      end
    end

    # Nested check
    run_check[:block] do |blk|
      ParamValidator.check(params[pk], context: context, &blk)
    end

    # Array Items check
    run_check[:items] do |blk|
      if params[pk].is_a?(Array)
        error_items = 0
        astore = ErrorStore.new

        params[pk].each_with_index do |v, i|
          errs = nil
          if blk.is_a?(Hash)
            pv = ParamValidator.new(nil, self.context, params[pk])
            pv.parameter(i, **blk)
            errs = pv.errors.store_for(i)
          else
            errs = ParamValidator.check(params[pk][i], context: context, &blk)
          end

          if errs.present?
            error_items += 1
            if error_items > 5
              check_results[:items]
              astore.push("Too Many Errors")
              break
            else
              astore.push_to(i, errs)
            end
          end
        end

        astore
      else
        raise "items: validator can only be used with Arrays"
      end
    end
  end

  final_errors = ErrorStore.new
  checks.each do |check, check_prefix|
    if check_prefix == :all || check_prefix == nil
      all_results.each do |field, err_map|
        errs = err_map[check]
        next unless errs.present?

        final_errors.push_to(field, errs)
      end
    elsif check_prefix == :none
      all_results.each do |field, err_map|
        errs = err_map[check]
        final_errors.push_to(field, "must NOT be #{check}") unless errs.present?
      end
    else
      counts = check_pass_count(check, all_results)
      field_key = param_keys.join(', ')
      string_prefixes = {
        onep: 'One or more of',
        onem: 'At most one of',
        one: 'Exactly one of',
      }

      if (counts[:passed] != 1 && check_prefix == :one) ||
        (counts[:passed] > 1 && check_prefix == :onem) ||
        (counts[:passed] < 1 && check_prefix == :onep)

        final_errors.push("#{string_prefixes[check_prefix]} #{field_key} must be #{check}")
      end
    end
  end

  @errors.merge!(final_errors)

  nil
end