Created
May 11, 2010 15:55
-
-
Save christophercotton/397462 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example class by christophercotton to show how to validate JSON in | |
# ActiveRecord | |
class Stuff < ActiveRecord::Base | |
validates_presence_of :my_field | |
validate :valid_json | |
# make sure the JSON is valid | |
def valid_json | |
begin | |
ActiveSupport::JSON.decode(my_field) | |
rescue | |
@errors.add(:my_field, "must contain valid JSON") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment