Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zinovyev/validacity

Validation objects for Rails
https://github.com/zinovyev/validacity

Last synced: 1 day ago
JSON representation

Validation objects for Rails

Awesome Lists containing this project

README

        

# Validacity

Move validation definitions out of you models and form objects.

## Usage

You can apply multiple sets of validation rules on-fly,
remove any them if not needed and re-validate object against
new rules afterwards.

By the way, the validation errors will be putten to the object
so all your view helpers will be available to display the error list.

This approach will suitable if you need to validate different states
of the same object differently. Say you're using a state machine
and each state has it's own field set and the fields from the other
satates shouldn't be validated at this time.

Or perhaps you just have to many constructions like
`validate :blablabla, presence: true` in your model class and you want to
get rid of it so you can focus on buisiness logic.

You can add a concern to your model so it becomes **validatable**:

```ruby

# app/models/user.rb

class User
include Validacity::Validatable
validations :user_personal_data
end

```

Or you can add it directly to your object:

```ruby

user = User.new
user.validations :user_personal_data

```

Generate new validation:

```bash

$ bundle exec rails g validacity:validation UserPersonalData

```

```ruby

# app/validations/user_personal_data_validation.rb

class UserPersonalDataValidation
validate :name, presence_of: true
# ...a ton of different validations
end

```

Now let's try to validate your user object:

```ruby

user = User.new

user.valid? # => false

user.name = "John"

uesr.valid? # => true

```

## Installation

Add this line to your application's Gemfile:

```ruby

gem 'validacity'

```

And then execute:

```bash

$ bundle

```

Now run the validation installation command:

```bash

$ bundle exec rails g validacity:install

```

And the event validation:

```bash

$ bundle exec rails g validacity:validation Event

```

## Contributing

Contribution directions go here.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).