0.01
No commit activity in last 3 years
No release in over 3 years
Adds type validation for classes with ActiveModel::Validations.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 5.0
~> 10.0

Runtime

 Project Readme

Ruby Gem Build Status Maintainability Test Coverage

TypeValidator

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

Table of Contents

  • Required Ruby version
  • Installation
  • Usage
    • Default validation
    • allow_nil option and strict mode
  • Development
  • Contributing
  • License
  • Code of Conduct

Required Ruby version

>= 2.2.0

Installation

Add this line to your application's Gemfile:

gem 'type_validator'

And then execute:

$ bundle

Or install it yourself as:

$ gem install type_validator

Usage

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

Object#instance_of?

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] }

Object#kind_of?

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]}

Object#respond_to?

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 }

Array.new.all? { |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] }

Array.new.all? { |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
#or
validates! :name, type: { is_a: String }

Development

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 rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/serradura/type_validator. 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.

License

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.