
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
validates_serialized
Advanced tools
ActiveModel provides ways to validate attributes, and to serialize objects. This gem provides some ActiveModel extensions and syntactic sugar to simplify the process of validating those serialized objects.
This gem provides:
Add this line to your application's Gemfile:
gem 'validates_serialized', '~> 0.0.1'
And then execute:
$ bundle
Or install it yourself as:
$ gem install validates_serialized
Here we have an example, serializable class called 'Person' with a name and age attribute.
class Person
def initialize(h={})
h.each {|k,v| send("#{k}=",v)}
end
def name
@name ||= nil
end
def name=(val)
@name = val
end
def age
@age ||= nil
end
def age=(val)
@age = val
end
end
Now we can serialize this object and validate its properties in another class
class Family < ActiveRecord::Base
include ActiveModel
...
serialize :father, Person
validates_serialized :father do
validates :name, presence: true
validates :age, numericality: { greater_than: 21 }
end
end
The validations will be run against the serialized object whenever validation hooks are fired. E.g.
# With valid serialized object
valid_father = Person.new(name: "Bob", age: 31)
family = Family.new(father: valid_father)
family.valid? #=> true
# With invalid serialized object
valid_father = Person.new(name: "Bob", age: 13)
family = Family.new(father: valid_father)
family.valid? #=> false
family.errors[:father] #=> ["age must be greater than 13"]
class Comment < ActiveRecord::Base
include ActiveModel
...
serialize :metadata, Hash
validates_hash_keys :metadata do
validates :timestamp, presence: true
validates :locale, presence: true
end
end
# With valid hash
comment = Comment.new(metadata: { timestamp: Time.new(2014, 1, 1), locale: "Ohio" })
comment.valid? #=> true
# With invalid hash
comment = Comment.new(metadata: { timestamp: Time.new(2014, 1, 1), locale: nil })
comment.valid? #=> false
comment.errors[:metadata] #=> ["locale can't be blank"]
class Comment < ActiveRecord::Base
include ActiveModel
...
serialize :ratings, Hash
validates_hash_values_with :ratings, numericality: { greater_than: 0 }
end
# With valid hash
comment = Comment.new(ratings: { tom: 4, jim: 2 })
comment.valid? #=> true
# With invalid hash
comment = Comment.new(ratings: { tom: 4, jim: -1 })
comment.valid? #=> false
comment.errors[:ratings] #=> ["ratings must be greater than 0"]
class Comment < ActiveRecord::Base
include ActiveModel
...
serialize :tags, Array
validates_array_values_with :tags, length: { minimum: 4 }
end
# With valid hash
comment = Comment.new(tags: ["ruby" "rails"])
comment.valid? #=> true
# With invalid hash
comment = Comment.new(tags: ["ruby" "rails", "ror"])
comment.valid? #=> false
comment.errors[:tags] #=> ["tags is too short (minimum is 4 characters)"]
class Comment < ActiveRecord::Base
include ActiveModel
...
serialize :tags, Array
validates_each_in_array :tags do
validates :value, length: { minimum: 4 } #the attribute 'value' with access each value
end
end
# With valid hash
comment = Comment.new(tags: ["ruby" "rails"])
comment.valid? #=> true
# With invalid hash
comment = Comment.new(tags: ["ruby" "rails", "ror"])
comment.valid? #=> false
comment.errors[:tags] #=> ["tags is too short (minimum is 4 characters)"]
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)FAQs
Unknown package
We found that validates_serialized demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.