parameters_schema 0.42
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/README.md +286 -0
- data/Rakefile +8 -0
- data/lib/parameters_schema/core_ext.rb +21 -0
- data/lib/parameters_schema/exceptions.rb +23 -0
- data/lib/parameters_schema/options.rb +80 -0
- data/lib/parameters_schema/schema.rb +296 -0
- data/lib/parameters_schema.rb +8 -0
- data/test/helpers.rb +56 -0
- data/test/test_options.rb +74 -0
- data/test/test_schema_allow.rb +114 -0
- data/test/test_schema_allow_empty.rb +30 -0
- data/test/test_schema_allow_nil.rb +34 -0
- data/test/test_schema_hash.rb +90 -0
- data/test/test_schema_required.rb +74 -0
- data/test/test_schema_simple.rb +58 -0
- data/test/test_schema_types.rb +452 -0
- data/test/test_schema_types_complex.rb +126 -0
- metadata +77 -0
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'parameters_schema'
|
3
|
+
require_relative 'helpers'
|
4
|
+
|
5
|
+
describe 'Complex types' do
|
6
|
+
describe 'Multiple' do
|
7
|
+
it 'allows multiple types for a parameter' do
|
8
|
+
schema = ParametersSchema::Schema.new do
|
9
|
+
param :potatoe, type: [String, :boolean]
|
10
|
+
end
|
11
|
+
|
12
|
+
schema
|
13
|
+
.validate!(potatoe: 'Eramosa')
|
14
|
+
.must_equal_hash(potatoe: 'Eramosa')
|
15
|
+
|
16
|
+
schema
|
17
|
+
.validate!(potatoe: true)
|
18
|
+
.must_equal_hash(potatoe: true)
|
19
|
+
|
20
|
+
[2, 3.0, Date.today, DateTime.now, [1], { nope: true }].each do |value|
|
21
|
+
Proc
|
22
|
+
.new{ schema.validate!(potatoe: value) }
|
23
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
24
|
+
.errors.must_equal_hash(potatoe: :disallowed)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'is swallowed by the :any keyword' do
|
29
|
+
schema = ParametersSchema::Schema.new do
|
30
|
+
param :potatoe, type: [String, :any]
|
31
|
+
end
|
32
|
+
|
33
|
+
[1, 1.0, Date.today, DateTime.now, [1], { nope: true }, 'Eramosa', true].each do |value|
|
34
|
+
schema
|
35
|
+
.validate!(potatoe: value)
|
36
|
+
.must_equal_hash(potatoe: value)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'Array of something' do
|
42
|
+
it 'allows the type' do
|
43
|
+
schema = ParametersSchema::Schema.new do
|
44
|
+
param :potatoe, type: { Array => String }
|
45
|
+
end
|
46
|
+
|
47
|
+
schema
|
48
|
+
.validate!(potatoe: ['Eramosa', 'Kennebec', 'Conestoga'])
|
49
|
+
.must_equal_hash(potatoe: ['Eramosa', 'Kennebec', 'Conestoga'])
|
50
|
+
|
51
|
+
[2, 3.0, Date.today, DateTime.now, [1], { nope: true }, [true, false, true]].each do |value|
|
52
|
+
Proc
|
53
|
+
.new{ schema.validate!(potatoe: value) }
|
54
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
55
|
+
.errors.must_equal_hash(potatoe: :disallowed)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'requires all values to be of the same type by default' do
|
60
|
+
schema = ParametersSchema::Schema.new do
|
61
|
+
param :potatoe, type: { Array => :boolean }
|
62
|
+
end
|
63
|
+
|
64
|
+
schema
|
65
|
+
.validate!(potatoe: [true, false, false, true])
|
66
|
+
.must_equal_hash(potatoe: [true, false, false, true])
|
67
|
+
|
68
|
+
Proc
|
69
|
+
.new{ schema.validate!(potatoe: [true, false, false, 4]) }
|
70
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
71
|
+
.errors.must_equal_hash(potatoe: :disallowed)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'accepts multiple values' do
|
75
|
+
schema = ParametersSchema::Schema.new do
|
76
|
+
param :potatoe, type: { Array => [:boolean, Fixnum, Symbol] }
|
77
|
+
end
|
78
|
+
|
79
|
+
schema
|
80
|
+
.validate!(potatoe: [true, 2, :working, false, 2, :still_working])
|
81
|
+
.must_equal_hash(potatoe: [true, 2, :working, false, 2, :still_working])
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'accepts an array of arrays of :any' do
|
85
|
+
schema = ParametersSchema::Schema.new do
|
86
|
+
param :potatoe, type: { Array => Array }
|
87
|
+
end
|
88
|
+
|
89
|
+
schema
|
90
|
+
.validate!(potatoe: [[1, 2], [true, false], ['a', 'b', 'c'], [{ name: 'Eramosa' }]])
|
91
|
+
.must_equal_hash(potatoe: [[1, 2], [true, false], ['a', 'b', 'c'], [{ name: 'Eramosa' }]])
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'accepts an array of arrays' do
|
95
|
+
schema = ParametersSchema::Schema.new do
|
96
|
+
param :potatoe, type: { Array => { Array => Fixnum } }
|
97
|
+
end
|
98
|
+
|
99
|
+
schema
|
100
|
+
.validate!(potatoe: [[1, 2], [3, 4, 5], [6, 7]])
|
101
|
+
.must_equal_hash(potatoe: [[1, 2], [3, 4, 5], [6, 7]])
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'can be used in conjunction with array: true' do
|
105
|
+
schema = ParametersSchema::Schema.new do
|
106
|
+
param :potatoe, type: { Array => :boolean }, array: true
|
107
|
+
end
|
108
|
+
|
109
|
+
schema
|
110
|
+
.validate!(potatoe: [true, true, false])
|
111
|
+
.must_equal_hash(potatoe: [true, true, false])
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'doesnt make sense for any other type so fallback to the key' do
|
115
|
+
schema = ParametersSchema::Schema.new do
|
116
|
+
param :potatoe, type: { Date => Fixnum }
|
117
|
+
end
|
118
|
+
|
119
|
+
today = Date.today
|
120
|
+
|
121
|
+
schema
|
122
|
+
.validate!(potatoe: today)
|
123
|
+
.must_equal_hash(potatoe: today)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: parameters_schema
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.42'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jodi Giordano
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Validates parameters of requests using a schema defined with a simple
|
28
|
+
DSL
|
29
|
+
email: giordano.jodi@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- Gemfile
|
35
|
+
- Rakefile
|
36
|
+
- README.md
|
37
|
+
- lib/parameters_schema/core_ext.rb
|
38
|
+
- lib/parameters_schema/exceptions.rb
|
39
|
+
- lib/parameters_schema/options.rb
|
40
|
+
- lib/parameters_schema/schema.rb
|
41
|
+
- lib/parameters_schema.rb
|
42
|
+
- test/helpers.rb
|
43
|
+
- test/test_options.rb
|
44
|
+
- test/test_schema_allow.rb
|
45
|
+
- test/test_schema_allow_empty.rb
|
46
|
+
- test/test_schema_allow_nil.rb
|
47
|
+
- test/test_schema_hash.rb
|
48
|
+
- test/test_schema_required.rb
|
49
|
+
- test/test_schema_simple.rb
|
50
|
+
- test/test_schema_types.rb
|
51
|
+
- test/test_schema_types_complex.rb
|
52
|
+
homepage: https://github.com/jodigiordano/parameters_schema
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata: {}
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 2.0.3
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: Strict schema for request parameters
|
76
|
+
test_files: []
|
77
|
+
has_rdoc:
|