parameters_schema 0.42 → 1.0.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 +4 -4
- data/Gemfile +3 -3
- data/README.md +286 -286
- data/Rakefile +7 -7
- data/lib/parameters_schema/core_ext.rb +21 -21
- data/lib/parameters_schema/exceptions.rb +23 -23
- data/lib/parameters_schema/options.rb +79 -79
- data/lib/parameters_schema/schema.rb +295 -295
- data/lib/parameters_schema.rb +8 -8
- data/test/helpers.rb +56 -56
- data/test/test_options.rb +74 -74
- data/test/test_schema_allow.rb +114 -114
- data/test/test_schema_allow_empty.rb +29 -29
- data/test/test_schema_allow_nil.rb +33 -33
- data/test/test_schema_hash.rb +90 -90
- data/test/test_schema_required.rb +74 -74
- data/test/test_schema_simple.rb +58 -58
- data/test/test_schema_types.rb +452 -452
- data/test/test_schema_types_complex.rb +126 -126
- metadata +9 -10
data/test/test_schema_hash.rb
CHANGED
@@ -1,90 +1,90 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'parameters_schema'
|
3
|
-
require_relative 'helpers'
|
4
|
-
|
5
|
-
describe 'Object' do
|
6
|
-
it 'creates a sub-schema for objects' do
|
7
|
-
ParametersSchema::Schema.new do
|
8
|
-
param :potatoe do
|
9
|
-
param :name
|
10
|
-
end
|
11
|
-
end
|
12
|
-
.must_allow({ name: 'Eramosa' })
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'creates a as many sub-schemas as needed' do
|
16
|
-
schema = ParametersSchema::Schema.new do
|
17
|
-
param :potatoe do
|
18
|
-
param :quantity, type: Fixnum
|
19
|
-
param :description do
|
20
|
-
param :name
|
21
|
-
param :local, type: :boolean
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
schema
|
27
|
-
.validate!(potatoe: { quantity: '10', description: { name: 'Eramosa', local: 't' } })
|
28
|
-
.must_equal_hash(potatoe: { quantity: 10, description: { name: 'Eramosa', local: true } })
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'allows an empty sub-schema' do
|
32
|
-
schema = ParametersSchema::Schema.new do
|
33
|
-
param :potatoe do end
|
34
|
-
end
|
35
|
-
.must_deny(nil, ParametersSchema::ErrorCode::NIL)
|
36
|
-
.must_deny({}, ParametersSchema::ErrorCode::EMPTY)
|
37
|
-
|
38
|
-
Proc
|
39
|
-
.new{ schema.validate!(potatoe: { name: 'Eramosa' }) }
|
40
|
-
.must_raise(ParametersSchema::InvalidParameters)
|
41
|
-
.errors.must_equal_hash(potatoe: { name: ParametersSchema::ErrorCode::UNKNOWN })
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'can be embeded in array' do
|
45
|
-
[{ array: true}, type: { Array => Hash }].each do |array_format|
|
46
|
-
schema = ParametersSchema::Schema.new do
|
47
|
-
param :potatoes, array_format do
|
48
|
-
param :name
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
schema
|
53
|
-
.validate!(potatoes: [{ name: 'Ac Belmont' }, { name: 'Eramosa' }])
|
54
|
-
.must_equal_hash(potatoes: [{ name: 'Ac Belmont' }, { name: 'Eramosa' }])
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'can be embeded in array - complex' do
|
59
|
-
schema = ParametersSchema::Schema.new do
|
60
|
-
param :potatoes, array: true do
|
61
|
-
param :quantity, type: Fixnum
|
62
|
-
param :description do
|
63
|
-
param :name
|
64
|
-
param :local, type: :boolean
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
potatoes = [
|
70
|
-
{
|
71
|
-
quantity: 10,
|
72
|
-
description: {
|
73
|
-
name: 'Eramosa',
|
74
|
-
local: true
|
75
|
-
}
|
76
|
-
},
|
77
|
-
{
|
78
|
-
quantity: 1000,
|
79
|
-
description: {
|
80
|
-
name: 'Eramosa II',
|
81
|
-
local: false
|
82
|
-
}
|
83
|
-
}
|
84
|
-
]
|
85
|
-
|
86
|
-
schema
|
87
|
-
.validate!(potatoes: potatoes)
|
88
|
-
.must_equal_hash(potatoes: potatoes)
|
89
|
-
end
|
90
|
-
end
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'parameters_schema'
|
3
|
+
require_relative 'helpers'
|
4
|
+
|
5
|
+
describe 'Object' do
|
6
|
+
it 'creates a sub-schema for objects' do
|
7
|
+
ParametersSchema::Schema.new do
|
8
|
+
param :potatoe do
|
9
|
+
param :name
|
10
|
+
end
|
11
|
+
end
|
12
|
+
.must_allow({ name: 'Eramosa' })
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'creates a as many sub-schemas as needed' do
|
16
|
+
schema = ParametersSchema::Schema.new do
|
17
|
+
param :potatoe do
|
18
|
+
param :quantity, type: Fixnum
|
19
|
+
param :description do
|
20
|
+
param :name
|
21
|
+
param :local, type: :boolean
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
schema
|
27
|
+
.validate!(potatoe: { quantity: '10', description: { name: 'Eramosa', local: 't' } })
|
28
|
+
.must_equal_hash(potatoe: { quantity: 10, description: { name: 'Eramosa', local: true } })
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'allows an empty sub-schema' do
|
32
|
+
schema = ParametersSchema::Schema.new do
|
33
|
+
param :potatoe do end
|
34
|
+
end
|
35
|
+
.must_deny(nil, ParametersSchema::ErrorCode::NIL)
|
36
|
+
.must_deny({}, ParametersSchema::ErrorCode::EMPTY)
|
37
|
+
|
38
|
+
Proc
|
39
|
+
.new{ schema.validate!(potatoe: { name: 'Eramosa' }) }
|
40
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
41
|
+
.errors.must_equal_hash(potatoe: { name: ParametersSchema::ErrorCode::UNKNOWN })
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'can be embeded in array' do
|
45
|
+
[{ array: true}, type: { Array => Hash }].each do |array_format|
|
46
|
+
schema = ParametersSchema::Schema.new do
|
47
|
+
param :potatoes, array_format do
|
48
|
+
param :name
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
schema
|
53
|
+
.validate!(potatoes: [{ name: 'Ac Belmont' }, { name: 'Eramosa' }])
|
54
|
+
.must_equal_hash(potatoes: [{ name: 'Ac Belmont' }, { name: 'Eramosa' }])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'can be embeded in array - complex' do
|
59
|
+
schema = ParametersSchema::Schema.new do
|
60
|
+
param :potatoes, array: true do
|
61
|
+
param :quantity, type: Fixnum
|
62
|
+
param :description do
|
63
|
+
param :name
|
64
|
+
param :local, type: :boolean
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
potatoes = [
|
70
|
+
{
|
71
|
+
quantity: 10,
|
72
|
+
description: {
|
73
|
+
name: 'Eramosa',
|
74
|
+
local: true
|
75
|
+
}
|
76
|
+
},
|
77
|
+
{
|
78
|
+
quantity: 1000,
|
79
|
+
description: {
|
80
|
+
name: 'Eramosa II',
|
81
|
+
local: false
|
82
|
+
}
|
83
|
+
}
|
84
|
+
]
|
85
|
+
|
86
|
+
schema
|
87
|
+
.validate!(potatoes: potatoes)
|
88
|
+
.must_equal_hash(potatoes: potatoes)
|
89
|
+
end
|
90
|
+
end
|
@@ -1,74 +1,74 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'parameters_schema'
|
3
|
-
require_relative 'helpers'
|
4
|
-
|
5
|
-
describe 'Required' do
|
6
|
-
before do
|
7
|
-
ParametersSchema::Options.reset_defaults
|
8
|
-
end
|
9
|
-
|
10
|
-
it 'is required by default' do
|
11
|
-
Proc.new do
|
12
|
-
ParametersSchema::Schema.new do
|
13
|
-
param :potatoe
|
14
|
-
end.validate!({})
|
15
|
-
end
|
16
|
-
.must_raise(ParametersSchema::InvalidParameters)
|
17
|
-
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::MISSING)
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'can be explicitly stated as required' do
|
21
|
-
Proc.new do
|
22
|
-
ParametersSchema::Schema.new do
|
23
|
-
param :potatoe, required: true
|
24
|
-
end.validate!({})
|
25
|
-
end
|
26
|
-
.must_raise(ParametersSchema::InvalidParameters)
|
27
|
-
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::MISSING)
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'can be set to be not required' do
|
31
|
-
ParametersSchema::Schema.new do
|
32
|
-
param :potatoe, required: false
|
33
|
-
end
|
34
|
-
.validate!({})
|
35
|
-
.must_equal_hash({})
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'applies to object params - validation successful' do
|
39
|
-
ParametersSchema::Schema.new do
|
40
|
-
param :potatoe do
|
41
|
-
param :name
|
42
|
-
param :type, required: false
|
43
|
-
end
|
44
|
-
end
|
45
|
-
.validate!(potatoe: { name: 'Eramosa' })
|
46
|
-
.must_equal_hash(potatoe: { name: 'Eramosa' })
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'applies to object params - validation failed on name' do
|
50
|
-
Proc.new do
|
51
|
-
ParametersSchema::Schema.new do
|
52
|
-
param :potatoe do
|
53
|
-
param :name
|
54
|
-
param :type, required: false
|
55
|
-
end
|
56
|
-
end.validate!(potatoe: {})
|
57
|
-
end
|
58
|
-
.must_raise(ParametersSchema::InvalidParameters)
|
59
|
-
.errors.must_equal_hash(potatoe: { name: ParametersSchema::ErrorCode::MISSING })
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'applies to object params - validation failed on potatoe' do
|
63
|
-
Proc.new do
|
64
|
-
ParametersSchema::Schema.new do
|
65
|
-
param :potatoe do
|
66
|
-
param :name
|
67
|
-
param :type, required: false
|
68
|
-
end
|
69
|
-
end.validate!({})
|
70
|
-
end
|
71
|
-
.must_raise(ParametersSchema::InvalidParameters)
|
72
|
-
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::MISSING)
|
73
|
-
end
|
74
|
-
end
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'parameters_schema'
|
3
|
+
require_relative 'helpers'
|
4
|
+
|
5
|
+
describe 'Required' do
|
6
|
+
before do
|
7
|
+
ParametersSchema::Options.reset_defaults
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'is required by default' do
|
11
|
+
Proc.new do
|
12
|
+
ParametersSchema::Schema.new do
|
13
|
+
param :potatoe
|
14
|
+
end.validate!({})
|
15
|
+
end
|
16
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
17
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::MISSING)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'can be explicitly stated as required' do
|
21
|
+
Proc.new do
|
22
|
+
ParametersSchema::Schema.new do
|
23
|
+
param :potatoe, required: true
|
24
|
+
end.validate!({})
|
25
|
+
end
|
26
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
27
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::MISSING)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'can be set to be not required' do
|
31
|
+
ParametersSchema::Schema.new do
|
32
|
+
param :potatoe, required: false
|
33
|
+
end
|
34
|
+
.validate!({})
|
35
|
+
.must_equal_hash({})
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'applies to object params - validation successful' do
|
39
|
+
ParametersSchema::Schema.new do
|
40
|
+
param :potatoe do
|
41
|
+
param :name
|
42
|
+
param :type, required: false
|
43
|
+
end
|
44
|
+
end
|
45
|
+
.validate!(potatoe: { name: 'Eramosa' })
|
46
|
+
.must_equal_hash(potatoe: { name: 'Eramosa' })
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'applies to object params - validation failed on name' do
|
50
|
+
Proc.new do
|
51
|
+
ParametersSchema::Schema.new do
|
52
|
+
param :potatoe do
|
53
|
+
param :name
|
54
|
+
param :type, required: false
|
55
|
+
end
|
56
|
+
end.validate!(potatoe: {})
|
57
|
+
end
|
58
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
59
|
+
.errors.must_equal_hash(potatoe: { name: ParametersSchema::ErrorCode::MISSING })
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'applies to object params - validation failed on potatoe' do
|
63
|
+
Proc.new do
|
64
|
+
ParametersSchema::Schema.new do
|
65
|
+
param :potatoe do
|
66
|
+
param :name
|
67
|
+
param :type, required: false
|
68
|
+
end
|
69
|
+
end.validate!({})
|
70
|
+
end
|
71
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
72
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::MISSING)
|
73
|
+
end
|
74
|
+
end
|
data/test/test_schema_simple.rb
CHANGED
@@ -1,58 +1,58 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'parameters_schema'
|
3
|
-
require_relative 'helpers'
|
4
|
-
|
5
|
-
describe 'Empty schema' do
|
6
|
-
before do
|
7
|
-
ParametersSchema::Options.reset_defaults
|
8
|
-
@schema = ParametersSchema::Schema.new do end
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'accepts nil params' do
|
12
|
-
@schema
|
13
|
-
.validate!(nil)
|
14
|
-
.must_equal_hash({})
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'accepts empty params' do
|
18
|
-
@schema
|
19
|
-
.validate!({})
|
20
|
-
.must_equal_hash({})
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'wont accept non-empty params' do
|
24
|
-
Proc
|
25
|
-
.new{ @schema.validate!(potatoe: 'Eramosa') }
|
26
|
-
.must_raise(ParametersSchema::InvalidParameters)
|
27
|
-
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::UNKNOWN)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
describe 'Simple schema' do
|
32
|
-
before do
|
33
|
-
@schema = ParametersSchema::Schema.new do
|
34
|
-
param :potatoe
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'accepts string or symbol keys' do
|
39
|
-
['potatoe', :potatoe].each do |key|
|
40
|
-
@schema
|
41
|
-
.validate!(key => 'Eramosa')
|
42
|
-
.must_equal_hash(key => 'Eramosa')
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'validates a valid input' do
|
47
|
-
@schema
|
48
|
-
.validate!(potatoe: 'Eramosa')
|
49
|
-
.must_equal_hash(potatoe: 'Eramosa')
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'validates an invalid input because of a missing key' do
|
53
|
-
exception = Proc
|
54
|
-
.new{ @schema.validate!({}) }
|
55
|
-
.must_raise(ParametersSchema::InvalidParameters)
|
56
|
-
.errors.must_equal_hash(potatoe: :missing)
|
57
|
-
end
|
58
|
-
end
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'parameters_schema'
|
3
|
+
require_relative 'helpers'
|
4
|
+
|
5
|
+
describe 'Empty schema' do
|
6
|
+
before do
|
7
|
+
ParametersSchema::Options.reset_defaults
|
8
|
+
@schema = ParametersSchema::Schema.new do end
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'accepts nil params' do
|
12
|
+
@schema
|
13
|
+
.validate!(nil)
|
14
|
+
.must_equal_hash({})
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'accepts empty params' do
|
18
|
+
@schema
|
19
|
+
.validate!({})
|
20
|
+
.must_equal_hash({})
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'wont accept non-empty params' do
|
24
|
+
Proc
|
25
|
+
.new{ @schema.validate!(potatoe: 'Eramosa') }
|
26
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
27
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::UNKNOWN)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'Simple schema' do
|
32
|
+
before do
|
33
|
+
@schema = ParametersSchema::Schema.new do
|
34
|
+
param :potatoe
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'accepts string or symbol keys' do
|
39
|
+
['potatoe', :potatoe].each do |key|
|
40
|
+
@schema
|
41
|
+
.validate!(key => 'Eramosa')
|
42
|
+
.must_equal_hash(key => 'Eramosa')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'validates a valid input' do
|
47
|
+
@schema
|
48
|
+
.validate!(potatoe: 'Eramosa')
|
49
|
+
.must_equal_hash(potatoe: 'Eramosa')
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'validates an invalid input because of a missing key' do
|
53
|
+
exception = Proc
|
54
|
+
.new{ @schema.validate!({}) }
|
55
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
56
|
+
.errors.must_equal_hash(potatoe: :missing)
|
57
|
+
end
|
58
|
+
end
|