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
Post a Comment