Project

rite

0.0
No commit activity in last 3 years
No release in over 3 years
Easily define complex validator pipelines and rules for Ruby classes/hashes/anything.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.13.0
~> 12.0
~> 3.0
 Project Readme

Rite

Rspec

Define validators for Ruby classes and objects. Rite provides a simple, chainable validator solution based on the principles of functional design: small, composable validators used to build complex validation logic.

Roadmap to v1

  • gemspec
  • Validator base class
  • Basic validators
    • type validator
    • value validator
    • required validator
    • numeric validator
    • hash validator
    • array validator
  • Transformers (chainable with validators but transform data/error messages)
  • Passage
  • "Friendly" DSL
    • DSL for validator
      • define validate function
      • define custom failure message
      • define error handling
    • DSL for transformer
    • DSL for Passage

Installation

Add this line to your application's Gemfile:

gem 'rite'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install rite

Usage

It starts with defining a small validator, like a validating type:

string_validator = Rite.define_validator do
  validate do |value|
    value.is_a?(String)
  end

  failure_message '"%value" must be a string'
end

And now we can use it to validate some input:

some_value = get_data_from_somewhere
if string_validator.valid?(some_value)
  # It's a string!
else
  # It's not a string!
  puts string_validator.failure_message(some_value)
end

But it's obvious this is overkill just to perform a simple #is_a? call. That's what Rite::Passage solves -- chaining. We usually don't want to verify just type in real world applications, we may want to verify that it matches some expected format:

(NOTE: This API is not yet implemented and subject to change)

ssn_validator = Rite::Validators
  .string # .is_a?(String)
  .matches(/\d{3}=\d{2}-\d{4}/) # .matches?(regex)
  .required # !.nil? && .length > 0
positive_integers = Rite::Validators
  .integer # .is_a?(Integer)
  .greater_than(0, :or_equal) # >= 0
  .required # !.nil? && .length > 0

These validators create "chains" that will run the value through multiple validators. These passages can also be used to chain other passages!

valid_ssn_validator = Rite::Validators
  .with(ssn_validator)
  .not_equal('000-00-0000')

Or you can use specialized validtors to inject transformations to the output:

# arrow points to failure message should that step fail but the final #message
# means if any step fails the failure message provided to #message will be the
# return value
ssn_validator = Rite::Validators
  .string # -> '"value" was expected to be String'
  .matches(/\d{3}=\d{2}-\d{4}/) # -> '"value" did not match expected format'
  .required # -> 'cannot be nil or blank'
  .message('"%value" is not a valid SSN') # overrides any failure message by this piont
  # -> '"value" is not a valid SSN"

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To 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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/bbuck/rite. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

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

Code of Conduct

Everyone interacting in the Rite project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.