NamedValidations

NamedValidations defines aliases for a set of validations for ActiveModel's validates method.

Example:

class CommonValidations < NamedValidations
  define :required, :presence, true
  define :short_text, :length, maximum: 100
  define :long_text, :length, maximum: 5_000
  define :formatted_text, :format,
    with: /\A(?:[^:[:space:]]+:[^\n]*(\n\s+[^\n]*)*\n)*\z/,
    allow_blank: true
  define :formatted_long_text do |*_args|
    long_text.formatted_long_text
  end
end

class Article < ApplicationRecord
  base = CommonValidations.new
  validates :title, base.short_text.required
  validates :content, base.long_text(minmum: 100)
  validates :meta, base.formatted_long_text
end

Installation

Add this line to your application's Gemfile:

gem 'named_validations'

And then execute:

$ bundle

Or install it yourself as:

$ gem install named_validations

Usage

Simple alias (define-by-value)

define :required, :presence, true

First argument is a name for the defining alias. Second argument is a validation name, and third argument is parameters for it.

The above code defines new alias :required which is equal to { presence: true }.

Validation name can be any of defined aliases including your aliases. NamedValidations has pre-defined aliases such as:

  • :absence
  • :acceptance
  • :associated
  • :confirmation
  • :exclusion
  • :format
  • :inclusion
  • :length
  • :numericality
  • :presence
  • :size
  • :uniqueness

Parameters is usually Boolean or Hash.

define :short_text, :length, minimum: 5, maximum: 78

It is equal to { length: { minimum: 5, maximum: 78 } }.

Using alias

NamedValidations instance can receive a defined alias.

Simple alias and pre-defined aliases can take one or more arguments. The argument is merged into return value of the alias.

class Sample < NamedValidations
  define :required, :presence, true
end

base.required                   # represents: { presence: true }
base.required(false)            # represents: { presence: false }
base.required(on: :create, ...) # represents: { presence: { on: :create, ... } }

base.required(false, { on: :create, foo: :bar }, { on: :update })
# represents: { presence: { on: :update, foo: :bar } }

Alias returns new NamedValidations instance.

base = Sample.new # => #<Sample:0x3fe051053ec8>
base.required     # => #<Sample:0x3fe0510132ec>

So you can chain aliases.

base.length(maximum: 78).format(with: /@/)
# represents: { length: { maximum: 78}, format: { with: /@/ } }

vals = base.length(maximum: 78).length(minimum: 5)
# represents: { length: { maximum: 78, minimum: 5 } }

vals.length(maximum: 1_000)
# represents: { length: { maximum: 1_000, minimum: 5 } }

Complex alias (define-by-block)

You can define more complex alias by using block.

define :long_format_text do |arg = true, *args|
  length(maximum: 1_000)
  .format({ with: /@/ }, *args)
  .presence(arg)
end

define :clear do |*opts|
  Sample.new
end

The block should return a new NamedValidations instance.

Defined aliases can take some arguments according to the block arguments.

Defaults options

NamedValidations has special alias :defaults. It applies default options for validations.

validates :title, length(...).format(...).defaults(if: -> { ... })
# same as:
#   validates :title, length: { ... }, format: { ... }, if: -> { ... }

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test 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/arika/named\_validations.

License

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