class JsonValidator

Public Class Methods

new(options) click to toggle source
Calls superclass method
# File lib/json_validator.rb, line 6
def initialize(options)
  options.reverse_merge!(message: :invalid_json)
  super
end

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# 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

Private Instance Methods

json?(str) click to toggle source
# File lib/json_validator.rb, line 20
def json?(str)
  return false unless str.is_a?(String)
  JSON.parse(str).all?
rescue JSON::ParserError
  false
end