Sha256: 7c9367a6ebe9a1cb73f91fd1e2e5ae3ec577a47ac65457bcca57b239e95e9ce1

Contents?: true

Size: 433 Bytes

Versions: 29

Compression:

Stored size: 433 Bytes

Contents

class JsonValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    record.errors[attribute] << (options[:message] || I18n.t("slash_admin.view.json_not_valid")) unless valid_json?(value)
  end

  def valid_json?(string)
    # handle parsed already string
    if string.is_a? Hash
      return true
    end
    begin
      !!JSON.parse(string)
    rescue JSON::ParserError
      false
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
slash_admin-1.5.11 config/initializers/validators.rb
slash_admin-1.5.10 config/initializers/validators.rb
slash_admin-1.5.9 config/initializers/validators.rb
slash_admin-1.5.8 config/initializers/validators.rb
slash_admin-1.5.7 config/initializers/validators.rb
slash_admin-1.5.6 config/initializers/validators.rb
slash_admin-1.5.5 config/initializers/validators.rb
slash_admin-1.5.4 config/initializers/validators.rb
slash_admin-1.5.3 config/initializers/validators.rb
slash_admin-1.5.2 config/initializers/validators.rb
slash_admin-1.5.1 config/initializers/validators.rb
slash_admin-1.5 config/initializers/validators.rb
slash_admin-1.4.1 config/initializers/validators.rb
slash_admin-1.4 config/initializers/validators.rb
slash_admin-1.3.5 config/initializers/validators.rb
slash_admin-1.3.4 config/initializers/validators.rb
slash_admin-1.3.3 config/initializers/validators.rb
slash_admin-1.3.2 config/initializers/validators.rb
slash_admin-1.3.1 config/initializers/validators.rb
slash_admin-1.3.0 config/initializers/validators.rb