23 lines
427 B
Ruby
23 lines
427 B
Ruby
#
|
|
#
|
|
#
|
|
#
|
|
# shouldn't be used in Rails 4
|
|
#
|
|
#
|
|
#
|
|
#
|
|
# This needs to be outside the module to work.
|
|
class JsonValidator < ActiveModel::EachValidator
|
|
# implement the method called during validation
|
|
def is_json?(value)
|
|
begin
|
|
!!JSON.parse(value)
|
|
rescue
|
|
false
|
|
end
|
|
end
|
|
def validate_each(record, attribute, value)
|
|
record.errors[attribute] << 'must be JSON' unless value.nil? || is_json?(value)
|
|
end
|
|
end |