{"id":15432352,"url":"https://github.com/serradura/type_validator","last_synced_at":"2025-04-28T11:47:26.771Z","repository":{"id":59158413,"uuid":"208680716","full_name":"serradura/type_validator","owner":"serradura","description":"Adds type validation for classes with ActiveModel::Validations.","archived":false,"fork":false,"pushed_at":"2020-01-04T01:32:38.000Z","size":62,"stargazers_count":17,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T01:18:46.134Z","etag":null,"topics":["activemodel","ruby","ruby-gem","ruby-on-rails","type-checking","validations"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/type_validator","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/serradura.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-16T01:00:24.000Z","updated_at":"2021-12-10T00:52:19.000Z","dependencies_parsed_at":"2022-09-13T20:10:53.350Z","dependency_job_id":null,"html_url":"https://github.com/serradura/type_validator","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serradura%2Ftype_validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serradura%2Ftype_validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serradura%2Ftype_validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serradura%2Ftype_validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/serradura","download_url":"https://codeload.github.com/serradura/type_validator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251309831,"owners_count":21568911,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activemodel","ruby","ruby-gem","ruby-on-rails","type-checking","validations"],"created_at":"2024-10-01T18:26:12.537Z","updated_at":"2025-04-28T11:47:26.751Z","avatar_url":"https://github.com/serradura.png","language":"Ruby","readme":"![Ruby](https://img.shields.io/badge/ruby-2.2+-ruby.svg?colorA=99004d\u0026colorB=cc0066)\n[![Gem](https://img.shields.io/gem/v/type_validator.svg?style=flat-square)](https://rubygems.org/gems/type_validator)\n[![Build Status](https://travis-ci.com/serradura/type_validator.svg?branch=master)](https://travis-ci.com/serradura/type_validator)\n[![Maintainability](https://api.codeclimate.com/v1/badges/cf8b233beedae37b82dd/maintainability)](https://codeclimate.com/github/serradura/type_validator/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/cf8b233beedae37b82dd/test_coverage)](https://codeclimate.com/github/serradura/type_validator/test_coverage)\n\n# TypeValidator \u003c!-- omit in toc --\u003e\n\nAdds type validation for classes with [`ActiveModel::Validations \u003e= 3.2`](https://api.rubyonrails.org/classes/ActiveModel/Validations.html).\n\n## Table of Contents \u003c!-- omit in toc --\u003e\n- [Required Ruby version](#required-ruby-version)\n- [Installation](#installation)\n- [Usage](#usage)\n  - [Default validation](#default-validation)\n  - [allow_nil option and strict mode](#allow_nil-option-and-strict-mode)\n- [Development](#development)\n- [Contributing](#contributing)\n- [License](#license)\n- [Code of Conduct](#code-of-conduct)\n\n## Required Ruby version\n\u003e \\\u003e= 2.2.0\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'type_validator'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install type_validator\n\n## Usage\n\nUse any of the type validations below into your models/classes:\n\n**[Object#instance_of?](https://ruby-doc.org/core-2.6.4/Object.html#method-i-instance_of-3F)**\n\n```ruby\nvalidates :name, type: { instance_of: String }\n\n# or use an array to verify if the attribute\n# is an instance of one of the classes\n\nvalidates :name, type: { instance_of: [String, Symbol] }\n```\n\n**[Object#kind_of?](https://ruby-doc.org/core-2.6.4/Object.html#method-i-kind_of-3F)**\n\n```ruby\nvalidates :name, type: { is_a: String }\n# or\nvalidates :name, type: { kind_of: String }\n\n# Use an array to verify if the attribute\n# is an instance of one of the classes\n\nvalidates :status, type: { is_a: [String, Symbol]}\n# or\nvalidates :status, type: { kind_of: [String, Symbol]}\n```\n\n**[Object#respond_to?](https://ruby-doc.org/core-2.6.4/Object.html#method-i-respond_to-3F)**\n\n```ruby\nvalidates :handler, type: { respond_to: :call }\n```\n\n**Class == Class || Class \u003c Class**\n\n```ruby\n# Verifies if the attribute value is the class or a subclass.\n\nvalidates :handler, type: { klass: Handler }\n```\n\n**Array.new.all? { |item| item.is_a?(Class) }**\n\n```ruby\nvalidates :account_types, type: { array_of: String }\n\n# or use an array to verify if the attribute\n# is an instance of one of the classes\n\nvalidates :account_types, type: { array_of: [String, Symbol] }\n```\n\n**Array.new.all? { |item| expected_values.include?(item) }**\n\n```ruby\n# Verifies if the attribute value\n# is an array with some or all the expected values.\n\nvalidates :account_types, type: { array_with: ['foo', 'bar'] }\n```\n\n### Default validation\n\nBy default, you can define the attribute type directly (without a hash). e.g.\n\n```ruby\nvalidates :name, type: String\n# or\nvalidates :name, type: [String, Symbol]\n```\n\nTo changes this behavior you can set another strategy to validates the attributes types:\n\n```ruby\nTypeValidator.default_validation = :instance_of\n\n# Tip: Create an initializer if you are in a Rails application.\n```\n\nAnd these are the available options to define the default validation:\n-  `kind_of` *(default)*\n-  `is_a`\n-  `instance_of`\n\n### `allow_nil` option and `strict` mode\n\nYou can use the `allow_nil` option with any of the type validations. e.g.\n\n```ruby\nvalidates :name, type: { is_a: String }, allow_nil: true\n```\n\nAnd any of the validations work with the`strict: true` option\nor with the `validates!` method. e.g.\n\n```ruby\nvalidates :name, type: { is_a: String }, strict: true\n#or\nvalidates! :name, type: { is_a: String }\n```\n\n## Development\n\nAfter 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.\n\nTo 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](https://rubygems.org).\n\n## Contributing\n\nBug 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](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the TypeValidator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/serradura/type_validator/blob/master/CODE_OF_CONDUCT.md).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserradura%2Ftype_validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fserradura%2Ftype_validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserradura%2Ftype_validator/lists"}