Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/neovintage/accord
Validations for Crystal Objects
https://github.com/neovintage/accord
crystal
Last synced: 15 days ago
JSON representation
Validations for Crystal Objects
- Host: GitHub
- URL: https://github.com/neovintage/accord
- Owner: neovintage
- License: mit
- Created: 2016-11-17T17:22:34.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-11-12T13:29:04.000Z (almost 4 years ago)
- Last Synced: 2024-10-04T21:33:32.023Z (about 1 month ago)
- Topics: crystal
- Language: Crystal
- Homepage:
- Size: 16.6 KB
- Stars: 24
- Watchers: 4
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crystal - accord - Shareable validation library for Crystal Objects (Validation)
- awesome-crystal - accord - Sharable validations for Crystal objects (Misc)
- awesome-crystal - accord - Shareable validation library for Crystal Objects (Validation)
README
# Accord
![CI](https://github.com/neovintage/accord/workflows/CI/badge.svg)
A validation library for Crystal Objects which takes its inspiration from [Valcro](https://github.com/hgmnz/valcro), a simple validation library for Ruby. There are some differences between the Crystal version and Ruby that you'll need to pay attention to.
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
accord:
github: neovintage/accord
```## Usage
Validations can be defined within the object that needs to be validated or as separate classes. When using the inline method of validation,
an instance method of `validate` needs to be defined. This allows crystal to access all of the instance variables that exist as part of the
instantiated object.```crystal
require "accord"class Dog
include Accordproperty name
def validate
errors.add(:name, "must be tough") if name != "spike"
end
enddog = Dog.new
dog.name = "chuck"
dog.validate!
dog.valid? # false
dog.error_messages # ["name must be tough"]
dog.name = "spike"
dog.validate!
dog.valid? # true
```### Sharing Validations
One of the big values for this library is the ability to share validations across objects. When creating a validation as an object,
the validator object must:* Accept the object to be validated as a parameter to the constructor
* Define a `call` instance method that accepts a parameter of type `Accord::ErrorList`
* Be a subclass of `Accord::Validator````crystal
require "accord"class NameValidator < Accord::Validator
def initialize(context)
@context = context
enddef call(errors : Accord::ErrorList)
if @context.name != "spike"
errors.add(:name, "must be spike")
end
end
endclass Dog
include Accord
validates_with [ NameValidator ]
endclass Cat
include Accord
validates_with [ NameValidator ]
end
```In cases where you need to be more explicit when sharing validator objects, specifying a union type to the constructor may be necessary.
Here's a partial example:```crystal
alias AnimalValidationTypes = (Dog | Cat)class NameValidator < Accord::Validator
def initialize(context : AnimalValidationTypes)
end
end
```### Mixing Validations
Accord can mix inline and sharable validations. In terms of the order of operations, sharable validations occur first and then inline
validations. The sharable validations are executed in the order that they're defined within the Array passed to `validates_with`.```crystal
class Dog
include Accordvalidates_with [ NameValidator, AgeValidator ]
def validate
errors.add(:base, "Shouldn't be barking") if night == true && barking == true
end
end
```In this example, `NameValidator` would be executed first, then `AgeValidator` and finally the `validate` method.
### Adding Errors
The `ErrorList` instance acts allows you to add new errors directly with the `add` instance method. When specifying the
the name of the object it must be a symbol and when that error is turned into a string, the message is appended to the
name of the symbol.If you don't want the error message to prepend the symbol, a special symbol identifier exists called `:base`.
```crystal
errors = Accord::ErrorList.new
errors.add(:base, "I like writing my own error msgs")
errors.add(:name, "must be awesome")
errors.full_messages # ["I like writing my own error msgs", "name must be awesome"]
```## Contributing
1. Fork it ( https://github.com/neovintage/accord/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request## Contributors
- [neovintage](https://github.com/neovintage) Rimas Silkaitis - creator, maintainer