mutations 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 60a9dcaccc2cded1876539ff36b62111b6bfdcec887494265fe4fa510c360ad5
4
- data.tar.gz: 7a2bd2aacb4f8b9296e7f4a638fe42a1034ee8736a24a31d811580a87e4ecbe7
3
+ metadata.gz: c4a28329a36aca21130a7a521f6833ad47ce35f31d0c1014c432eafbdad667b5
4
+ data.tar.gz: 5b085b2e98b416ff440b6842b556bad82b7c6d38e233828f0a84a192dcb2ee0a
5
5
  SHA512:
6
- metadata.gz: 756a1dd30f3affd27ef111ebfbdf066e3e056767f6a08a7b69f8c39831f977bb9ee96b82e2f729cc02d4f6bffa2ea04f8ad8e405b9b6855f7a3bc06a20a4039a
7
- data.tar.gz: b18adcdba20934dda3555cb3b72d90e23cd81612e152991338dd750f2d4bbd57ced91037b9900c4d42f8d6090cf4fe662ca0e91c3dba64692a5d0f0e3f46cae3
6
+ metadata.gz: 80ceb34a91da980b81eae6f47e26c967a03c7a11d49afa7da6646236c7a39ccee2936471abb12d66a43269231cbff889b03ca01ed0c5978fffeedf11e8b2fcfb
7
+ data.tar.gz: ad443a9ce7ed66004ddf09454315902a0a47d161c9a732a70d0f26e532d000ba86a51f83da1dbbef08dc96145a322cf26834f02f1a8b92a341b15f6f2f387fec
@@ -1,15 +1,14 @@
1
1
  language: ruby
2
- dist: trusty
3
- sudo: required
4
2
  before_install:
5
- - gem install bundler # the default bundler version on travis is very old and causes 1.9.3 build issues
3
+ - gem install bundler || gem install bundler -v 1.17.3 # the default bundler version on travis is very old and causes 1.9.3 build issues
6
4
  rvm:
7
5
  - 1.9.3
8
- - jruby-1.7.26
9
6
  - 2.0.0
10
7
  - 2.1.10
