Skip to content

Commit 86d27c5

Browse files
committed
Fix validations for options hash
1 parent a3cfdc4 commit 86d27c5

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

app/messages/validators.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,17 @@ def validate(record)
242242

243243
class OptionsValidator < ActiveModel::Validator
244244
def validate(record)
245-
unless record.options.is_a?(Hash)
246-
record.errors.add(:options, message: "'options' is not an object")
247-
return
245+
246+
# Make sure to write an empty hash if options is nil
247+
if record.options.nil?
248+
record.options = {}
248249
end
249250

250-
if record.options.empty?
251-
record.errors.add(:options, message: "'options' must include one or more valid options")
251+
# Empty option hashes are allowed, so we skip further validation
252+
record.options.blank? && return
253+
254+
unless record.options.is_a?(Hash)
255+
record.errors.add(:options, message: "'options' is not a valid object")
252256
return
253257
end
254258

0 commit comments

Comments
 (0)