Class: AdvancedAR::ParamValidator

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

Constant Summary collapse

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].freeze
PREFIXES =
%i[all onem onep one none].freeze
PREFIX_ALIASES =
{ any: :onep, not: :none }.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, options = nil) ⇒ ParamValidator

Returns a new instance of ParamValidator.



16
17
18
19
20
21
22
23
24
# File 'lib/advanced_ar/param_validator.rb', line 16

def initialize(block, context, parameters = nil, options = nil)
  @block = block
  @context = context
  @params = parameters || context.params
  @subkeys = []
  @options = options || {}
  @errors = {}
  @explicit_parameters = []
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

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



32
33
34
35
36
37
38
39
# File 'lib/advanced_ar/param_validator.rb', line 32

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



26
27
28
29
30
# File 'lib/advanced_ar/param_validator.rb', line 26

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

Instance Method Details

#apply_checks(&blk) ⇒ Object



41
42
43
44
45
# File 'lib/advanced_ar/param_validator.rb', line 41

def apply_checks(&blk)
  blk ||= @block
  args = trim_arguments(blk, [params, @subkeys[-1]])
  instance_exec(*args, &blk)
end

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



47
48
49
50
51
52
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
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
# File 'lib/advanced_ar/param_validator.rb', line 47

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|
      return 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|
      iterate_array = false # TODO
      sub_parameter(pk) do
        if params.is_a?(Array) && iterate_array
          params.each_with_index do |v, i|
            sub_parameter(i) { apply_checks(&blk) }
          end
        else
          apply_checks(&blk)
        end
      end
    end

    # Nested check
    run_check[:items] do |blk|
      sub_parameter(pk) do
        if params.is_a?(Array)
          params.each_with_index do |v, i|
            sub_parameter(i) { apply_checks(&blk) }
          end
        else
          raise "items: validator can only be used with Arrays"
        end
      end
    end
  end

  final_errors = {}
  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[field] = merge_error_hashes(final_errors[field], errs)
      end
    elsif check_prefix == :none
      all_results.each do |field, err_map|
        errs = err_map[check]
        final_errors[field] = merge_error_hashes(final_errors[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 = merge_error_hashes(final_errors, "#{string_prefixes[check_prefix]} #{field_key} #{check}")
      end
    end
  end

  @errors = merge_error_hashes(@errors, final_errors)
  final_errors
end