Giter Site home page Giter Site logo

aequitas's Introduction

This module provides validations for any Ruby class.

Specifying Validations

There are two primary ways to implement validations

1) Placing validation methods with properties as params in your class

require 'aequitas'

class ProgrammingLanguage
  include Aequitas

  attr_accessor :name

  validates_presence_of :name
end

2) (TODO) Using inferred validations on Virtus attributes, please see Aequitas::Inferred. Note that not all validations that are provided via validation methods, are also available as autovalidation options. If they are available, they’re functionally equivalent though.

class ProgrammingLanguage
  include Virtus
  include Aequitas

  attribute :name, String, :required => true
end

See Aequitas::Macros to learn about the complete collection of validation rules available.

Validating

Aequitas validations may be manually evaluated against a resource using the ‘#valid?` method, which will return true if the resource is valid, and false if it is invalid.

Working with Validation Errors

If an instance fails one or more validation rules, Aequitas::Violation instances will populate the Aequitas::ViolationSet object that is available through the #errors method.

For example:

my_account = Account.new(:name => "Jose")
if my_account.valid?
  # my_account is valid and has been saved
else
  my_account.errors.each do |e|
    puts e
  end
end

See Aequitas::ViolationSet for all you can do with the #errors method.

Contextual Validation

Aequitas also provide a means of grouping your validations into contexts. This enables you to run different sets of validations when you need it. For example, an instance may require separate validation rules depending on its state: publishing, exporting, importing and so on.

Again, using our example for pure Ruby class validations:

class ProgrammingLanguage
  include Virtus
  include Aequitas

  attribute :name, String

  def ensure_allows_manual_memory_management
    # ...
  end

  def ensure_allows_optional_parentheses
    # ...
  end

  validates_presence_of :name
  validates_with_method :ensure_allows_optional_parentheses,     :when => [:implementing_a_dsl]
  validates_with_method :ensure_allows_manual_memory_management, :when => [:doing_system_programming]
end

ProgrammingLanguage instance now use #valid? method with one of two context symbols:

@ruby.valid?(:implementing_a_dsl)       # => true
@ruby.valid?(:doing_system_programming) # => false

@c.valid?(:implementing_a_dsl)       # => false
@c.valid?(:doing_system_programming) # => true

Each context causes different set of validations to be triggered. If you don’t specify a context using :when, :on or :group options (they are all aliases and do the same thing), default context name is :default. When you do model.valid? (without specifying context explicitly), again, :default context is used. One validation can be used in two, three or five contexts if you like:

class Book
  include Virtus
  include Aequitas

  attribute :id,           Serial
  attribute :name,         String

  attribute :agreed_title, String
  attribute :finished_toc, Boolean

  # used in all contexts, including default
  validates_presence_of :name,         :when => [:default, :sending_to_print]
  validates_presence_of :agreed_title, :when => [:sending_to_print]

  validates_with_block :toc, :when => [:sending_to_print] do
    if self.finished_toc
      [true]
    else
      [false, "TOC must be finalized before you send a book to print"]
    end
  end
end

In the example above, name is validated for presence in both :default context and :sending_to_print context, while TOC related block validation and title presence validation only take place in :sending_to_print context.

aequitas's People

Contributors

azimux avatar benburkert avatar bernerdschaefer avatar dbussink avatar dkubb avatar eclubb avatar emmanuel avatar foysavas avatar gix avatar joe avatar jpr5 avatar mayo avatar michaelklishin avatar myabc avatar namelessjon avatar ndarilek avatar pathsny avatar paul avatar pol avatar postmodern avatar rwestgeest avatar sam avatar senny avatar sfeley avatar shanna avatar snusnu avatar solnic avatar somebee avatar xaviershay avatar zirni avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.