Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mbj/vanguard
External validations for ruby objects
https://github.com/mbj/vanguard
Last synced: 13 days ago
JSON representation
External validations for ruby objects
- Host: GitHub
- URL: https://github.com/mbj/vanguard
- Owner: mbj
- License: mit
- Created: 2013-03-21T09:54:44.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-06-14T16:34:13.000Z (over 9 years ago)
- Last Synced: 2024-10-19T12:03:50.388Z (25 days ago)
- Language: Ruby
- Size: 224 KB
- Stars: 120
- Watchers: 12
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
vanguard
========This library provides external validations for any Ruby class.
History:
--------It originates from [emmanuels aequitas repository](https://github.com/emmanuel/vanguard)
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](https://github.com/dkubb/equalizer) and [adamantium](https://github.com/dkubb/adamantium) where possible.## Specifying Validations
```ruby
require 'vanguard'class ProgrammingLanguage
attr_reader :namedef initialize(name)
@name = name
end
endVALIDATOR = Vanguard::Validator.build do
validates_presence_of :name
endruby = ProgrammingLanguage.new('ruby')
result = VALIDATOR.call(ruby)
result.valid? # => true
result.violations # => #other = ProgrammingLanguage.new('')
result = VALIDATOR.call(other)
result.valid? # => false
result.violations # => #}>
```See `Vanguard::Macros` to learn about the complete collection of validation rules available.
## Credits
* Markus Schirp [mbj](https://github.com/mbj)
* Emmanuel Gomez [emmanuel](https://github.com/emmanuel)## 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:
```ruby
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.