parameters_schema 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -1
- data/lib/parameters_schema/schema.rb +17 -7
- data/test/test_schema_default_value.rb +78 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27f563b75284363c989baf6b54a932ff2297d4d0
|
4
|
+
data.tar.gz: 56f396dfe522de312289f5ab44dd638cde52e53f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3da599edadcc20dcc772b9ec3c4ac9d71d21653e49e49d6c97f6d13c359854424f4e03bfdd93fbd8861727ca494508c8b3cbd6a22ed6a2f5b97786e470a47e8
|
7
|
+
data.tar.gz: 29d4d87b1e4a17c948b7f9caaf691e96a0ce92d0823e4a8196cb301c1de0fedbedd336dbfc03c1afa97215c4991fbebb5d5971d6ab7600b527225f784511e41b
|
data/README.md
CHANGED
@@ -96,10 +96,17 @@ The valid options for a parameter are:
|
|
96
96
|
* required # Whether the parameter is required. Default: true.
|
97
97
|
* type # The type of the parameter. Default: String.
|
98
98
|
* allow # The allowed values of the parameter. Default: :any.
|
99
|
-
* deny
|
99
|
+
* deny # The denied values of the parameter. Default: :none.
|
100
100
|
* array # Whether the parameter is an array. Default: false.
|
101
|
+
* default # Default value when the parameter is missing. Default: nil.
|
101
102
|
```
|
102
103
|
|
104
|
+
Notes on `default` option:
|
105
|
+
|
106
|
+
* When set, the `required` option becomes automatically `false`.
|
107
|
+
* When set, the provided value is always kept in sanitized hash, even if the value is `nil`.
|
108
|
+
* Cannot be used on a `array` or `hash` type (but can be used on the fields of an hash!).
|
109
|
+
|
103
110
|
### Parameter types
|
104
111
|
|
105
112
|
The available types are:
|
@@ -19,7 +19,8 @@ module ParametersSchema
|
|
19
19
|
private
|
20
20
|
|
21
21
|
def param(name, options = {}, &inner_params)
|
22
|
-
options[:required] = !options.has_key?(:required) || options[:required].present?
|
22
|
+
options[:required] = (!options.has_key?(:required) || options[:required].present?) && !options.has_key?(:default)
|
23
|
+
options[:default_is_nil] = options.has_key?(:default) && options[:default].nil?
|
23
24
|
|
24
25
|
options[:type] = [options[:type] || String].flatten
|
25
26
|
options[:allow] = [options[:allow].present? ? options[:allow] : ParametersSchema::Options.any_keyword].flatten
|
@@ -119,8 +120,10 @@ module ParametersSchema
|
|
119
120
|
|
120
121
|
if options[:required] && !options[:parent].has_key?(name)
|
121
122
|
error = ParametersSchema::ErrorCode::MISSING
|
122
|
-
elsif options[:parent].has_key?(name)
|
123
|
+
elsif options[:parent].has_key?(name) && !options[:parent][name].nil?
|
123
124
|
value = options[:parent][name]
|
125
|
+
else
|
126
|
+
value = options[:default]
|
124
127
|
end
|
125
128
|
|
126
129
|
[value, error]
|
@@ -129,7 +132,7 @@ module ParametersSchema
|
|
129
132
|
def __validate_param_value_nil(value, options)
|
130
133
|
error = nil
|
131
134
|
|
132
|
-
if !options[:allow].include?(ParametersSchema::Options.nil_keyword) && value.nil?
|
135
|
+
if !options[:default_is_nil] && !options[:allow].include?(ParametersSchema::Options.nil_keyword) && value.nil?
|
133
136
|
error = ParametersSchema::ErrorCode::NIL
|
134
137
|
end
|
135
138
|
|
@@ -227,13 +230,13 @@ module ParametersSchema
|
|
227
230
|
error = ParametersSchema::ErrorCode::DISALLOWED unless value.respond_to?(:to_sym)
|
228
231
|
value = value.to_sym if error.blank? # cast to right type.
|
229
232
|
elsif type == Date
|
230
|
-
begin
|
233
|
+
begin
|
231
234
|
value = value.kind_of?(String) ? Date.parse(value) : value.to_date
|
232
235
|
rescue
|
233
236
|
error = ParametersSchema::ErrorCode::DISALLOWED
|
234
237
|
end
|
235
238
|
elsif type == DateTime
|
236
|
-
begin
|
239
|
+
begin
|
237
240
|
value = value.kind_of?(String) ? DateTime.parse(value) : value.to_datetime
|
238
241
|
rescue
|
239
242
|
error = ParametersSchema::ErrorCode::DISALLOWED
|
@@ -268,7 +271,14 @@ module ParametersSchema
|
|
268
271
|
end
|
269
272
|
|
270
273
|
def __stop_validation(name, value, error, options)
|
271
|
-
{
|
274
|
+
{
|
275
|
+
param: name,
|
276
|
+
error: error,
|
277
|
+
value: value,
|
278
|
+
keep_if_nil:
|
279
|
+
options[:allow].include?(ParametersSchema::Options.nil_keyword) ||
|
280
|
+
(value.nil? && options[:default_is_nil] )
|
281
|
+
}
|
272
282
|
end
|
273
283
|
|
274
284
|
def __handle_errors
|
@@ -293,4 +303,4 @@ module ParametersSchema
|
|
293
303
|
end
|
294
304
|
end
|
295
305
|
end
|
296
|
-
end
|
306
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'parameters_schema'
|
3
|
+
require_relative 'helpers'
|
4
|
+
|
5
|
+
describe 'Default value' do
|
6
|
+
before do
|
7
|
+
ParametersSchema::Options.reset_defaults
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'doesnt keep the parameter when not explicitly set to nil' do
|
11
|
+
ParametersSchema::Schema.new do
|
12
|
+
param :potatoe, required: false
|
13
|
+
end
|
14
|
+
.validate!({})
|
15
|
+
.must_equal_hash({})
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'keeps the parameter when explicitly set to nil' do
|
19
|
+
ParametersSchema::Schema.new do
|
20
|
+
param :potatoe, default: nil
|
21
|
+
end
|
22
|
+
.validate!({})
|
23
|
+
.must_equal_hash({ potatoe: nil })
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'must be set to a value of the same type' do
|
27
|
+
ParametersSchema::Schema.new do
|
28
|
+
param :potatoe, default: 'Eramosa'
|
29
|
+
end
|
30
|
+
.validate!({})
|
31
|
+
.must_equal_hash({ potatoe: 'Eramosa' })
|
32
|
+
|
33
|
+
Proc.new do
|
34
|
+
ParametersSchema::Schema.new do
|
35
|
+
param :potatoe, type: Fixnum, default: 'Eramosa'
|
36
|
+
end
|
37
|
+
.validate!({})
|
38
|
+
.must_equal_hash({ potatoe: 'Eramosa' })
|
39
|
+
end
|
40
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
41
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::DISALLOWED)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'keeps the value provided' do
|
45
|
+
ParametersSchema::Schema.new do
|
46
|
+
param :potatoe, default: nil
|
47
|
+
end
|
48
|
+
.validate!(potatoe: 'Eramosa')
|
49
|
+
.must_equal_hash({ potatoe: 'Eramosa' })
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'works with complex values - array of hashes' do
|
53
|
+
ParametersSchema::Schema.new do
|
54
|
+
param :potatoes, array: true do
|
55
|
+
param :variety, default: 'Eramosa'
|
56
|
+
param :origin
|
57
|
+
end
|
58
|
+
end
|
59
|
+
.validate!(potatoes: [{ origin: 'NB' }, { origin: 'Canada' }])
|
60
|
+
.must_equal_hash(potatoes: [
|
61
|
+
{ variety: 'Eramosa', origin: 'NB' },
|
62
|
+
{ variety: 'Eramosa', origin: 'Canada' }
|
63
|
+
])
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'works with complex values - fields of hash' do
|
67
|
+
default_value = { variety: 'Eramosa', origin: 'New Brunswick' }
|
68
|
+
|
69
|
+
ParametersSchema::Schema.new do
|
70
|
+
param :potatoe do
|
71
|
+
param :variety, default: 'Eramosa'
|
72
|
+
param :origin, default: 'New Brunswick'
|
73
|
+
end
|
74
|
+
end
|
75
|
+
.validate!(potatoe: { origin: 'Canada' })
|
76
|
+
.must_equal_hash(potatoe: { variety: 'Eramosa', origin: 'Canada' })
|
77
|
+
end
|
78
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parameters_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jodi Giordano
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -44,6 +44,7 @@ files:
|
|
44
44
|
- test/test_schema_allow.rb
|
45
45
|
- test/test_schema_allow_empty.rb
|
46
46
|
- test/test_schema_allow_nil.rb
|
47
|
+
- test/test_schema_default_value.rb
|
47
48
|
- test/test_schema_hash.rb
|
48
49
|
- test/test_schema_required.rb
|
49
50
|
- test/test_schema_simple.rb
|