mutations 0.8.3 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3eda7e54196daf6a06659a669f0864921ab376c54f218f4463f7c8b06d1dc075
4
- data.tar.gz: 0c6d22af4d18c4ae3041c600f2c61873cae3728514f49d48dfed41bfd6b57e63
3
+ metadata.gz: 60a9dcaccc2cded1876539ff36b62111b6bfdcec887494265fe4fa510c360ad5
4
+ data.tar.gz: 7a2bd2aacb4f8b9296e7f4a638fe42a1034ee8736a24a31d811580a87e4ecbe7
5
5
  SHA512:
6
- metadata.gz: eb82171717884e5ed61fa499c50fa9f5d3b4b1108c988619610b461e76f1a25c70078ad4aa3fff275e32e4b43f711cbe334d402b86e61a53d58778b4792c5986
7
- data.tar.gz: 9538cc3fbe474561fe72ff096c072128a03bb13237541e1e21338d72557abd2beeb75e85d1ff2bb72b5c9d9015bb89046b44d4330e6cd3ddde8eebb6f1e3ebb7
6
+ metadata.gz: 756a1dd30f3affd27ef111ebfbdf066e3e056767f6a08a7b69f8c39831f977bb9ee96b82e2f729cc02d4f6bffa2ea04f8ad8e405b9b6855f7a3bc06a20a4039a
7
+ data.tar.gz: b18adcdba20934dda3555cb3b72d90e23cd81612e152991338dd750f2d4bbd57ced91037b9900c4d42f8d6090cf4fe662ca0e91c3dba64692a5d0f0e3f46cae3
@@ -1,3 +1,9 @@
1
+ 0.9.0
2
+ -----------
3
+ - Retain `_id` suffix in error keys (#129, @khalilovcmd)
4
+ - Add a symbol filter: `symbol :color, in: %i(red blue green)` (#119, @estraph)
5
+ - Add `empty_is_nil` option to string filter (#135, @skylerwshaw)
6
+
1
7
  0.8.3
2
8
  -----------
3
9
  - Add `min_length` and `max_length` options to array filter (#128, @jwoertink)
data/README.md CHANGED
@@ -149,7 +149,7 @@ Here, we pass two hashes to CreateComment. Even if the params[:comment] hash has
149
149
  ```ruby
150
150
  required do
151
151
  string :name, max_length: 10
152
- string :state, in: %w(AL AK AR ... WY)
152
+ symbol :state, in: %i(AL AK AR ... WY)
153
153
  integer :age
154
154
  boolean :is_special, default: true
155
155
  model :account
@@ -20,6 +20,7 @@ require 'mutations/file_filter'
20
20
  require 'mutations/model_filter'
21
21
  require 'mutations/array_filter'
22
22
  require 'mutations/hash_filter'
23
+ require 'mutations/symbol_filter'
23
24
  require 'mutations/outcome'
24
25
  require 'mutations/command'
25
26
 
@@ -47,9 +47,20 @@ module Mutations
47
47
  # :index -- index of error if it's in an array
48
48
  def message(key, error_symbol, options = {})
49
49
  if options[:index]
50
- "#{(key || 'array').to_s.titleize}[#{options[:index]}] #{MESSAGES[error_symbol]}"
50
+ "#{titleize(key || 'array')}[#{options[:index]}] #{MESSAGES[error_symbol]}"
51
51
  else
52
- "#{key.to_s.titleize} #{MESSAGES[error_symbol]}"
52
+ "#{titleize(key)} #{MESSAGES[error_symbol]}"
53
+ end
54
+ end
55
+
56
+ def titleize(key)
57
+ key = key.to_s.downcase
58
+ if key == "id"
59
+ "ID"
60
+ elsif key.end_with?("_id")
61
+ "#{key.titleize} ID"
62
+ else
63
+ key.titleize
53
64
  end
54
65
  end
55
66
  end
@@ -4,6 +4,7 @@ module Mutations
4
4
  :strip => true, # true calls data.strip if data is a string
5
5
  :strict => false, # If false, then symbols, numbers, and booleans are converted to a string with to_s.
6
6
  :nils => false, # true allows an explicit nil to be valid. Overrides any other options
7
+ :empty_is_nil => false, # if true, treat empty string as if it were nil
7
8
  :empty => false, # false disallows "". true allows "" and overrides any other validations (b/c they couldn't be true if it's empty)
8
9
  :min_length => nil, # Can be a number like 5, meaning that 5 codepoints are required
9
10
  :max_length => nil, # Can be a number like 10, meaning that at most 10 codepoints are permitted
@@ -14,6 +15,9 @@ module Mutations
14
15
  }
15
16
 
16
17
  def filter(data)
18
+ if options[:empty_is_nil] && data == ""
19
+ data = nil
20
+ end
17
21
 
18
22
  # Handle nil case
19
23
  if data.nil?
@@ -0,0 +1,26 @@
1
+ module Mutations
2
+ class SymbolFilter < AdditionalFilter
3
+ @default_options = {
4
+ :nils => false, # true allows an explicit nil to be valid. Overrides any other options
5
+ :in => nil, # Can be an array like %i(red blue green)
6
+ }
7
+
8
+ def filter(data)
9
+ if data.nil?
10
+ return [nil, nil] if options[:nils]
11
+ return [nil, :nils]
12
+ end
13
+
14
+ case data
15
+ when Symbol # we're good!
16
+ when String then data = data.to_sym
17
+ else return [nil, :symbol]
18
+ end
19
+
20
+ # Ensure it matches `in`
21
+ return [data, :in] if options[:in] && !options[:in].include?(data)
22
+
23
+ [data, nil]
24
+ end
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  module Mutations
2
- VERSION = "0.8.3"
2
+ VERSION = "0.9.0"
3
3
  end
@@ -55,9 +55,21 @@ describe "Mutations - errors" do
55
55
  assert o.errors[:arr1][2].is_a?(Mutations::ErrorAtom)
56
56
  end
57
57
 
58
- it "titleizes keys" do
59
- atom = Mutations::ErrorAtom.new(:newsletter_subscription, :boolean)
60
- assert_equal "Newsletter Subscription isn't a boolean", atom.message
58
+ describe "error messages" do
59
+ it "titleizes keys" do
60
+ atom = Mutations::ErrorAtom.new(:newsletter_subscription, :boolean)
61
+ assert_equal "Newsletter Subscription isn't a boolean", atom.message
62
+ end
63
+
64
+ it "titleizes _id postfix as ID" do
65
+ atom = Mutations::ErrorAtom.new(:newsletter_subscription_id, :boolean)
66
+ assert_equal "Newsletter Subscription ID isn't a boolean", atom.message
67
+ end
68
+
69
+ it "titleizes id as ID" do
70
+ atom = Mutations::ErrorAtom.new(:id, :boolean)
71
+ assert_equal "ID isn't a boolean", atom.message
72
+ end
61
73
  end
62
74
 
63
75
  describe "Bunch o errors" do
@@ -59,6 +59,19 @@ describe "Mutations::StringFilter" do
59
59
  assert_equal nil, errors
60
60
  end
61
61
 
62
+ it "considers empty strings to be nil if empty_is_nil option is used" do
63
+ f = Mutations::StringFilter.new(:empty_is_nil => true)
64
+ _filtered, errors = f.filter("")
65
+ assert_equal :nils, errors
66
+ end
67
+
68
+ it "returns empty strings as nil if empty_is_nil option is used" do
69
+ f = Mutations::StringFilter.new(:empty_is_nil => true, :nils => true)
70
+ filtered, errors = f.filter("")
71
+ assert_equal nil, filtered
72
+ assert_equal nil, errors
73
+ end
74
+
62
75
  it "considers empty strings to be invalid" do
63
76
  sf = Mutations::StringFilter.new(:empty => false)
64
77
  filtered, errors = sf.filter("")
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Mutations::SymbolFilter" do
4
+
5
+ it "allows strings" do
6
+ sf = Mutations::SymbolFilter.new
7
+ filtered, errors = sf.filter("hello")
8
+ assert_equal :hello, filtered
9
+ assert_equal nil, errors
10
+ end
11
+
12
+ it "allows symbols" do
13
+ sf = Mutations::SymbolFilter.new
14
+ filtered, errors = sf.filter(:hello)
15
+ assert_equal :hello, filtered
16
+ assert_equal nil, errors
17
+ end
18
+
19
+ it "doesn't allow non-symbols" do
20
+ sf = Mutations::SymbolFilter.new
21
+ [["foo"], {:a => "1"}, Object.new].each do |thing|
22
+ _filtered, errors = sf.filter(thing)
23
+ assert_equal :symbol, errors
24
+ end
25
+ end
26
+
27
+ it "considers nil to be invalid" do
28
+ sf = Mutations::SymbolFilter.new(:nils => false)
29
+ filtered, errors = sf.filter(nil)
30
+ assert_equal nil, filtered
31
+ assert_equal :nils, errors
32
+ end
33
+
34
+ it "considers nil to be valid" do
35
+ sf = Mutations::SymbolFilter.new(:nils => true)
36
+ filtered, errors = sf.filter(nil)
37
+ assert_equal nil, filtered
38
+ assert_equal nil, errors
39
+ end
40
+
41
+ it "considers non-inclusion to be invalid" do
42
+ sf = Mutations::SymbolFilter.new(:in => [:red, :blue, :green])
43
+ filtered, errors = sf.filter(:orange)
44
+ assert_equal :orange, filtered
45
+ assert_equal :in, errors
46
+ end
47
+
48
+ it "considers inclusion to be valid" do
49
+ sf = Mutations::SymbolFilter.new(:in => [:red, :blue, :green])
50
+ filtered, errors = sf.filter(:red)
51
+ assert_equal :red, filtered
52
+ assert_equal nil, errors
53
+ end
54
+
55
+ 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.8.3
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Novak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-31 00:00:00.000000000 Z
11
+ date: 2019-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -84,6 +84,7 @@ files:
84
84
  - lib/mutations/model_filter.rb
85
85
  - lib/mutations/outcome.rb
86
86
  - lib/mutations/string_filter.rb
87
+ - lib/mutations/symbol_filter.rb
87
88
  - lib/mutations/time_filter.rb
88
89
  - lib/mutations/version.rb
89
90
  - mutations.gemspec
@@ -105,6 +106,7 @@ files:
105
106
  - spec/simple_command.rb
106
107
  - spec/spec_helper.rb
107
108
  - spec/string_filter_spec.rb
109
+ - spec/symbol_filter_spec.rb
108
110
  - spec/time_filter_spec.rb
109
111
  homepage: http://github.com/cypriss/mutations
110
112
  licenses:
@@ -125,8 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
127
  - !ruby/object:Gem::Version
126
128
  version: '0'
127
129
  requirements: []
128
- rubyforge_project:
129
- rubygems_version: 2.7.6
130
+ rubygems_version: 3.0.2
130
131
  signing_key:
131
132
  specification_version: 4
132
133
  summary: Compose your business logic into commands that sanitize and validate input.