Skip to content

Instantly share code, notes, and snippets.

@christophercotton
Created May 11, 2010 15:55
Show Gist options
  • Save christophercotton/397462 to your computer and use it in GitHub Desktop.
Save christophercotton/397462 to your computer and use it in GitHub Desktop.
# 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