Kemalyst Validators provides a standard way of creating validation for models. The library is built for Kemalyst but can be leveraged by any plain old object.
Add this to your application's
dependencies: kemalyst-validators: github: drujensen/kemalyst-validators
Kemalyst::Validators is a mix-in that you include in your class:
require "kemalyst-validators" class Person include Kemalyst::Validators property name : String? validate "Name is required", -> (this : Person) do return this.name != nil end validate "Name must be 3 characters long", -> (this : Person) do if name = this.name return name.size > 2 end return true end def initialize(@name = nil) end end
validate macro takes two parameters. The first is the message that will
display when the validation fails. The second is a
Proc that is provided an
self and returns either true or false.
To check to see if your instance is valid, call
valid?. Each Proc will be
called and if any of them fails, an
errors Array with the messages is
person = Person.new(name: "JD") person.valid?.should eq false person.errors.should eq "Name must be 3 characters long"
-  Provide standard validators that can be used per field
-  Provide field name that fails in the errors Array
- Fork it ( https://github.com/drujensen/kemalyst-validators/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
- drujensen Dru Jensen - creator, maintainer