explicit-parameters 0.0.3 → 0.1.0
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 +4 -4
- data/lib/explicit_parameters/parameters.rb +15 -2
- data/lib/explicit_parameters/version.rb +1 -1
- data/spec/parameters_spec.rb +58 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a6880bad37cdfc35abc3a08ffd8591df594dfa4
|
4
|
+
data.tar.gz: 4377837d9757eae083a2a20f41ff925066a39f11
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49b1ac6b9514b4d25351ef8028eec8e24a4468332b6457ddb540c0d46f3171e73a5d7a02fb60e1a8432bb6927eda3e4595c6078a2fa70b983213ae3480de9cb1
|
7
|
+
data.tar.gz: 8e4c49b64a93ef0e0a28d133ac749f15d48024c1c92eb2b128d73c9bdbfc87f29c7dd885ab9ec4645400a8e6f957f634d0dc4920b91e6eb5992308cf49385e56
|
@@ -33,7 +33,16 @@ module ExplicitParameters
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def accepts(name, type = nil, options = {}, &block)
|
36
|
-
|
36
|
+
if block_given?
|
37
|
+
subtype = define(name, &block)
|
38
|
+
if type == Array
|
39
|
+
type = Array[subtype]
|
40
|
+
elsif type == nil
|
41
|
+
type = subtype
|
42
|
+
else
|
43
|
+
raise ArgumentError, "`type` argument can only be `nil` or `Array` when a block is provided"
|
44
|
+
end
|
45
|
+
end
|
37
46
|
attribute(name, type, options.slice(:default, :required))
|
38
47
|
validations = options.except(:default)
|
39
48
|
validations[:coercion] = true
|
@@ -66,7 +75,11 @@ module ExplicitParameters
|
|
66
75
|
end
|
67
76
|
|
68
77
|
def validate_attribute_provided!(attribute_name, value)
|
69
|
-
|
78
|
+
if !@original_attributes.key?(attribute_name.to_s)
|
79
|
+
errors.add attribute_name, 'is required'
|
80
|
+
elsif attribute_set[attribute_name].type.primitive == Array && value == [].freeze
|
81
|
+
errors.add attribute_name, 'is required'
|
82
|
+
end
|
70
83
|
end
|
71
84
|
|
72
85
|
def validate_attribute_coercion!(attribute_name, value)
|
data/spec/parameters_spec.rb
CHANGED
@@ -89,4 +89,62 @@ RSpec.describe ExplicitParameters::Parameters do
|
|
89
89
|
}.to raise_error(ExplicitParameters::InvalidParameters, message)
|
90
90
|
end
|
91
91
|
end
|
92
|
+
|
93
|
+
context 'with list of parameters' do
|
94
|
+
let :definition do
|
95
|
+
ExplicitParameters::Parameters.define(:nested) do
|
96
|
+
accepts :addresses, Array do
|
97
|
+
requires :street, String
|
98
|
+
requires :city, String
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
let :parameters do
|
104
|
+
{
|
105
|
+
'addresses' => [
|
106
|
+
{
|
107
|
+
'street' => '3575 St-Laurent',
|
108
|
+
'city' => 'Montréal',
|
109
|
+
}
|
110
|
+
]
|
111
|
+
}
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'the exposed parameter is an Array' do
|
115
|
+
expect(params.addresses).to be_an Array
|
116
|
+
expect(params.addresses.size).to be == 1
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'parses nested hashes' do
|
120
|
+
expect(params.addresses.first.street).to be == '3575 St-Laurent'
|
121
|
+
expect(params.addresses.first.city).to be == 'Montréal'
|
122
|
+
end
|
123
|
+
|
124
|
+
context 'when required' do
|
125
|
+
let :definition do
|
126
|
+
ExplicitParameters::Parameters.define(:nested) do
|
127
|
+
requires :addresses, Array do
|
128
|
+
requires :street, String
|
129
|
+
requires :city, String
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'reports missing attributes' do
|
135
|
+
message = {errors: {addresses: ['is required']}}.to_json
|
136
|
+
expect {
|
137
|
+
definition.parse!({})
|
138
|
+
}.to raise_error(ExplicitParameters::InvalidParameters, message)
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'considers empty arrays as missing' do
|
142
|
+
message = {errors: {addresses: ['is required']}}.to_json
|
143
|
+
expect {
|
144
|
+
params = definition.parse!({'addresses' => []})
|
145
|
+
p params.addresses
|
146
|
+
}.to raise_error(ExplicitParameters::InvalidParameters, message)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
92
150
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: explicit-parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean Boussier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -138,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
138
|
version: '0'
|
139
139
|
requirements: []
|
140
140
|
rubyforge_project:
|
141
|
-
rubygems_version: 2.4.
|
141
|
+
rubygems_version: 2.4.6
|
142
142
|
signing_key:
|
143
143
|
specification_version: 4
|
144
144
|
summary: Explicit parameters validation and casting for Rails APIs
|