https://github.com/am-kantox/mutations-validate-outcome
Mixin to add an outcome validation to mutations gem
https://github.com/am-kantox/mutations-validate-outcome
Last synced: 24 days ago
JSON representation
Mixin to add an outcome validation to mutations gem
- Host: GitHub
- URL: https://github.com/am-kantox/mutations-validate-outcome
- Owner: am-kantox
- License: mit
- Created: 2016-05-18T13:35:46.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-22T15:07:48.000Z (almost 8 years ago)
- Last Synced: 2025-02-09T19:53:12.373Z (3 months ago)
- Language: Ruby
- Size: 29.3 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Mutations :: Mixin for validating outcome
[](https://travis-ci.org/am-kantox/mutations-validate-outcome)
[](https://codeclimate.com/github/am-kantox/mutations-validate-outcome)
[](https://codeclimate.com/github/am-kantox/mutations-validate-outcome/coverage)
[](https://codeclimate.com/github/am-kantox/mutations-validate-outcome)Mixin for [`cypriss/mutations`](https://github.com/cypriss/mutations) allowing validation of outcome
using the same techniques as an input validation by original gem.## Installation
In your `Gemfile` make a following change:
```diff
- gem 'mutations'
+ gem 'mutations-validate-outcome'
```In your code:
```diff
- require 'mutations'
+ require 'mutations_validate_outcome'
```## Differences against [`cypriss/mutations`](https://github.com/cypriss/mutations)
* dropped a support for `1.9` and `j**`
* `CommandReturningHash`, `CommandReturningArray` are commands, that are supposed to return… well, you guessed
* `outcome_required` and `outcome_optional` filters are introduced for the new `CommandReturningHash` and `CommandReturningArray` classes
* `CommandReturningHash#validate_outcome` method is a sibling of `validate` for additional outcome validation on mutations, that are supposed to return a `Hash`
* `CommandReturningArray#validate_outcome` method is a sibling of `validate` for additional outcome validation on mutations, that are supposed to return an `Array` of similar `Hash`es; the checker for this command is the same as for `CommandReturningHash`, outcome consists of those elements passing validation, `errors` contains an additional field with failed items.#### Example
```ruby
class SimpleCommandReturningHash < Mutations::CommandReturningHash
required do
string :name, max_length: 10
string :email
endoptional do
integer :amount
endoutcome_required do
# outcome[:name] is to be shorter than 6 symbols
string :name, max_length: 5
# outcome[:email] is to be presented
string :email
endoutcome_optional do
integer :amount
enddef validate
add_error(:email, :invalid, 'Email must contain @') unless email && email.include?('@')
enddef execute
inputs
end# outcome[:name] must include 'John' substring
def validate_outcome(outcome)
add_error(:name, :invalid, 'Name must contain john') unless outcome[:name].include?('John')
end
end
``````ruby
class SimpleCommandReturningArray < Mutations::CommandReturningArray
required do
string :name, max_length: 10
string :email
endoutcome_required do
string :name, max_length: 5
string :email
enddef execute
[inputs.dup, {name: 'Aleksei', email: '[email protected]'}]
end
end
```## Changelog
#### `0.9.0` — `ActiveRecord::Relation` support
## License
The gem is produced by [Kantox LTD](https://kantox.com).
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).