Class: JsonValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/json_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ JsonValidator

Returns a new instance of JsonValidator.



6
7
8
9
# File 'lib/json_validator.rb', line 6

def initialize(options)
  options.reverse_merge!(message: :invalid_json)
  super
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



11
12
13
14
15
16
# File 'lib/json_validator.rb', line 11

def validate_each(record, attribute, value)
  return if json?(value)
  message = options.fetch(:message)

  record.errors.add(attribute, message)
end