paramore 3.9.1 → 3.9.5

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: 443c4fd7a430d181af1f221f8e2f8d162cde504112ccafa80e7d672445add93d
4
- data.tar.gz: 8557e290f05dcc0654a5bcbb55b2832d41ea0d98ac171bd3e29939a1d9312e4f
3
+ metadata.gz: dc2e88fe6b6a88a0447d4625d2d6111a1741a6fb73d0e1564defb60ab59a7ceb
4
+ data.tar.gz: b6698578f905466beede01ff25d16d7e22455e61755de121cf07c869f643f371
5
5
  SHA512:
6
- metadata.gz: d86cf6d7f77328271d2bc82beebf700ccb70c7303b77654342a20e7087a4d22da5779bdfcae8e64624b1f04316f0f15472385234a4668bcfdbf7f5ba88760379
7
- data.tar.gz: b8f97f9e0dde2c4f646876fb4ac8fea06301fcbe882e4244a088439091464161d873791923dd2415f67a1169c2c1c2851dd5be21323d97f8aab3700bd6e460c8
6
+ metadata.gz: 4e54fdb3b3082165128001c95a58c49d9a862d82486448e9cb5d163a853ba92ea62b284d01dd349f4f24a3da843c456944a38a155072e2f7cdc8592a8c90c5b2
7
+ data.tar.gz: 4df4f72270c8556e3d24d6b41d63244a91b6409fe1c232d562eb63970037e85f58ea83c16ba86458aacf77f303580bfba52b4eedf3311b0caed947ba9e3e2942
@@ -15,81 +15,92 @@ module Paramore
15
15
  end
16
16
 
17
17
  def run
18
- cast(root_field, data, 'data')
18
+ cast(root_field, data, nil)
19
19
  end
20
20
 
21
- def cast(field, value, name = nil)
21
+ def cast(field, value, key, namespace = nil)
22
+ full_name = [namespace, key].select(&:present?).join('.')
23
+
22
24
  if value.nil?
23
- if field.nullable? || field.default?
24
- return field.default
25
- else
26
- raise Paramore::NilParameter, name
27
- end
25
+ return field.default if field.nullable? || field.default?
26
+
27
+ raise Paramore::NilParameter, full_name
28
28
  elsif value == ''
29
- if field.allow_empty?
30
- return typecast_value(field.type, '', name)
31
- elsif field.default?
32
- return field.default
33
- elsif field.nullable?
34
- return
35
- else
36
- raise Paramore::NilParameter, name
37
- end
29
+ return typecast_value(field.type, '', full_name) if field.allow_empty?
30
+ return field.default if field.default?
31
+ return if field.nullable?
32
+
33
+ raise Paramore::NilParameter, full_name
38
34
  end
39
35
 
40
36
  case field.type
41
37
  when Hash
42
- typecast_hash(field, value || {}, name)
38
+ typecast_hash(field, value || {}, full_name)
43
39
  when Array
44
- typecast_array(field, value, name)
40
+ typecast_array(field, value, full_name)
45
41
  else
46
- typecast_value(field.type, value, name)
42
+ typecast_value(field.type, value, full_name)
47
43
  end
48
44
  end
49
45
 
50
- def typecast_hash(field, hash, name)
51
- raise Paramore::HashExpected.new(name, hash) unless hash.is_a?(Hash)
46
+ def typecast_hash(field, hash, full_name)
47
+ raise Paramore::HashExpected.new(full_name, hash) unless hash.is_a?(Hash)
52
48
 
53
49
  result =
54
50
  if field.wildly_keyed_hash?
55
- value_field = field.type.values.first
56
- key_type = field.type.keys.first
57
- hash.to_h do |name, value|
58
- [typecast_value(key_type, name, nil), cast(value_field, value, name)]
59
- end
51
+ cast_wild_hash(field, hash, full_name)
60
52
  else
