Project

valcro

0.02
Low commit activity in last 3 years
No release in over a year
Valcro is a validation library for arbitrary ruby objects. I's primary goal is to remain as simple as possible so that it can be useful to a large surface of use cases remaining easily extensible.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Valcro

Valcro is the simple validation library for Ruby. It provides

  • A declarative way to describe what makes an object valid
  • A way to declare validator classes encapsulating validations to be shared across objects
  • A method for running validations against an instance of your class
  • A method for checking whether an object is valid
  • Visibility into what validations failed

It is similar in spirit to ActiveModel's or Sequel's validations, but far simpler and with a slightly different API. It does not provide validation helpers such as validates_presence_of or similar.

Example

require 'valcro'

class Dog
  include Valcro

  attr_accessor :name

  validate do
    errors.add(:name, 'must be great') unless name =~ /super|great|cool/
  end
end

dog = Dog.new
dog.name = 'spike'
dog.validate
dog.valid?
 => false
dog.errors[:name]
 => ["must be great"]
dog.error_messages
 => "name must be great"

Unlike other ruby validation libraries, you must call validate explicitely to check for validity of objects.

Sharing validators

You may also have Cats in your system, which share the same validation. Valcro lets you encapsulate validations in an object with the following contract:

  • It is constructed with the object being validated
  • It responds to call, which receives a Valcro::ErrorList. Add validation errors to this object.
class GreatNameValidator
  def initialize(thing_with_name)
    @thing_with_name = thing_with_name
  end

  def call(errors)
    errors.add(:name, 'must be great') unless @thing_with_name.name =~ /super|great|cool/
  end
end

class Dog
  include Valcro
  attr_accessor :name
  validates_with GreatNameValidator
end

class Cat
  include Valcro
  attr_accessor :name
  validates_with GreatNameValidator
end

License

Valcro is copyright (c) Harold Giménez and is released under the terms of the MIT License found in the LICENSE file.