# Validates Type 2.0

DevFeed: [Validates Type 2.0](<https://devfeed.tech/articles/validates-type-2-0-21052.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2015/11/29/updated-validates-type/>)

Published: 2015-11-29T12:00:00Z

Content type: article

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [Library](<https://devfeed.tech/topics/library.md>), [Databases](<https://devfeed.tech/topics/databases.md>)

Tags: [activerecord](<https://devfeed.tech/tags/activerecord.md>), [book](<https://devfeed.tech/tags/book.md>), [information](<https://devfeed.tech/tags/information.md>), [library](<https://devfeed.tech/tags/library.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [serialization](<https://devfeed.tech/tags/serialization.md>), [validation](<https://devfeed.tech/tags/validation.md>)

## AI overview

The article introduces Validates Type 2.0, a Ruby gem that validates whether values exactly match expected types. The update expands validation from basic Ruby types to any defined system type and adds greater flexibility for serialized attributes, including use cases involving ActiveRecord models and legacy database columns.

## Source excerpt

One of the first projects that I worked on during this Year of Commits was validates_type. In case that name is too obscure, validates_type is a gem for validating that a specific value is exactly the type it is expected to be. The validates_type library is compatible to ActiveModel style validations. Originally, the validates_type gem was very strict with what it validated against. It started with basic included Ruby types like Integer, Float, and String. This was a fine first step but proved too restrictive for basically any use case. In a recent update, the validates_type gem has been extended to validate against any defined type in a system. With this update, the uses of this gem greatly increased. They increased so much that it is possible more than 2 people will use it, and that would be pretty awesome. Still Valid The validates_type gem can be useful for a system that cares about exact types at save time. For instance, a model that has an incorrect database column type (because of legacy or other reasons) can still control validation over its data just the same. If a "junior developer who is now somehow the CTO" created the original system. And if that developer did something like create an is_published column on the authors table set as a varchar, validates_type makes that less of an issue. class Author < ActiveRecord::Base validates_type :is_published, :boolean end If someone tries to assign a nonsense value to the is_published attribute, validates_type will ensure it does not save. Note: callback skipping methods will still save bad values, just like any other ActiveRecord validators. > author = Author.new # => #<Author id: nil, name: nil, created_at: nil, updated_at: nil, is_published: nil> > author.is_published = 'foo' # => "foo" > author.save! # => ActiveRecord::RecordInvalid: Validation failed: is_published is expected to be a Boolean and is not. This way, there is not a bunch of random data in the authors table because of a bad decision made a while a