Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yez/validates_type
Type Validation for Rails
https://github.com/yez/validates_type
hacktoberfest
Last synced: 3 months ago
JSON representation
Type Validation for Rails
- Host: GitHub
- URL: https://github.com/yez/validates_type
- Owner: yez
- License: mit
- Created: 2015-04-29T04:45:04.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-01-18T19:15:06.000Z (almost 2 years ago)
- Last Synced: 2024-07-25T15:23:21.538Z (4 months ago)
- Topics: hacktoberfest
- Language: Ruby
- Homepage:
- Size: 55.7 KB
- Stars: 49
- Watchers: 3
- Forks: 8
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
## validates_type
[![Build Status](https://travis-ci.org/yez/validates_type.svg?branch=master)](https://travis-ci.org/yez/validates_type)
[![Open Source Helpers](https://www.codetriage.com/yez/validates_type/badges/users.svg)](https://www.codetriage.com/yez/validates_type)### Rails type validation
#### Purpose
Most Rails applications will have types coerced by their ORM connection adapters (like the `pg` gem or `mysql2`). However, this only useful for applications with very well defined schemas. If your application has a legacy storage layer that you can no longer modify or a lot of `store_accessor` columns, this solution is a nice middle ground to ensure your data is robust.
This also prevents your data from being coerced into values that you did not intend by ActiveRecord adapters.
#### Usage
##### With ActiveRecord
```ruby
class Foo < ActiveRecord::Base# validate that attribute :bar is a String
validates_type :bar, :string# validate that attribute :baz is an Integer with a custom error message
validates_type :baz, :integer, message: 'Baz must be an Integer'# validate that attribute :qux is an Array, allow blank
validates_type :qux, :array, allow_blank: true# validate that attribute :whatever is a Boolean
validates_type :whatever, :boolean# validate that attribute :thing is a Float or nil
validates_type :thing, :float, allow_nil: true# validate that attribute :when is a Time
validates_type :when, :time# validate that attribute :birthday is a Date or blank
validates_type :birthday, :date, allow_blank: true# validate that attribute :fox is of type Custom
validates_type :fox, Custom
end
```##### With ActiveModel
```ruby
class Bar
include ActiveModel::Validationsattr_accessor :foo, :qux
validates_type :foo, :string
# Custom error message support
validates_type :qux, :boolean, message: 'Attribute qux must be a boolean!'
end
```##### With shortcut syntax
```ruby
class Banana < ActiveRecord::Base# The banana's peel attribute must be a string
validates :peel, type: { type: :string }# Custom error message for ripeness of banana
validates :ripe, type: { type: :boolean, message: 'Only ripe bananas allowed' }
end
```##### With multiple modifiers
```ruby
class Foo < ActiveRecord::Base
# validate that attribute :baz is an Integer with a custom error message
# only if :conditional_method evaluates to true
validates_type :baz, :integer, message: 'Baz must be an Integer', if: :conditional_methoddef conditional_method
# some kind of logic that is important to pass
end# validate that attribute :baz is a Float and is included in a specific array
validates_type :foo, :float, in: [1.0, 2.5, 3.0]
end
```#### Supported types
- `:array`
- `:big_decimal`
- `:boolean`
- `:date`
- `:float`
- `:hash`
- `:integer`
- `:string`
- `:symbol`
- `:time`#### Any class name, possibly something custom defined in your app, is also game.
#### Contributing
Please feel free to submit pull requests to address issues that you may find with this software. One commit per pull request is preferred please and thank you.