61
- if options[:no_extra_keys]
62
- extra = hash.keys - field.type.keys
63
- raise "Found extra keys in `name`: #{extra}!"
64
- end
65
-
66
- field
67
- .type
68
- .reject { |name, value_field| missing_and_optional?(hash, name, value_field) }
69
- .to_h { |name, value_field| [name, cast(value_field, hash[name], name)] }
53
+ cast_regular_hash(field, hash, full_name)
70
54
  end
71
55
 
72
56
  field.compact? ? result.compact : result
73
57
  end
74
58
 
75
- def typecast_array(field, array, name)
76
- raise Paramore::ArrayExpected.new(name, array) unless array.is_a?(Array)
59
+ def typecast_array(field, array, full_name)
60
+ raise Paramore::ArrayExpected.new(full_name, array) unless array.is_a?(Array)
77
61
 
78
- result = array
62
+ result =
63
+ array
79
64
  .reject { |unit| unit.to_s == '' && field.compact? }
80
- .map { |unit| cast(Paramore.field(field.type.first, null: true), unit) }
65
+ .map { |unit| cast(Paramore.field(field.type.first, null: true), unit, nil, full_name) }
81
66
 
82
67
  field.compact? ? result.compact : result
83
68
  end
84
69
 
85
- def typecast_value(type, value, name)
70
+ def typecast_value(type, value, full_name)
86
71
  type.send(Paramore.configuration.type_method_name, value)
87
72
  rescue StandardError => e
88
- raise "The field `#{name}` raised \"#{e}\"!"
73
+ raise "Tried casting #{Paramore::ErrorFieldName(full_name)}, but \"#{e}\" was raised!"
74
+ end
75
+
76
+ def cast_wild_hash(field, hash, full_name)
77
+ value_field = field.type.values.first
78
+ key_type = field.type.keys.first
79
+
80
+ hash.to_h do |inner_key, value|
81
+ [
82
+ typecast_value(key_type, inner_key, nil),
83
+ cast(value_field, value, inner_key, full_name)
84
+ ]
85
+ end
86
+ end
87
+
88
+ def cast_regular_hash(field, hash, full_name)
89
+ if options[:no_extra_keys]
90
+ extra = hash.keys - field.type.keys
91
+ raise "Found extra keys in #{Paramore::ErrorFieldName(full_name)}: #{extra}!" if extra.any?
92
+ end
93
+
94
+ field
95
+ .type
96
+ .reject { |inner_key, value_field| missing_and_optional?(hash, inner_key, value_field) }
97
+ .to_h do |inner_key, value_field|
98
+ [inner_key, cast(value_field, hash[inner_key], inner_key, full_name)]
99
+ end
89
100
  end
90
101
 
91
- def missing_and_optional?(hash, name, field)
92
- !hash.key?(name) && !field.required?
102
+ def missing_and_optional?(hash, key, field)
103
+ !hash.key?(key) && !field.required?
93
104
  end
94
105
  end
95
106
  end
@@ -1,24 +1,24 @@
1
1
  class Paramore::NilParameter < StandardError
2
2
  def initialize(param_name)
3
- super("Received a nil `#{param_name}`, but it's type is non nullable!")
3
+ super("Received a nil #{Paramore::ErrorFieldName(param_name)}, but its type is non nullable!")
4
4
  end
5
5
  end
6
6
 
7
7
  class Paramore::NonField < StandardError
8
8
  def initialize(param_name, type)
9
- super("`#{param_name}` defined as a `#{type.class}`, expected a call of `Paramore.field()`! Perhaps you declared a plain hash instead of Paramore.field({})?")
9
+ super("#{Paramore::ErrorFieldName(param_name)} defined as a `#{type.class}`, expected a call of `Paramore.field()`! Perhaps you declared a plain hash instead of Paramore.field({})?")
10
10
  end
11
11
  end
12
12
 
13
13
  class Paramore::HashExpected < StandardError
