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, validate_required: !answer.aborted)) ⇒ 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_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_not_all_checked(question, validation, value) ⇒ Object
- #validate_regexp(question, validation, value) ⇒ Object
- #validate_required? ⇒ Boolean
-
#validate_single_select_value(question, value) ⇒ Object
rubocop:enable CyclomaticComplexity, Metrics/MethodLength.
- #validate_too_many_checked(question, validation, value) ⇒ Object
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
#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
201 202 203 |
# File 'lib/quby/answers/services/answer_validator.rb', line 201 def depends_on_key_answered(key) answer.depends_on_lookup[key] end |
#send_date_error(question, validation) ⇒ Object
106 107 108 |
# File 'lib/quby/answers/services/answer_validator.rb', line 106 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
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 |
# File 'lib/quby/answers/services/answer_validator.rb', line 22 def validate questionnaire.questions.each do |question| next unless question next if question.hidden? value = answer.send(question.key) next if value.blank? && %i[date check_box].exclude?(question.type) # we only validate presence on the client case question.type when :select, :radio, :scale validate_single_select_value(question, value) end 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 :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 :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
187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/quby/answers/services/answer_validator.rb', line 187 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_date(question, validation, value) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/quby/answers/services/answer_validator.rb', line 91 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
84 85 86 87 88 89 |
# File 'lib/quby/answers/services/answer_validator.rb', line 84 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
77 78 79 80 81 82 |
# File 'lib/quby/answers/services/answer_validator.rb', line 77 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
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/quby/answers/services/answer_validator.rb', line 136 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
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/quby/answers/services/answer_validator.rb', line 175 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
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/quby/answers/services/answer_validator.rb', line 123 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?(&:blank?)) 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_not_all_checked(question, validation, value) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/quby/answers/services/answer_validator.rb', line 162 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
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/quby/answers/services/answer_validator.rb', line 110 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? ⇒ Boolean
13 |
# File 'lib/quby/answers/services/answer_validator.rb', line 13 def validate_required?; @validate_required; end |
#validate_single_select_value(question, value) ⇒ Object
rubocop:enable CyclomaticComplexity, Metrics/MethodLength
71 72 73 74 75 |
# File 'lib/quby/answers/services/answer_validator.rb', line 71 def validate_single_select_value(question, value) return if value.blank? return if question..any? { |option| option.key.to_s == value.to_s } answer.send(:add_error, question, :valid_option, "Invalid option") end |
#validate_too_many_checked(question, validation, value) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/quby/answers/services/answer_validator.rb', line 150 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 |