Class: Quby::Answers::Services::AnswerValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/quby/answers/services/answer_validator.rb

Defined Under Namespace

Classes: InvalidValue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(questionnaire, answer, validate_required: !answer.aborted)) ⇒ AnswerValidator

Returns a new instance of AnswerValidator.



15
16
17
18
19
# File 'lib/quby/answers/services/answer_validator.rb', line 15

def initialize(questionnaire, answer, validate_required: !answer.aborted)
  @questionnaire = questionnaire
  @answer        = answer
  @validate_required = validate_required
end

Instance Attribute Details

#answerObject (readonly)

Returns the value of attribute answer.



12
13
14
# File 'lib/quby/answers/services/answer_validator.rb', line 12

def answer
  @answer
end

#questionnaireObject (readonly)

Returns the value of attribute questionnaire.



11
12
13
# File 'lib/quby/answers/services/answer_validator.rb', line 11

def questionnaire
  @questionnaire
end

Instance Method Details

#depends_on_key_answered(key) ⇒ Object



239
240
241
# File 'lib/quby/answers/services/answer_validator.rb', line 239

def depends_on_key_answered(key)
  answer.depends_on_lookup[key]
end

#send_date_error(question, validation) ⇒ Object



126
127
128
# File 'lib/quby/answers/services/answer_validator.rb', line 126

def send_date_error(question, validation)
  answer.send(:add_error, question, :valid_date, validation[:message] || "Does not match expected pattern.")
end

#validateObject

rubocop:disable CyclomaticComplexity, Metrics/MethodLength



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
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
# File 'lib/quby/answers/services/answer_validator.rb', line 22

def validate
  questionnaire.questions.each do |question|
    next unless question
    next if question.hidden?

    if question.depends_on.present?
      next unless question.depends_on.any? { |key| depends_on_key_answered(key) }
    end

    value = answer.send(question.key)
    next if answer.skip_validation?(value, question)

    question.validations.each do |validation|
      begin
        case validation[:type]
        when :valid_integer
          validate_integer(question, validation, value)
        when :valid_float
          validate_float(question, validation, value)
        when :valid_date
          value = question.components.each_with_object({}) do |component, hash|
            key = question.send("#{component}_key")
            hash[component] = answer.send(key)
          end
          validate_date(question, validation, value)
        when :regexp
          validate_regexp(question, validation, value)
        when :requires_answer
          if question.type == :date
            value = question.answer_keys.each_with_object({}) do |key, hash|
              hash[key] = answer.send(key)
            end
          end
          validate_required(question, validation, value)
        when :minimum
          validate_minimum(question, validation, value)
        when :maximum
          validate_maximum(question, validation, value)
        when :too_many_checked
          validate_too_many_checked(question, validation, value)
        when :not_all_checked
          validate_not_all_checked(question, validation, value)
        when :maximum_checked_allowed
          validate_maximum_checked_allowed(question, validation, value)
        when :minimum_checked_required
          validate_minimum_checked_required(question, validation, value)
        when :answer_group_minimum
          validate_answer_group_minimum(question, validation, value)
        when :answer_group_maximum
          validate_answer_group_maximum(question, validation, value)
        end
      rescue InvalidValue
        answer.send(:add_error, question, validation[:type], validation[:message] || "Invalid value.")
      end
    end
  end
end

#validate_answer_group_maximum(question, validation, value) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/quby/answers/services/answer_validator.rb', line 225

def validate_answer_group_maximum(question, validation, value)
  # We have decided not to allow bypassing this validation when
  # aborted, since it's possible to make a decision about which fields
  # to keep and which ones to blank. If you want to do anything at all
  # with this completion, you'll need to decide that at some point
  # anyway, so we think it's best to do that as early as possible.

  answered = answer.send(:calc_answered, answer.question_groups[validation[:group]])
  if answered > validation[:value]
    answer.send(:add_error, question, :answer_group_maximum,
                validation[:message] || "Needs at most #{validation[:value]} question(s) answered.")
  end
end

#validate_answer_group_minimum(question, validation, value) ⇒ Object



215
216
217
218
219
220
221
222
223
# File 'lib/quby/answers/services/answer_validator.rb', line 215

def validate_answer_group_minimum(question, validation, value)
  return unless validate_required?

  answered = answer.send(:calc_answered, answer.question_groups[validation[:group]])
  if answered < validation[:value]
    answer.send(:add_error, question, :answer_group_minimum,
                validation[:message] || "Needs at least #{validation[:value]} question(s) answered.")
  end
end

#validate_date(question, validation, value) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/quby/answers/services/answer_validator.rb', line 111

def validate_date(question, validation, value)
  # Skip this validation if all date parts are empty
  return if value.values.all?(&:blank?)

  # Check if there are required date parts missing
  required_values = value.fetch_values(*question.required_components)
  send_date_error(question, validation) if required_values.any?(&:blank?)

  begin
    convert_answer_value(question, value)
  rescue InvalidValue
    send_date_error(question, validation)
  end
end

#validate_float(question, validation, value) ⇒ Object



104
105
106
107
108
109
# File 'lib/quby/answers/services/answer_validator.rb', line 104

def validate_float(question, validation, value)
  return if value.blank?
  Float(value)
