Class: Quby::Answers::Services::AnswerValidator
- Inherits:
-
Object
- Object
- Quby::Answers::Services::AnswerValidator
- Defined in:
- lib/quby/answers/services/answer_validator.rb
Defined Under Namespace
Classes: InvalidValue
Instance Attribute Summary collapse
-
#answer ⇒ Object
readonly
Returns the value of attribute answer.
-
#questionnaire ⇒ Object
readonly
Returns the value of attribute questionnaire.
Instance Method Summary collapse
- #depends_on_key_answered(key) ⇒ Object
-
#initialize(questionnaire, answer) ⇒ AnswerValidator
constructor
A new instance of AnswerValidator.
- #send_date_error(question, validation) ⇒ Object
-
#validate ⇒ Object
rubocop:disable CyclomaticComplexity, Metrics/MethodLength.
- #validate_answer_group_maximum(question, validation, value) ⇒ Object
- #validate_answer_group_minimum(question, validation, value) ⇒ Object
- #validate_date(question, validation, value) ⇒ Object
- #validate_float(question, validation, value) ⇒ Object
- #validate_integer(question, validation, value) ⇒ Object
- #validate_maximum(question, validation, value) ⇒ Object
- #validate_maximum_checked_allowed(question, validation, value) ⇒ Object
- #validate_minimum(question, validation, value) ⇒ Object
- #validate_minimum_checked_required(question, validation, value) ⇒ Object
- #validate_not_all_checked(question, validation, value) ⇒ Object
- #validate_regexp(question, validation, value) ⇒ Object
-
#validate_required(question, validation, value) ⇒ Object
rubocop:enable CyclomaticComplexity, Metrics/MethodLength.
- #validate_too_many_checked(question, validation, value) ⇒ Object
Constructor Details
#initialize(questionnaire, answer) ⇒ AnswerValidator
Returns a new instance of AnswerValidator.
14 15 16 17 |
# File 'lib/quby/answers/services/answer_validator.rb', line 14 def initialize(questionnaire, answer) @questionnaire = questionnaire @answer = answer end |
Instance Attribute Details
#answer ⇒ Object (readonly)
Returns the value of attribute answer.
12 13 14 |
# File 'lib/quby/answers/services/answer_validator.rb', line 12 def answer @answer end |
#questionnaire ⇒ Object (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
237 238 239 |
# File 'lib/quby/answers/services/answer_validator.rb', line 237 def depends_on_key_answered(key) answer.depends_on_lookup[key] end |
#send_date_error(question, validation) ⇒ Object
124 125 126 |
# File 'lib/quby/answers/services/answer_validator.rb', line 124 def send_date_error(question, validation) answer.send(:add_error, question, :valid_date, validation[:message] || "Does not match expected pattern.") end |
#validate ⇒ Object
rubocop:disable CyclomaticComplexity, Metrics/MethodLength
20 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 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 |
# File 'lib/quby/answers/services/answer_validator.rb', line 20 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
223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/quby/answers/services/answer_validator.rb', line 223 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
213 214 215 216 217 218 219 220 221 |
# File 'lib/quby/answers/services/answer_validator.rb', line 213 def validate_answer_group_minimum(question, validation, value) return if @answer.aborted 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
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/quby/answers/services/answer_validator.rb', line 109 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
102 103 104 105 106 107 |
# File 'lib/quby/answers/services/answer_validator.rb', line 102 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
95 96 97 98 99 100 |
# File 'lib/quby/answers/services/answer_validator.rb', line 95 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
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/quby/answers/services/answer_validator.rb', line 154 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
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/quby/answers/services/answer_validator.rb', line 193 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
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/quby/answers/services/answer_validator.rb', line 141 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
205 206 207 208 209 210 211 |
# File 'lib/quby/answers/services/answer_validator.rb', line 205 def validate_minimum_checked_required(question, validation, value) return if @answer.aborted 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
180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/quby/answers/services/answer_validator.rb', line 180 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
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/quby/answers/services/answer_validator.rb', line 128 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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/quby/answers/services/answer_validator.rb', line 79 def validate_required(question, validation, value) return if @answer.aborted 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_too_many_checked(question, validation, value) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/quby/answers/services/answer_validator.rb', line 168 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 |