brycesenz / validates_serialized Goto Github PK
View Code? Open in Web Editor NEWValidation helpers for serialized ActiveModel attributes
Validation helpers for serialized ActiveModel attributes
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.
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?
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)
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.
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.
the method in validating-a-serialized-array-syntax-1 should be validates_array_values
Is it possible to use a conditional validation with validates_serialized gem ?
If not, I am able to contribute with it.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.