rescue ArgumentError
  answer.send(:add_error, question, :valid_float, validation[:message] || "Invalid float")
end

#validate_integer(question, validation, value) ⇒ Object



97
98
99
100
101
102
# File 'lib/quby/answers/services/answer_validator.rb', line 97

def validate_integer(question, validation, value)
  return if value.blank?
  Integer(value)
rescue ArgumentError
  answer.send(:add_error, question, :valid_integer, validation[:message] || "Invalid integer")
end

#validate_maximum(question, validation, value) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/quby/answers/services/answer_validator.rb', line 156

def validate_maximum(question, validation, value)
  # We have decided not to allow bypassing this validation when
  # aborted, since whatever is using this data further on is likely not
  # built to take into account values outside the intended range. (e.g.
  # BMI calculation)

  return if value.blank? || (question.type == :date && value.values.all?(&:blank?))

  converted_answer_value = convert_answer_value(question, value)
  if converted_answer_value > validation[:value]
    answer.send(:add_error, question, validation[:type], validation[:message] || "Exceeds maximum")
  end
end

#validate_maximum_checked_allowed(question, validation, value) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/quby/answers/services/answer_validator.rb', line 195

def validate_maximum_checked_allowed(question, validation, value)
  # We have decided not to allow bypassing this validation when
  # aborted, since it's possible to make a decision about which fields
  # to keep and which ones to blank. If you want to do anything at all
  # with this completion, you'll need to decide that at some point
  # anyway, so we think it's best to do that as early as possible.

  if value.values.reduce(:+) > question.maximum_checked_allowed.to_i
    answer.send(:add_error, question, :maximum_checked_allowed, validation[:message] || "Too many options checked.")
  end
end

#validate_minimum(question, validation, value) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/quby/answers/services/answer_validator.rb', line 143

def validate_minimum(question, validation, value)
  # We have decided not to allow bypassing this validation when
  # aborted, since whatever is using this data further on is likely not
  # built to take into account values outside the intended range. (e.g.
  # BMI calculation)

  return if value.blank? || (question.type == :date && value.values.all?(&:empty?))
  converted_answer_value = convert_answer_value(question, value)
  if converted_answer_value < validation[:value]
    answer.send(:add_error, question, validation[:type], validation[:message] || "Smaller than minimum")
  end
end

#validate_minimum_checked_required(question, validation, value) ⇒ Object



207
208
209
210
211
212
213
# File 'lib/quby/answers/services/answer_validator.rb', line 207

def validate_minimum_checked_required(question, validation, value)
  return unless validate_required?

  if value.values.reduce(:+) < question.minimum_checked_required.to_i
    answer.send(:add_error, question, :minimum_checked_required, validation[:message] || "Not enough options checked.")
  end
end

#validate_not_all_checked(question, validation, value) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/quby/answers/services/answer_validator.rb', line 182

def validate_not_all_checked(question, validation, value)
  # We have decided not to allow bypassing this validation when
  # aborted, since it's possible to make a decision about which fields
  # to keep and which ones to blank. If you want to do anything at all
  # with this completion, you'll need to decide that at some point
  # anyway, so we think it's best to do that as early as possible.

  if answer.send(question.check_all_option) == 1 and
      value.values.reduce(:+) < value.length - (question.uncheck_all_option ? 1 : 0)
    answer.send(:add_error, question, :not_all_checked, validation[:message] || "Invalid combination of options.")
  end
end

#validate_regexp(question, validation, value) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/quby/answers/services/answer_validator.rb', line 130

def validate_regexp(question, validation, value)
  # We have decided not to allow bypassing this validation when
  # aborted, since whatever is using this data further on is likely not
  # built to take into account values that do not conform to the given
  # format.

  return if value.blank?
  match = validation[:matcher].match(value)
  unless match && match[0] == value
    answer.send(:add_error, question, validation[:type], validation[:message] || "Does not match expected pattern.")
  end
end

#validate_required(question, validation, value) ⇒ Object

rubocop:enable CyclomaticComplexity, Metrics/MethodLength



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/quby/answers/services/answer_validator.rb', line 81

def validate_required(question, validation, value)
  return unless validate_required?
  valid = case question.type
          when :date
            required_keys = question.required_components.map do |key|
              question.send(key.to_s + "_key")
            end
            value.values_at(*required_keys).all?(&:present?)
          when :check_box
            value.values.reduce(:+) > 0
          else
            value.present?
          end
  answer.send(:add_error, question, validation[:type], validation[:message] || "Must be answered.") unless valid
end

#validate_required?Boolean

Returns:

  • (Boolean)


13
# File 'lib/quby/answers/services/answer_validator.rb', line 13

def validate_required?; @validate_required; end

#validate_too_many_checked(question, validation, value) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/quby/answers/services/answer_validator.rb', line 170

def validate_too_many_checked(question, validation, value)
  # We have decided not to allow bypassing this validation when
  # aborted, since it's possible to make a decision about which fields
  # to keep and which ones to blank. If you want to do anything at all
  # with this completion, you'll need to decide that at some point
  # anyway, so we think it's best to do that as early as possible.

  if answer.send(question.uncheck_all_option) == 1 and value.values.reduce(:+) > 1
    answer.send(:add_error, question, :too_many_checked, validation[:message] || "Invalid combination of options.")
  end
end