14
14
  def initialize(param_name, param)
15
- super("Expected `#{param_name}` to be a hash, received #{param.class} instead!")
15
+ super("Expected #{Paramore::ErrorFieldName(param_name)} to be a hash, received #{param.class} instead!")
16
16
  end
17
17
  end
18
18
 
19
19
  class Paramore::ArrayExpected < StandardError
20
20
  def initialize(param_name, param)
21
- super("Expected `#{param_name}` to be an array, received #{param.class} instead!")
21
+ super("Expected #{Paramore::ErrorFieldName(param_name)} to be an array, received #{param.class} instead!")
22
22
  end
23
23
  end
24
24
 
@@ -27,3 +27,7 @@ class Paramore::HashTooWild < StandardError
27
27
  super("A hash field with a type as key may not contain any more entries! (so, eg.: { String => field } is ok, but { String => field, user_id: field } is not)")
28
28
  end
29
29
  end
30
+
31
+ def Paramore::ErrorFieldName(name)
32
+ name.present? ? "`#{name}`" : 'root field'
33
+ end
@@ -1,7 +1,14 @@
1
1
  module Paramore
2
+ module TypeRegexes
3
+ INTEGER_REGEX = /^-{0,1}\d+$/
4
+ FLOAT_REGEX = /^-{0,1}\d+(\.\d*){0,1}$/
5
+ end
6
+
2
7
  module BigDecimal
3
8
  module_function
4
9
  def [](input)
10
+ raise ArgumentError "#{input} is not a BigDecimal!" unless input.to_s.match?(TypeRegexes::FLOAT_REGEX)
11
+
5
12
  BigDecimal(input)
6
13
  end
7
14
  end
@@ -18,7 +25,7 @@ module Paramore
18
25
  module Float
19
26
  module_function
20
27
  def [](input)
21
- raise ArgumentError "#{input} is not a float!" unless input.to_s.match?(/-{0,1}\d*(\.\d*){0,1}/)
28
+ raise ArgumentError "#{input} is not a Float!" unless input.to_s.match?(TypeRegexes::FLOAT_REGEX)
22
29
 
23
30
  input.to_f
24
31
  end
@@ -27,7 +34,7 @@ module Paramore
27
34
  module Int
28
35
  module_function
29
36
  def [](input)
30
- raise ArgumentError, "#{input} is not an integer!" unless input.to_s.match?(/^-{0,1}\d*$/)
37
+ raise ArgumentError, "#{input} is not an Integer!" unless input.to_s.match?(TypeRegexes::INTEGER_REGEX)
31
38
 
32
39
  input.to_i
33
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paramore
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.9.1
4
+ version: 3.9.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Kairevičius
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-07 00:00:00.000000000 Z
11
+ date: 2022-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-rails
@@ -73,9 +73,6 @@ dependencies:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '5.0'
76
- - - "<"
77
- - !ruby/object:Gem::Version
78
- version: '7'
79
76
  type: :runtime
80
77
  prerelease: false
81
78
  version_requirements: !ruby/object:Gem::Requirement
@@ -83,9 +80,6 @@ dependencies:
83
80
  - - ">="
84
81
  - !ruby/object:Gem::Version
85
82
  version: '5.0'
86
- - - "<"
87
- - !ruby/object:Gem::Version
88
- version: '7'
89
83
  description: |
90
84
  Paramore lets you declare which parameters are permitted and what object is responsible
91
85
  for typing/sanitizing/type-casting them before they are passed along to your models/domain.
@@ -115,7 +109,7 @@ licenses:
115
109
  - MIT
116
110
  metadata: {}
117
111
  post_install_message: |
118
- Thank you for installing Paramore 3.9.1 !
112
+ Thank you for installing Paramore 3.9.5 !
119
113
  From the command line you can run `paramore` to generate a configuration file
120
114
 
121
115
  More details here : https://github.com/lumzdas/paramore/blob/master/README.md