mutations 0.8.1 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +5 -0
- data/lib/mutations/command.rb +1 -1
- data/lib/mutations/hash_filter.rb +8 -6
- data/lib/mutations/version.rb +1 -1
- data/spec/command_spec.rb +39 -3
- data/spec/hash_filter_spec.rb +12 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f9df957aca8cdb7ad065a71ade7fb1ccf48091e025e73f395822f53dab1de255
|
4
|
+
data.tar.gz: a6a3df2daca41bb6886bc76c75027f28d131c483d69120a10a5b108c79f549c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4ba37c6965c370d1d5cf39a28dcdb1a854e72ca0ad7ae91c5612bce03d2c35e09431eb21ffc4e46ec212d3806a89562517fea37eba2fdfce3683cfae0a0bb21
|
7
|
+
data.tar.gz: 5ed2f4404656a7646dbfb93a69f2bc7008c5182d44e2bb66e3e1ba9a259cd100c56e4849cb6ec5bd923d919db2aee25c2a8d7888862066f15e0b763a28bc67f2
|
data/CHANGELOG.md
CHANGED
data/lib/mutations/command.rb
CHANGED
@@ -56,7 +56,7 @@ module Mutations
|
|
56
56
|
# Instance methods
|
57
57
|
def initialize(*args)
|
58
58
|
@raw_inputs = args.inject({}.with_indifferent_access) do |h, arg|
|
59
|
-
raise ArgumentError.new("All arguments must be hashes") unless arg.
|
59
|
+
raise ArgumentError.new("All arguments must be hashes") unless arg.respond_to?(:to_hash)
|
60
60
|
h.merge!(arg)
|
61
61
|
end
|
62
62
|
|
@@ -79,11 +79,11 @@ module Mutations
|
|
79
79
|
end
|
80
80
|
|
81
81
|
# Ensure it's a hash
|
82
|
-
return [data, :hash] unless data.
|
82
|
+
return [data, :hash] unless data.respond_to?(:to_hash)
|
83
83
|
|
84
|
-
# We always want a hash with
|
84
|
+
# We always want a hash with indifferent access
|
85
85
|
unless data.is_a?(HashWithIndifferentAccess)
|
86
|
-
data = data.with_indifferent_access
|
86
|
+
data = data.to_hash.with_indifferent_access
|
87
87
|
end
|
88
88
|
|
89
89
|
errors = ErrorHash.new
|
@@ -113,16 +113,18 @@ module Mutations
|
|
113
113
|
elsif !is_required && sub_error == :nils && filterer.discard_nils?
|
114
114
|
data.delete(key)
|
115
115
|
else
|
116
|
-
|
116
|
+
error_key = filterer.options[:error_key] || key
|
117
|
+
sub_error = ErrorAtom.new(error_key, sub_error) if sub_error.is_a?(Symbol)
|
117
118
|
errors[key] = sub_error
|
118
119
|
end
|
119
120
|
end
|
120
|
-
|
121
|
+
|
121
122
|
if !data.has_key?(key)
|
122
123
|
if filterer.has_default?
|
123
124
|
filtered_data[key] = filterer.default
|
124
125
|
elsif is_required
|
125
|
-
|
126
|
+
error_key = filterer.options[:error_key] || key
|
127
|
+
errors[key] = ErrorAtom.new(error_key, :required)
|
126
128
|
end
|
127
129
|
end
|
128
130
|
end
|
data/lib/mutations/version.rb
CHANGED
data/spec/command_spec.rb
CHANGED
@@ -59,12 +59,12 @@ describe "Command" do
|
|
59
59
|
|
60
60
|
it "should execute custom validate method during run" do
|
61
61
|
outcome = SimpleCommand.run(:name => "JohnLong", :email => "xxxx")
|
62
|
-
|
62
|
+
|
63
63
|
assert !outcome.success?
|
64
64
|
assert_nil outcome.result
|
65
65
|
assert_equal :invalid, outcome.errors.symbolic[:email]
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
it "should execute custom validate method only if regular validations succeed" do
|
69
69
|
outcome = SimpleCommand.validate(:name => "JohnTooLong", :email => "xxxx")
|
70
70
|
|
@@ -87,7 +87,7 @@ describe "Command" do
|
|
87
87
|
assert_equal ({:name => "John", :email => "bob@jones.com", :amount => 5}).stringify_keys, outcome.result
|
88
88
|
end
|
89
89
|
|
90
|
-
it "shouldn't accept
|
90
|
+
it "shouldn't accept objects that are not hashes or directly mappable to hashes" do
|
91
91
|
assert_raises ArgumentError do
|
92
92
|
SimpleCommand.run(nil)
|
93
93
|
end
|
@@ -101,6 +101,19 @@ describe "Command" do
|
|
101
101
|
end
|
102
102
|
end
|
103
103
|
|
104
|
+
it 'should accept objects that are conceptually hashes' do
|
105
|
+
class CustomPersonHash
|
106
|
+
def to_hash
|
107
|
+
{ name: 'John', email: 'john@example.com' }
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
outcome = SimpleCommand.run(CustomPersonHash.new)
|
112
|
+
|
113
|
+
assert outcome.success?
|
114
|
+
assert_equal ({ name: "John", email: "john@example.com" }).stringify_keys, outcome.result
|
115
|
+
end
|
116
|
+
|
104
117
|
it "should accept nothing at all" do
|
105
118
|
SimpleCommand.run # make sure nothing is raised
|
106
119
|
end
|
@@ -168,6 +181,29 @@ describe "Command" do
|
|
168
181
|
end
|
169
182
|
end
|
170
183
|
|
184
|
+
describe "CustomErrorKeyCommand" do
|
185
|
+
class CustomErrorKeyCommand < Mutations::Command
|
186
|
+
required { string :name, error_key: :other_name }
|
187
|
+
optional { string :email, min_length: 4, error_key: :other_email }
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should return the optional error key in the error message if required" do
|
191
|
+
outcome = CustomErrorKeyCommand.run
|
192
|
+
|
193
|
+
assert !outcome.success?
|
194
|
+
assert_equal :required, outcome.errors.symbolic[:name]
|
195
|
+
assert_equal "Other Name is required", outcome.errors.message[:name]
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should return the optional error key in the error message if optional" do
|
199
|
+
outcome = CustomErrorKeyCommand.run(email: "foo")
|
200
|
+
|
201
|
+
assert !outcome.success?
|
202
|
+
assert_equal :min_length, outcome.errors.symbolic[:email]
|
203
|
+
assert_equal "Other Email is too short", outcome.errors.message[:email]
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
171
207
|
describe "NestingErrorfulCommand" do
|
172
208
|
class NestingErrorfulCommand < Mutations::Command
|
173
209
|
|
data/spec/hash_filter_spec.rb
CHANGED
@@ -20,6 +20,18 @@ describe "Mutations::HashFilter" do
|
|
20
20
|
assert_equal :hash, errors
|
21
21
|
end
|
22
22
|
|
23
|
+
it "allows non-hashes that implement #to_hash" do
|
24
|
+
input = Object.new
|
25
|
+
def input.to_hash
|
26
|
+
{:foo => "bar"}
|
27
|
+
end
|
28
|
+
hf = Mutations::HashFilter.new do
|
29
|
+
string :foo
|
30
|
+
end
|
31
|
+
_filtered, errors = hf.filter(input)
|
32
|
+
assert_equal nil, errors
|
33
|
+
end
|
34
|
+
|
23
35
|
it "allows wildcards in hashes" do
|
24
36
|
hf = Mutations::HashFilter.new do
|
25
37
|
string :*
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mutations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Novak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
126
126
|
version: '0'
|
127
127
|
requirements: []
|
128
128
|
rubyforge_project:
|
129
|
-
rubygems_version: 2.
|
129
|
+
rubygems_version: 2.7.6
|
130
130
|
signing_key:
|
131
131
|
specification_version: 4
|
132
132
|
summary: Compose your business logic into commands that sanitize and validate input.
|