11
- - 2.2.6
12
- - 2.3.3
13
- - 2.4.0
14
- - jruby-9.1.7.0
15
- - rbx-3
8
+ - 2.2.10
9
+ - 2.3.8
10
+ - 2.4.9
11
+ - 2.5.7
12
+ - 2.6.5
13
+ - 2.7.0
14
+ - jruby-9.2.10.0
@@ -1,3 +1,9 @@
1
+ 0.9.1
2
+ -----------
3
+ - Treat blank strings as nil when `strip` and `empty_is_nil` options are set (#148, @saverio-kantox)
4
+ - Don't use full key in nested error messages (#149, @eugeneius)
5
+ - Don't remove unknown characters in string filter (#150, @eugeneius)
6
+
1
7
  0.9.0
2
8
  -----------
3
9
  - Retain `_id` suffix in error keys (#129, @khalilovcmd)
data/Gemfile CHANGED
@@ -1,8 +1,2 @@
1
1
  source 'http://rubygems.org'
2
2
  gemspec
3
-
4
- platforms :rbx do
5
- gem 'rubysl', '~> 2.0'
6
- gem 'psych'
7
- gem 'rubinius-developer_tools'
8
- end
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Mutations
2
2
 
3
- [![Build Status](https://travis-ci.org/cypriss/mutations.svg)](https://travis-ci.org/cypriss/mutations)
3
+ [![Build Status](https://travis-ci.org/cypriss/mutations.svg?branch=master)](https://travis-ci.org/cypriss/mutations)
4
4
  [![Code Climate](https://codeclimate.com/github/cypriss/mutations.svg)](https://codeclimate.com/github/cypriss/mutations)
5
5
 
6
6
  Compose your business logic into commands that sanitize and validate input. Write safe, reusable, and maintainable code for Ruby and Rails apps.
data/Rakefile CHANGED
@@ -2,7 +2,6 @@ require "bundler/gem_tasks"
2
2
  require "rake/testtask"
3
3
  Rake::TestTask.new(:test) do |test|
4
4
  test.libs << "spec"
5
- # test.warning = true # Wow that outputs a lot of shit
6
5
  test.pattern = "spec/**/*_spec.rb"
7
6
  end
8
7
 
@@ -1,8 +1,7 @@
1
1
  module Mutations
2
2
  class ArrayFilter < InputFilter
3
3
  def self.register_additional_filter(type_class, type_name)
4
- define_method(type_name) do | *args, &block |
5
- options = args[0] || {}
4
+ define_method(type_name) do |options = {}, &block|
6
5
  @element_filter = type_class.new(options, &block)
7
6
  end
8
7
  end
@@ -119,7 +119,7 @@ module Mutations
119
119
  inner = path.inject(errs) do |cur_errors,part|
120
120
  cur_errors[part.to_sym] ||= ErrorHash.new
121
121
  end
122
- inner[last] = ErrorAtom.new(key, kind, :message => message)
122
+ inner[last] = ErrorAtom.new(last.to_sym, kind, :message => message)
123
123
  end
124
124
  end
125
125
 
@@ -9,10 +9,6 @@ module Mutations
9
9
  }
10
10
 
11
11
  def filter(data)
12
- if options[:empty_is_nil] && data == ""
13
- data = nil
14
- end
15
-
16
12
  # Handle nil case
17
13
  if data.nil?
18
14
  return [nil, nil] if options[:nils]
@@ -20,7 +16,13 @@ module Mutations
20
16
  end
21
17
 
22
18
  # Now check if it's empty:
23
- return [data, :empty] if "" == data
19
+ if data == ""
20
+ if options[:empty_is_nil]
21
+ return [nil, (:nils unless options[:nils])]
22
+ else
23
+ return [data, :empty]
24
+ end
25
+ end
24
26
 
25
27
  if data.is_a?(Date) # Date and DateTime
26
28
  actual_date = data
@@ -1,10 +1,7 @@
1
1
  module Mutations
2
2
  class HashFilter < InputFilter
3
3
  def self.register_additional_filter(type_class, type_name)
4
- define_method(type_name) do | *args, &block |
5
- name = args[0]
6
- options = args[1] || {}
7
-
4
+ define_method(type_name) do |name, options = {}, &block|
8
5
  @current_inputs[name.to_sym] = type_class.new(options, &block)
9
6
  end
10
7
  end
@@ -9,11 +9,6 @@ module Mutations
9
9
  }
10
10
 
11
11
  def filter(data)
12
-
13
- if options[:empty_is_nil] && data == ""
14
- data = nil
15
- end
16
-
17
12
  # Handle nil case
18
13
  if data.nil?
19
14
  return [nil, nil] if options[:nils]
@@ -21,7 +16,13 @@ module Mutations
21
16
  end
22
17
 
23
18
  # Now check if it's empty:
24
- return [data, :empty] if data == ""
19
+ if data == ""
20
+ if options[:empty_is_nil]
21
+ return [nil, (:nils unless options[:nils])]
22
+ else
23
+ return [data, :empty]
24
+ end
25
+ end
25
26
 
26
27
  # Ensure it's the correct data type (Integer)
27
28
  if !data.is_a?(Integer)
@@ -11,14 +11,10 @@ module Mutations
11
11
  :matches => nil, # Can be a regexp
12
12
  :in => nil, # Can be an array like %w(red blue green)
13
13
  :discard_empty => false, # If the param is optional, discard_empty: true drops empty fields.
14
- :allow_control_characters => false # false removes unprintable characters from the string
14
+ :allow_control_characters => false # false removes control characters from the string
15
15
  }
16
16
 
17
17
  def filter(data)
18
- if options[:empty_is_nil] && data == ""
19
- data = nil
20
- end
21
-
22
18
  # Handle nil case
23
19
  if data.nil?
24
20
  return [nil, nil] if options[:nils]
@@ -31,15 +27,17 @@ module Mutations
31
27
  # Now ensure it's a string:
32
28
  return [data, :string] unless data.is_a?(String)
33
29
 
34
- # At this point, data is a string. Now remove unprintable characters from the string:
35
- data = data.gsub(/[^[:print:]\t\r\n]+/, ' ') unless options[:allow_control_characters]
30
+ # At this point, data is a string. Now remove control characters from the string:
31
+ data = data.gsub(/((?=[[:cntrl:]])[^\t\r\n])+/, ' ') unless options[:allow_control_characters]
36
32
 
37
33
  # Transform it using strip:
38
34
  data = data.strip if options[:strip]
39
35
 
40
36
  # Now check if it's blank:
41
37
  if data == ""
42
- if options[:empty]
38
+ if options[:empty_is_nil]
39
+ return [nil, (:nils unless options[:nils])]
40
+ elsif options[:empty]
43
41
  return [data, nil]
44
42
  else
45
43
  return [data, :empty]
@@ -1,3 +1,3 @@
1
1
  module Mutations
2
- VERSION = "0.9.0"
2
+ VERSION = "0.9.1"
3
3
  end
@@ -155,7 +155,7 @@ describe "Mutations::ArrayFilter" do
155
155
 
156
156
  assert_equal nil, errors[0]
157
157
  assert_equal nil, errors[1]
158
- assert_equal ({"bar"=>:required}), errors[2].symbolic
158
+ assert_equal({"bar"=>:required}, errors[2].symbolic)
159
159
  end
160
160
 
161
161
  it "lets you pass arrays of arrays" do
@@ -224,7 +224,7 @@ describe "Mutations::ArrayFilter" do
224
224
  was = Mutations.cache_constants?
225
225
  Mutations.cache_constants = true
226
226
 
227
- f = Mutations::ArrayFilter.new(:simple_models, class: "SimpleModel")
227
+ f = Mutations::ArrayFilter.new(:simple_models, :class => "SimpleModel")
228
228
 
229
229
  m = SimpleModel.new
230
230
  filtered, errors = f.filter([m])
@@ -246,7 +246,7 @@ describe "Mutations::ArrayFilter" do
246
246
  was = Mutations.cache_constants?
247
247
  Mutations.cache_constants = false
248
248
 
249
- f = Mutations::ArrayFilter.new(:simple_models, class: "SimpleModel")
249
+ f = Mutations::ArrayFilter.new(:simple_models, :class => "SimpleModel")
250
250
 
251
251
  m = SimpleModel.new
252
252
  filtered, errors = f.filter([m])
@@ -8,7 +8,7 @@ describe "Command" do
8
8
  outcome = SimpleCommand.run(:name => "John", :email => "john@gmail.com", :amount => 5)
9
9
 
10
10
  assert outcome.success?
11
- assert_equal ({:name => "John", :email => "john@gmail.com", :amount => 5}).stringify_keys, outcome.result
11
+ assert_equal({:name => "John", :email => "john@gmail.com", :amount => 5}.stringify_keys, outcome.result)
12
12
  assert_equal nil, outcome.errors
13
13
  end
14
14
 
@@ -16,7 +16,7 @@ describe "Command" do
16
16
  outcome = SimpleCommand.run(:name => "John", :email => "john@gmail.com", :amount => 5, :buggers => true)
17
17
 
18
18
  assert outcome.success?
19
- assert_equal ({:name => "John", :email => "john@gmail.com", :amount => 5}).stringify_keys, outcome.result
19
+ assert_equal({:name => "John", :email => "john@gmail.com", :amount => 5}.stringify_keys, outcome.result)
20
20
  assert_equal nil, outcome.errors
21
21
  end
22
22
 
@@ -29,7 +29,7 @@ describe "Command" do
29
29
 
30
30
  it "shouldn't throw an exception with run!" do
31
31
  result = SimpleCommand.run!(:name => "John", :email => "john@gmail.com", :amount => 5)
32
- assert_equal ({:name => "John", :email => "john@gmail.com", :amount => 5}).stringify_keys, result
32
+ assert_equal({:name => "John", :email => "john@gmail.com", :amount => 5}.stringify_keys, result)
33
33
  end
34
34
 
35
35
  it "should throw an exception with run!" do
@@ -77,14 +77,14 @@ describe "Command" do
77
77
  outcome = SimpleCommand.run({:name => "John", :email => "john@gmail.com"}, {:email => "bob@jones.com", :amount => 5})
78
78
 
79
79
  assert outcome.success?
80
- assert_equal ({:name => "John", :email => "bob@jones.com", :amount => 5}).stringify_keys, outcome.result
80
+ assert_equal({:name => "John", :email => "bob@jones.com", :amount => 5}.stringify_keys, outcome.result)
81
81
  end
82
82
 
83
83
  it "should merge hashes indifferently" do
84
84
  outcome = SimpleCommand.run({:name => "John", :email => "john@gmail.com"}, {"email" => "bob@jones.com", "amount" => 5})
85
85
 
86
86
  assert outcome.success?
87
- assert_equal ({:name => "John", :email => "bob@jones.com", :amount => 5}).stringify_keys, outcome.result
87
+ assert_equal({:name => "John", :email => "bob@jones.com", :amount => 5}.stringify_keys, outcome.result)
88
88
  end
89
89
 
90
90
  it "shouldn't accept objects that are not hashes or directly mappable to hashes" do
@@ -104,14 +104,14 @@ describe "Command" do
104
104
  it 'should accept objects that are conceptually hashes' do
105
105
  class CustomPersonHash
106
106
  def to_hash
107
- { name: 'John', email: 'john@example.com' }
107
+ { :name => 'John', :email => 'john@example.com' }
108
108
  end
109
109
  end
110
110
 
111
111
  outcome = SimpleCommand.run(CustomPersonHash.new)
112
112
 
113
113
  assert outcome.success?
114
- assert_equal ({ name: "John", email: "john@example.com" }).stringify_keys, outcome.result
114
+ assert_equal({ :name => "John", :email => "john@example.com" }.stringify_keys, outcome.result)
115
115
  end
116
116
 
117
117
  it "should accept nothing at all" do
@@ -120,7 +120,7 @@ describe "Command" do
120
120
 
121
121
  it "should return the filtered inputs in the outcome" do
122
122
  outcome = SimpleCommand.run(:name => " John ", :email => "john@gmail.com", :amount => "5")
123
- assert_equal ({:name => "John", :email => "john@gmail.com", :amount => 5}).stringify_keys, outcome.inputs
123
+ assert_equal({:name => "John", :email => "john@gmail.com", :amount => 5}.stringify_keys, outcome.inputs)
124
124
  end
125
125
  end
126
126
 
@@ -137,7 +137,7 @@ describe "Command" do
137
137
 
138
138
  it "should define getter methods on params" do
139
139
  mutation = EigenCommand.run(:name => "John", :email => "john@gmail.com")
140
- assert_equal ({:name => "John", :email => "john@gmail.com"}), mutation.result
140
+ assert_equal({:name => "John", :email => "john@gmail.com"}, mutation.result)
141
141
  end
142
142
  end
143
143
 
@@ -155,7 +155,7 @@ describe "Command" do
155
155
 
156
156
  it "should define setter methods on params" do
157
157
  mutation = MutatatedCommand.run(:name => "John", :email => "john@gmail.com")
158
- assert_equal ({:name => "bob", :email => "bob@jones.com"}), mutation.result
158
+ assert_equal({:name => "bob", :email => "bob@jones.com"}, mutation.result)
159
159
  end
160
160
  end
161
161
 
@@ -183,8 +183,8 @@ describe "Command" do
183
183
 
184
184
  describe "CustomErrorKeyCommand" do
185
185
  class CustomErrorKeyCommand < Mutations::Command
186
- required { string :name, error_key: :other_name }
187
- optional { string :email, min_length: 4, error_key: :other_email }
186
+ required { string :name, :error_key => :other_name }
187
+ optional { string :email, :min_length => 4, :error_key => :other_email }
188
188
  end
189
189
 
190
190
  it "should return the optional error key in the error message if required" do
@@ -196,7 +196,7 @@ describe "Command" do
196
196
  end
197
197
 
198
198
  it "should return the optional error key in the error message if optional" do
199
- outcome = CustomErrorKeyCommand.run(email: "foo")
199
+ outcome = CustomErrorKeyCommand.run(:email => "foo")
200
200
 
201
201
  assert !outcome.success?
202
202
  assert_equal :min_length, outcome.errors.symbolic[:email]
@@ -223,6 +223,7 @@ describe "Command" do
223
223
  assert !outcome.success?
224
224
  assert_nil outcome.result
225
225
  assert_equal :is_a_bob, outcome.errors[:people].symbolic[:bob]
226
+ assert_equal "Bob is invalid", outcome.errors[:people].message[:bob]
226
227
  end
227
228
  end
228
229
 
@@ -21,12 +21,8 @@ describe "Mutations::DateFilter" do
21
21
  time = Time.now
22
22
  f = Mutations::DateFilter.new
23
23
  filtered, errors = f.filter(time)
24
- if time.respond_to?(:to_date) # 1.8.7 doesn't support to_date
25
- assert_equal time.to_date, filtered
26
- assert_equal nil, errors
27
- else
28
- assert_equal :date, errors
29
- end
24
+ assert_equal time.to_date, filtered
25
+ assert_equal nil, errors
30
26
  end
31
27
 
32
28
  it "checks if the given date is after a certain date" do
@@ -16,13 +16,13 @@ describe 'Mutations - defaults' do
16
16
  it "should have a default if no value is passed" do
17
17
  outcome = DefaultCommand.run
18
18
  assert_equal true, outcome.success?
19
- assert_equal ({"name" => "Bob Jones"}), outcome.result
19
+ assert_equal({"name" => "Bob Jones"}, outcome.result)
20
20
  end
21
21
 
22
22
  it "should have the passed value if a value is passed" do
23
23
  outcome = DefaultCommand.run(:name => "Fred")
24
24
  assert_equal true, outcome.success?
25
- assert_equal ({"name" => "Fred"}), outcome.result
25
+ assert_equal({"name" => "Fred"}, outcome.result)
26
26
  end
27
27
 
28
28
  it "should be an error if nil is passed on a required field with a default" do
@@ -26,33 +26,33 @@ describe "Mutations - errors" do
26
26
  o = GivesErrors.run(:hash1 => 1, :arr1 => "bob")
27
27
 
28
28
  assert !o.success?
29
- assert o.errors.is_a?(Mutations::ErrorHash)
30
- assert o.errors[:str1].is_a?(Mutations::ErrorAtom)
31
- assert o.errors[:str2].is_a?(Mutations::ErrorAtom)
29
+ assert_kind_of Mutations::ErrorHash, o.errors
30
+ assert_kind_of Mutations::ErrorAtom, o.errors[:str1]
31
+ assert_kind_of Mutations::ErrorAtom, o.errors[:str2]
32
32
  assert_nil o.errors[:int1]
33
- assert o.errors[:hash1].is_a?(Mutations::ErrorAtom)
34
- assert o.errors[:arr1].is_a?(Mutations::ErrorAtom)
33
+ assert_kind_of Mutations::ErrorAtom, o.errors[:hash1]
34
+ assert_kind_of Mutations::ErrorAtom, o.errors[:arr1]
35
35
  end
36
36
 
37
37
  it "returns an ErrorHash for nested hashes" do
38
38
  o = GivesErrors.run(:hash1 => {:bool1 => "poop"})
39
39
 
40
40
  assert !o.success?
41
- assert o.errors.is_a?(Mutations::ErrorHash)
42
- assert o.errors[:hash1].is_a?(Mutations::ErrorHash)
43
- assert o.errors[:hash1][:bool1].is_a?(Mutations::ErrorAtom)
44
- assert o.errors[:hash1][:bool2].is_a?(Mutations::ErrorAtom)
41
+ assert_kind_of Mutations::ErrorHash, o.errors
42
+ assert_kind_of Mutations::ErrorHash, o.errors[:hash1]
43
+ assert_kind_of Mutations::ErrorAtom, o.errors[:hash1][:bool1]
44
+ assert_kind_of Mutations::ErrorAtom, o.errors[:hash1][:bool2]
45
45
  end
46
46
 
47
47
  it "returns an ErrorArray for errors in arrays" do
48
48
  o = GivesErrors.run(:str1 => "a", :str2 => "opt1", :arr1 => ["bob", 1, "sally"])
49
49
 
50
50
  assert !o.success?
51
- assert o.errors.is_a?(Mutations::ErrorHash)
52
- assert o.errors[:arr1].is_a?(Mutations::ErrorArray)
53
- assert o.errors[:arr1][0].is_a?(Mutations::ErrorAtom)
51
+ assert_kind_of Mutations::ErrorHash, o.errors
52
+ assert_kind_of Mutations::ErrorArray, o.errors[:arr1]
53
+ assert_kind_of Mutations::ErrorAtom, o.errors[:arr1][0]
54
54
  assert_nil o.errors[:arr1][1]
55
- assert o.errors[:arr1][2].is_a?(Mutations::ErrorAtom)
55
+ assert_kind_of Mutations::ErrorAtom, o.errors[:arr1][2]
56
56
  end
57
57
 
58
58
  describe "error messages" do
@@ -97,7 +97,7 @@ describe "Mutations - errors" do
97
97
  expected = ["Str1 can't be blank", "Str2 isn't an option", "Int1 isn't an integer", "Bool1 isn't a boolean", "Bool2 is required", "Arr1[0] isn't an integer", "Arr1[2] isn't an integer"]
98
98
 
99
99
  assert_equal expected.size, @outcome.errors.message_list.size
100
- expected.each { |e| assert @outcome.errors.message_list.include?(e) }
100
+ expected.each { |e| assert_includes @outcome.errors.message_list, e }
101
101
  end
102
102
  end
103
103
 
@@ -8,16 +8,12 @@ describe "Mutations::FileFilter" do
8
8
  attr_accessor :content_type, :original_filename
9
9
  end
10
10
 
11
- # NOTE: Ruby 1.8.7 doesn't have file.size. Mutations need file to respond to size.
12
- # Therefore, in 1.8.7 File objects are invalid files.
13
- if File.new("README.md").respond_to?(:size)
14
- it "allows files - file class" do
15
- file = File.new("README.md")
16
- f = Mutations::FileFilter.new
17
- filtered, errors = f.filter(file)
18
- assert_equal file, filtered
19
- assert_equal nil, errors
20
- end
11
+ it "allows files - file class" do
12
+ file = File.new("README.md")
13
+ f = Mutations::FileFilter.new
14
+ filtered, errors = f.filter(file)
15
+ assert_equal file, filtered
16
+ assert_equal nil, errors
21
17
  end
22
18
 
23
19
  it "allows files - stringio class" do
@@ -32,7 +28,7 @@ describe "Mutations::FileFilter" do
32
28
  file = Tempfile.new("bob")
33
29
  f = Mutations::FileFilter.new
34
30
  filtered, errors = f.filter(file)
35
- assert filtered.is_a?(Tempfile) # NOTE: 1.9.3 and 1.8.7 treat == and eql? differently
31
+ assert_equal file, filtered
36
32
  assert_equal nil, errors
37
33
  end
38
34
 
@@ -8,7 +8,7 @@ describe "Mutations::HashFilter" do
8
8
  string :foo
9
9
  end
10
10
  filtered, errors = hf.filter(:foo => "bar")
11
- assert_equal ({"foo" => "bar"}), filtered
11
+ assert_equal({"foo" => "bar"}, filtered)
12
12
  assert_equal nil, errors
13
13
  end
14
14
 
@@ -37,7 +37,7 @@ describe "Mutations::HashFilter" do
37
37
  string :*
38
38
  end
39
39
  filtered, errors = hf.filter(:foo => "bar", :baz => "ban")
40
- assert_equal ({"foo" => "bar", "baz" => "ban"}), filtered
40
+ assert_equal({"foo" => "bar", "baz" => "ban"}, filtered)
41
41
  assert_equal nil, errors
42
42
  end
43
43
 
@@ -46,7 +46,7 @@ describe "Mutations::HashFilter" do
46
46
  float :foo
47
47
  end
48
48
  filtered, errors = hf.filter(:foo => 3.14)
49
- assert_equal ({"foo" => 3.14}), filtered
49
+ assert_equal({"foo" => 3.14}, filtered)
50
50
  assert_equal nil, errors
51
51
  end
52
52
 
@@ -55,7 +55,7 @@ describe "Mutations::HashFilter" do
55
55
  duck :foo, :methods => [:length]
56
56
  end
57
57
  filtered, errors = hf.filter(:foo => "123")
58
- assert_equal ({"foo" => "123"}), filtered
58
+ assert_equal({"foo" => "123"}, filtered)
59
59
  assert_equal nil, errors
60
60
  end
61
61
 
@@ -74,7 +74,7 @@ describe "Mutations::HashFilter" do
74
74
  file :foo
75
75
  end
76
76
  filtered, errors = hf.filter(:foo => sio)
77
- assert_equal ({"foo" => sio}), filtered
77
+ assert_equal({"foo" => sio}, filtered)
78
78
  assert_equal nil, errors
79
79
  end
80
80
 
@@ -83,7 +83,7 @@ describe "Mutations::HashFilter" do
83
83
  string :*
84
84
  end
85
85
  _filtered, errors = hf.filter(:foo => [])
86
- assert_equal ({"foo" => :string}), errors.symbolic
86
+ assert_equal({"foo" => :string}, errors.symbolic)
87
87
  end
88
88
 
89
89
  it "allows a mix of specific keys and then wildcards" do
@@ -92,7 +92,7 @@ describe "Mutations::HashFilter" do
92
92
  integer :*
93
93
  end
94
94
  filtered, errors = hf.filter(:foo => "bar", :baz => "4")
95
- assert_equal ({"foo" => "bar", "baz" => 4}), filtered
95
+ assert_equal({"foo" => "bar", "baz" => 4}, filtered)
96
96
  assert_equal nil, errors
97
97
  end
98
98
 
@@ -102,7 +102,7 @@ describe "Mutations::HashFilter" do
102
102
  integer :*
103
103
  end
104
104
  _filtered, errors = hf.filter(:foo => "bar", :baz => "poopin")
105
- assert_equal ({"baz" => :integer}), errors.symbolic
105
+ assert_equal({"baz" => :integer}, errors.symbolic)
106
106
  end
107
107
 
108
108
  describe "optional params and nils" do
@@ -117,7 +117,7 @@ describe "Mutations::HashFilter" do
117
117
  end
118
118
 
119
119
  filtered, errors = hf.filter(:foo => "bar")
120
- assert_equal ({"foo" => "bar"}), filtered
120
+ assert_equal({"foo" => "bar"}, filtered)
121
121
  assert_equal nil, errors
122
122
  end
123
123
 
@@ -132,7 +132,7 @@ describe "Mutations::HashFilter" do
132
132
  end
133
133
 
134
134
  filtered, errors = hf.filter(:foo => "bar", :bar => nil)
135
- assert_equal ({"foo" => "bar"}), filtered
135
+ assert_equal({"foo" => "bar"}, filtered)
136
136
  assert_equal nil, errors
137
137
  end
138
138
 
@@ -147,7 +147,7 @@ describe "Mutations::HashFilter" do
147
147
  end
148
148
 
149
149
  filtered, errors = hf.filter(:foo => "bar", :bar => nil)
150
- assert_equal ({"foo" => "bar", "bar" => nil}), filtered
150
+ assert_equal({"foo" => "bar", "bar" => nil}, filtered)
151
151
  assert_equal nil, errors
152
152
  end
153
153
  end
@@ -164,7 +164,7 @@ describe "Mutations::HashFilter" do
164
164
  end
165
165
 
166
166
  filtered, errors = hf.filter(:foo => "bar", :bar => "")
167
- assert_equal ({"foo" => "bar"}), filtered
167
+ assert_equal({"foo" => "bar"}, filtered)
168
168
  assert_equal nil, errors
169
169
  end
170
170
 
@@ -179,7 +179,7 @@ describe "Mutations::HashFilter" do
179
179
  end
180
180
 
181
181
  filtered, errors = hf.filter(:foo => "bar", :bar => " ")
182
- assert_equal ({"foo" => "bar"}), filtered
182
+ assert_equal({"foo" => "bar"}, filtered)
183
183
  assert_equal nil, errors
184
184
  end
185
185
 
@@ -194,7 +194,7 @@ describe "Mutations::HashFilter" do
194
194
  end
195
195
 
196
196
  filtered, errors = hf.filter(:foo => "bar", :bar => " ")
197
- assert_equal ({"foo" => "bar", "bar" => " "}), filtered
197
+ assert_equal({"foo" => "bar", "bar" => " "}, filtered)
198
198
  assert_equal nil, errors
199
199
  end
200
200
 
@@ -209,7 +209,7 @@ describe "Mutations::HashFilter" do
209
209
  end
210
210
 
211
211
  _filtered, errors = hf.filter(:foo => "bar", :bar => "")
212
- assert_equal ({"bar" => :empty}), errors.symbolic
212
+ assert_equal({"bar" => :empty}, errors.symbolic)
213
213
  end
214
214
 
215
215
  it "bar is optional -- discards empty -- now with wildcards" do
@@ -223,7 +223,7 @@ describe "Mutations::HashFilter" do
223
223
  end
224
224
 
225
225
  filtered, errors = hf.filter(:foo => "bar", :bar => "", :baz => "\t")
226
- assert_equal ({"foo" => "bar"}), filtered
226
+ assert_equal({"foo" => "bar"}, filtered)
227
227
  assert_equal nil, errors
228
228
  end
229
229
  end
@@ -240,7 +240,7 @@ describe "Mutations::HashFilter" do
240
240
  end
241
241
 
242
242
  filtered, errors = hf.filter(:foo => "bar", :bar => "baz")
243
- assert_equal ({"foo" => "bar"}), filtered
243
+ assert_equal({"foo" => "bar"}, filtered)
244
244
  assert_equal nil, errors
245
245
  end
246
246
 
@@ -255,7 +255,7 @@ describe "Mutations::HashFilter" do
255
255
  end
256
256
 
257
257
  filtered, errors = hf.filter(:foo => "bar", :bar => "baz", :wat => 1)
258
- assert_equal ({"foo" => "bar", "wat" => 1}), filtered
258
+ assert_equal({"foo" => "bar", "wat" => 1}, filtered)
259
259
  assert_equal nil, errors
260
260
  end
261
261
 
@@ -268,7 +268,7 @@ describe "Mutations::HashFilter" do
268
268
  end
269
269
 
270
270
  _filtered, errors = hf.filter(:foo => "bar")
271
- assert_equal ({"foo" => :integer}), errors.symbolic
271
+ assert_equal({"foo" => :integer}, errors.symbolic)
272
272
  end
273
273
  end
274
274
 
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe 'Mutations' do
4
4
 
5
5
  it 'should have a version' do
6
- assert Mutations::VERSION.is_a?(String)
6
+ assert_kind_of String, Mutations::VERSION
7
7
  end
8
8
 
9
9
  end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe "Mutations::StringFilter" do
@@ -16,7 +18,7 @@ describe "Mutations::StringFilter" do
16
18
  assert_equal nil, errors
17
19
  end
18
20
 
19
- it "allows fixnums" do
21
+ it "allows numbers" do
20
22
  sf = Mutations::StringFilter.new
21
23
  filtered, errors = sf.filter(1)
22
24
  assert_equal "1", filtered
@@ -93,7 +95,21 @@ describe "Mutations::StringFilter" do
93
95
  assert_equal :empty, errors
94
96
  end
95
97
 
96
- it "considers strings that contain only unprintable characters to be invalid" do
98
+ it "considers stripped strings that are blank to be nil if empty_is_nil option is used" do
99
+ sf = Mutations::StringFilter.new(:strip => true, :empty_is_nil => true, :nils => true)
100
+ filtered, errors = sf.filter(" ")
101
+ assert_equal nil, filtered
102
+ assert_equal nil, errors
103
+ end
104
+
105
+ it "considers stripped strings that are blank to be invalid if empty_is_nil option is used" do
106
+ sf = Mutations::StringFilter.new(:strip => true, :empty_is_nil => true)
107
+ filtered, errors = sf.filter(" ")
108
+ assert_equal nil, filtered
109
+ assert_equal :nils, errors
110
+ end
111
+
112
+ it "considers strings that contain only control characters to be invalid" do
97
113
  sf = Mutations::StringFilter.new(:empty => false)
98
114
  filtered, errors = sf.filter("\u0000\u0000")
99
115
  assert_equal "", filtered
@@ -172,7 +188,7 @@ describe "Mutations::StringFilter" do
172
188
 
173
189
  it "converts bigdecimals to strings" do
174
190
  sf = Mutations::StringFilter.new(:strict => false)
175
- filtered, errors = sf.filter(BigDecimal.new("0.0001"))
191
+ filtered, errors = sf.filter(BigDecimal("0.0001"))
176
192
  assert_equal("0.1E-3", filtered.upcase)
177
193
  assert_equal nil, errors
178
194
  end
@@ -207,7 +223,7 @@ describe "Mutations::StringFilter" do
207
223
 
208
224
  it "disallows bigdecimals" do
209
225
  sf = Mutations::StringFilter.new(:strict => true)
210
- big_decimal = BigDecimal.new("0.0001")
226
+ big_decimal = BigDecimal("0.0001")
211
227
  filtered, errors = sf.filter(big_decimal)
212
228
  assert_equal big_decimal, filtered
213
229
  assert_equal :string, errors
@@ -227,14 +243,14 @@ describe "Mutations::StringFilter" do
227
243
  assert_equal :string, errors
228
244
  end
229
245
 
230
- it "removes unprintable characters" do
246
+ it "removes control characters" do
231
247
  sf = Mutations::StringFilter.new(:allow_control_characters => false)
232
248
  filtered, errors = sf.filter("Hello\u0000\u0000World!")
233
249
  assert_equal "Hello World!", filtered
234
250
  assert_equal nil, errors
235
251
  end
236
252
 
237
- it "doesn't remove unprintable characters" do
253
+ it "doesn't remove control characters" do
238
254
  sf = Mutations::StringFilter.new(:allow_control_characters => true)
239
255
  filtered, errors = sf.filter("Hello\u0000\u0000World!")
240
256
  assert_equal "Hello\u0000\u0000World!", filtered
@@ -248,4 +264,11 @@ describe "Mutations::StringFilter" do
248
264
  assert_equal nil, errors
249
265
  end
250
266
 
267
+ it "doesn't remove emoji" do
268
+ sf = Mutations::StringFilter.new(:allow_control_characters => false)
269
+ filtered, errors = sf.filter("😂🙂🙃🤣🤩🥰🥱")
270
+ assert_equal "😂🙂🙃🤣🤩🥰🥱", filtered
271
+ assert_equal nil, errors
272
+ end
273
+
251
274
  end
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.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Novak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-05 00:00:00.000000000 Z
11
+ date: 2020-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -127,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
127
  - !ruby/object:Gem::Version
128
128
  version: '0'
129
129
  requirements: []
130
- rubygems_version: 3.0.2
130
+ rubygems_version: 3.1.2
131
131
  signing_key:
132
132
  specification_version: 4
133
133
  summary: Compose your business logic into commands that sanitize and validate input.