Need help with vanguard?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

mbj
121 Stars 4 Forks MIT License 27 Commits 5 Opened issues

Description

External validations for ruby objects

Services available

!
?

Need anything else?

Contributors list

# 38,101
Ruby
Rails
rspec
mutatio...
16 commits
# 56,577
Ruby
Shell
Rails
devise
4 commits
# 356,649
Redux
TypeScr...
Shell
rspec
1 commit

vanguard

This library provides external validations for any Ruby class.

History:

It originates from emmanuels aequitas repository with the following changes:

  • Only support for external validators
  • Use composable algebra for internals
  • Will allow serialization to javascript for client side validation (not implemented)
  • No contextual validators anymore (use additional external validators)
  • 100% code rewrite
  • Use equalizer and adamantium where possible.

Specifying Validations

require 'vanguard'

class ProgrammingLanguage attr_reader :name

def initialize(name) @name = name end end

VALIDATOR = Vanguard::Validator.build do validates_presence_of :name end

ruby = ProgrammingLanguage.new('ruby')

result = VALIDATOR.call(ruby) result.valid? # => true result.violations # => #set:

other = ProgrammingLanguage.new('')

result = VALIDATOR.call(other) result.valid? # => false result.violations # => #<set: ....>}>

See

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

Credits

Working with Validation Errors

If an instance fails one or more validation rules,

Vanguard::Violation
instances will populate the
Vanguard::ViolationSet
object that is available through the
Vanguard::Result#violations
method.

Vanguard currently has no support for generating human readable violation messages!

For example:

result = YOUR_VALIDATOR.call(Account.new(:name => "Jose"))
if result.valid?
  # my_account is valid and can be saved
else
  result.violations.each do |e|
    do_something_with(e)
  end
end

Contextual Validation

Vanguard does not provide a means of grouping your validations into contexts. Define a validator per context for this.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.