简体   繁体   中英

How to validate a json type attribute

Rails 4.0.4 w/ Postgres

Ruby 2.1.1

I am using a model with a JSON type attribute.

here the migration

def change
    create_table :my_models do |t|
      t.json :my_json_attribute, :null => false

      t.timestamps
    end
end

I want to validate this attribute in my form before saved or updated in the database.

Today I am getting a nasty JSON parser error (JSON::ParserError) instead of a friendly message on my form .. I am purposely giving an incorrect JSON input in my form to check if validation works and if I get a friendly message asking to verify the JSON string ... but I am not even sure how to check whether attribute_in_json_format was called

In my model class, I have something like this:

class MyModel < ActiveRecord::Base
  validate_presence_of :my_json_attribute

  validate :attribute_in_json_format

protected
  def attribute_in_json_format
    errors[:base] << "not in JSON format" unless my_json_attribute.is_json?
  end
end

Created an initializer string.rb:

require 'json'

class String
  def is_json?
    begin
      !!JSON.parse(self)
    rescue
      false
    end
  end
end

I am not getting any success ... I am still going to the MultiJson::ParseError instead of going through the validate.

Any suggestion?

I took my inspiration from this stackoverflow thread

Maybe using store_accessor would help.

class MyModel < ActiveRecord::Base
  store_accessor :my_jason_attribute
  validate_presence_of :my_json_attribute
...

Here is the documentation . Check the NOTE:

NOTE - If you are using PostgreSQL specific columns like hstore or json there is no need for the serialization provided by store. Simply use store_accessor instead to generate the accessor methods. Be aware that these columns use a string keyed hash and do not allow access using a symbol.

Hope this helps.

I am a bit late, but I think that your approach is in the right direction. However, you don't need to monkey-patch the String class. You can do this:

validate :attribute_in_json_format

private

def attribute_in_json_format
  JSON.parse(my_json_attribute.to_s)
rescue JSON::ParserError
  errors[:base] << "not in JSON format"
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM