Project

rasti-form

0.0
Low commit activity in last 3 years
No release in over a year
Forms validations and type casting
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.8
~> 5.0, < 5.11
~> 0.2
~> 12.0
~> 0.12

Runtime

 Project Readme

Rasti::Form

Gem Version CI Coverage Status Code Climate

Forms validations

Installation

Add this line to your application's Gemfile:

gem 'rasti-form'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rasti-form

Usage

Form type coercion

T = Rasti::Types

PointForm = Rasti::Form[x: T::Integer, y: T::Integer] # => PointForm[:x, :y]
form = PointForm.new x: '1', y: 2 # => #<PointForm[x: 1, y: 2]>
form.x # => 1
form.y # => 2
form.to_h # => {x: 1, y: 2}

PointForm.new x: true # => Validation errors:
                      #    - x: ["Invalid cast: true -> Rasti::Types::Integer"]

Form validations

class DateRangeForm < Rasti::Form
  TIME_FORMAT = '%d/%m/%Y'

  attribute :from, T::Time[TIME_FORMAT]
  attribute :to,   T::Time[TIME_FORMAT]

  private

  def validate
    if assert_present :from
      assert :from, from > Time.parse('2000-01-01'), 'From must be greater than 01/01/2000'
    end
    assert_present :to
    assert :from, from <= to, 'From must be less than To' if assigned?(:from) && assigned?(:to)
  end
end

DateRangeForm.new # => Validation errors:
                  #    - from: ["not present"]
                  #    - to: ["not present"]

DateRangeForm.new from: '15/07/1999', to: '08/10/2016' # => Validation errors: 
                                                       #    - from: ["From must be greater than 01/01/2000"]

DateRangeForm.new from: '20/10/2016', to: '08/10/2016' # => Validation errors: 
                                                       #    - from: ["From must be less than To"]

form = DateRangeForm.new from: '20/10/2016', to: '28/10/2016'
form.from # => 2016-10-20 00:00:00 -0300
form.to   # => 2016-10-28 00:00:00 -0300

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/gabynaiman/rasti-form.

License

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