Skip to content
Adds type validation for classes with ActiveModel::Validations.
Ruby Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Gem Build Status Maintainability Test Coverage


Adds type validation for classes with ActiveModel::Validations >= 3.2.

Required Ruby version

>= 2.2.0


Add this line to your application's Gemfile:

gem 'type_validator'

And then execute:

$ bundle

Or install it yourself as:

$ gem install type_validator


Use any of the type validations below into your models/classes:


validates :name, type: { instance_of: String }

# or use an array to verify if the attribute
# is an instance of one of the classes

validates :name, type: { instance_of: [String, Symbol] }


validates :name, type: { is_a: String }
# or
validates :name, type: { kind_of: String }

# Use an array to verify if the attribute
# is an instance of one of the classes

validates :status, type: { is_a: [String, Symbol]}
# or
validates :status, type: { kind_of: [String, Symbol]}


validates :handler, type: { respond_to: :call }

Class == Class || Class < Class

# Verifies if the attribute value is the class or a subclass.

validates :handler, type: { klass: Handler } { |item| item.is_a?(Class) }

validates :account_types, type: { array_of: String }

# or use an array to verify if the attribute
# is an instance of one of the classes

validates :account_types, type: { array_of: [String, Symbol] } { |item| expected_values.include?(item) }

# Verifies if the attribute value
# is an array with some or all the expected values.

validates :account_types, type: { array_with: ['foo', 'bar'] }

Default validation

By default, you can define the attribute type directly (without a hash). e.g.

validates :name, type: String
# or
validates :name, type: [String, Symbol]

To changes this behavior you can set another strategy to validates the attributes types:

TypeValidator.default_validation = :instance_of

# Tip: Create an initializer if you are in a Rails application.

And these are the available options to define the default validation:

  • kind_of (default)
  • is_a
  • instance_of

allow_nil option and strict mode

You can use the allow_nil option with any of the type validations. e.g.

validates :name, type: { is_a: String }, allow_nil: true

And any of the validations work with thestrict: true option or with the validates! method. e.g.

validates :name, type: { is_a: String }, strict: true
validates! :name, type: { is_a: String }


After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.


The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the TypeValidator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

You can’t perform that action at this time.