class ValidJsonValidator
Public Instance Methods
validate_each(record, attribute, value)
click to toggle source
# File lib/sift/validators/valid_json_validator.rb, line 2 def validate_each(record, attribute, value) record.errors.add attribute, "must be a valid JSON" unless valid_json?(value) end
Private Instance Methods
valid_json?(value)
click to toggle source
# File lib/sift/validators/valid_json_validator.rb, line 8 def valid_json?(value) value = value.strip if value.is_a?(String) JSON.parse(value) rescue JSON::ParserError false end