ruby on rails - How to validate the input value of text_area is JSON format? -


i using rails 4.2 ruby 2.1.5

here description model:

class description < activerecord::base   validates :value, presence: true   belongs_to :api end 

what can validate input of value json format?

add custom validate method parse value. rescue exception , add error if fails parsing.

validate :validate_json_format  private  def validate_json_format   return if value.blank?    json.parse(value)  rescue json::parsererror   errors.add(:value, 'invalid json format') end 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -