Git Product home page Git Product logo

validates_serialized's People

Contributors

brycesenz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

validates_serialized's Issues

Valdiateable Objects don't delegate correctly.

I thought I'd fixed this in 0.0.4, but ValidateableObjects, ValidateableHashes, and ValidateableArrayValues don't handle delegation correctly.

For example, in another Rails app I'm validating nested (serialized) properties on an ActiveRecord object, and I get the following error:

undefined method `_callback_before_12055' for {:type=>nil, :payment=>400, :employer_name=>"Acme"}:Hash

That is because the Hash is not passing that callback onto the object it represents.

Error validating hashes, Rails 4!

I'm getting an error validating serialized hashes, but I haven't tried anything else yet so it could be happening for other types too.

The code I used is pretty much the same as in the README here, the relevant part is:

serialize :ratings, Hash
validates_hash_values_with :ratings, numericality: { greater_than: 0 }

The error I'm getting is:

NoMethodError: undefined method `new' for :ratings:Symbol
~/.rvm/gems/ruby-2.2.1/gems/validates_serialized-0.0.8/lib/validates_serialized/validators/serialized_validator.rb:12:in `block in initialize'
~/.rvm/gems/ruby-2.2.1/gems/validates_serialized-0.0.8/lib/validates_serialized/validators/serialized_validator.rb:11:in `each'
~/.rvm/gems/ruby-2.2.1/gems/validates_serialized-0.0.8/lib/validates_serialized/validators/serialized_validator.rb:11:in `initialize'
~/.rvm/gems/ruby-2.2.1/gems/validates_serialized-0.0.8/lib/validates_serialized/validators/hash_validator.rb:22:in `new'
~/.rvm/gems/ruby-2.2.1/gems/validates_serialized-0.0.8/lib/validates_serialized/validators/hash_validator.rb:22:in `validates_hash_values_with'
...

Some versions that may be relevant:

Using activesupport 4.2.1
Using activemodel 4.2.1
Using activerecord 4.2.1
Using dm-core 1.2.1
Using dm-validations 1.2.0
Using pg 0.18.3
Using rails 4.2.1
Using spring 1.4.0
Using validates_serialized 0.0.8

Any idea?

The gem does not work with rails 3.*

Example:

#!/usr/bin/env ruby

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'

  gem 'activemodel', '3.2.22'
  gem 'validates_serialized', '0.0.8'
end

class ValidatorArrayTestOne
  include ActiveModel::Validations
  validates_array_values :my_attr, presence: true, inclusion: { in: [1, 2, 3, 4] }

  def initialize(h={})
    h.each {|k,v| send("#{k}=",v)}
  end

  def my_attr
    @my_attr ||= []
  end

  def my_attr=(val)
    @my_attr = val
  end
end

record = ValidatorArrayTestOne.new(my_attr: [1, 5])
puts record.valid?

Output:

Resolving dependencies...
Using i18n 0.7.0
Using multi_json 1.11.2
Using activesupport 3.2.22
Using builder 3.0.4
Using activemodel 3.2.22
Using validates_serialized 0.0.8
Using bundler 1.10.6
/home/dxdy/.rbenv/versions/1.9.3-p545/lib64/ruby/gems/1.9.1/gems/activemodel-3.2.22/lib/active_model/validator.rb:160:in `validate_each': Subclasses must implement a validate_each(record, attribute, value) method (NotImplementedError)

embedded validates with "if"-Proc don't receive model_instance

If I have a nested validation:

serialize :data, Hash
validates_hash_keys :data do
  validates :url, presence: true, if: Proc.new{|f| f.required_data_field?(:url) }
end

The f in the Proc won't be the model instance as usual, but a ActiveModel::Validations::HashBlockValidator::TempValidateableHash_. I could not find a way in the source to access the model_instance form this block validator, however, usually you will need to access the model for conditional validations.

Validateable Objects need to clear their validators

When we validated serialized objects, we use one of three classes: ValidateableHash, ValidateableArrayValue, or ValidateableObject.

In the block validators though, we calls a 'class_eval' on the instances of those objects, which adds new validations to that class instead of overwriting the existing validations. This causes projects with lots of use of these kinds of validations to break in unpredictable ways.

As a solution, we need to clear the validators on these classes after every validation is run.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.