Mutations :: Mixin for validating outcome
Mixin for 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:
- gem 'mutations'
+ gem 'mutations-validate-outcome'
In your code:
- require 'mutations'
+ require 'mutations_validate_outcome'
Differences against cypriss/mutations
- dropped a support for
1.9andj** CommandReturningHash,CommandReturningArrayare commands, that are supposed to return… well, you guessedoutcome_requiredandoutcome_optionalfilters are introduced for the newCommandReturningHashandCommandReturningArrayclassesCommandReturningHash#validate_outcomemethod is a sibling ofvalidatefor additional outcome validation on mutations, that are supposed to return aHashCommandReturningArray#validate_outcomemethod is a sibling ofvalidatefor additional outcome validation on mutations, that are supposed to return anArrayof similarHashes; the checker for this command is the same as forCommandReturningHash, outcome consists of those elements passing validation,errorscontains an additional field with failed items.
Example
class SimpleCommandReturningHash < Mutations::CommandReturningHash
required do
string :name, max_length: 10
string :email
end
optional do
integer :amount
end
outcome_required do
# outcome[:name] is to be shorter than 6 symbols
string :name, max_length: 5
# outcome[:email] is to be presented
string :email
end
outcome_optional do
integer :amount
end
def validate
add_error(:email, :invalid, 'Email must contain @') unless email && email.include?('@')
end
def 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
class SimpleCommandReturningArray < Mutations::CommandReturningArray
required do
string :name, max_length: 10
string :email
end
outcome_required do
string :name, max_length: 5
string :email
end
def execute
[inputs.dup, {name: 'Aleksei', email: '[email protected]'}]
end
end
License
The gem is produced by Kantox LTD. The gem is available as open source under the terms of the MIT License.

