compel 0.1.3 → 0.2.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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +7 -0
  3. data/README.md +65 -54
  4. data/lib/compel/builder/boolean.rb +13 -0
  5. data/lib/compel/builder/common.rb +24 -0
  6. data/lib/compel/builder/common_value.rb +34 -0
  7. data/lib/compel/builder/date.rb +23 -0
  8. data/lib/compel/builder/datetime.rb +23 -0
  9. data/lib/compel/builder/float.rb +15 -0
  10. data/lib/compel/builder/hash.rb +22 -0
  11. data/lib/compel/builder/integer.rb +15 -0
  12. data/lib/compel/builder/json.rb +13 -0
  13. data/lib/compel/builder/methods.rb +60 -0
  14. data/lib/compel/builder/schema.rb +27 -0
  15. data/lib/compel/builder/string.rb +30 -0
  16. data/lib/compel/builder/time.rb +23 -0
  17. data/lib/compel/coercion/boolean.rb +1 -1
  18. data/lib/compel/coercion/date.rb +19 -2
  19. data/lib/compel/coercion/datetime.rb +19 -2
  20. data/lib/compel/coercion/float.rb +1 -1
  21. data/lib/compel/coercion/hash.rb +1 -1
  22. data/lib/compel/coercion/integer.rb +1 -1
  23. data/lib/compel/coercion/json.rb +1 -1
  24. data/lib/compel/coercion/regexp.rb +17 -0
  25. data/lib/compel/coercion/string.rb +1 -1
  26. data/lib/compel/coercion/time.rb +19 -2
  27. data/lib/compel/coercion/type.rb +0 -13
  28. data/lib/compel/coercion.rb +8 -10
  29. data/lib/compel/contract.rb +18 -62
  30. data/lib/compel/exceptions/invalid_hash_error.rb +9 -0
  31. data/lib/compel/exceptions/type_error.rb +7 -0
  32. data/lib/compel/exceptions/validation_error.rb +7 -0
  33. data/lib/compel/validation.rb +36 -50
  34. data/lib/compel/validators/base.rb +19 -0
  35. data/lib/compel/validators/hash_validator.rb +51 -0
  36. data/lib/compel/validators/type_validator.rb +30 -0
  37. data/lib/compel/version.rb +1 -1
  38. data/lib/compel.rb +16 -11
  39. data/spec/compel/builder_spec.rb +226 -0
  40. data/spec/compel/coercion_spec.rb +85 -18
  41. data/spec/compel/compel_spec.rb +368 -160
  42. data/spec/compel/sinatra_integration_spec.rb +73 -0
  43. data/spec/compel/validation_spec.rb +122 -8
  44. data/spec/spec_helper.rb +19 -0
  45. data/spec/support/sinatra_app.rb +43 -0
  46. metadata +28 -10
  47. data/lib/compel/invalid_params_error.rb +0 -9
  48. data/lib/compel/param.rb +0 -46
  49. data/lib/compel/param_type_error.rb +0 -7
  50. data/lib/compel/param_validation_error.rb +0 -7
  51. data/spec/compel/contract_spec.rb +0 -36
  52. data/spec/compel/param_spec.rb +0 -25
@@ -5,13 +5,13 @@ describe Compel::Validation do
5
5
  context 'required' do
6
6
 
7
7
  it 'should validate without errors' do
8
- errors = Compel::Validation.validate(123, { required: true })
8
+ errors = Compel::Validation.validate(123, Compel::Coercion::Integer, { required: true })
9
9
 
10
10
  expect(errors.empty?).to eq(true)
11
11
  end
12
12
 
13
13
  it 'should validate with error' do
14
- errors = Compel::Validation.validate(nil, { required: true })
14
+ errors = Compel::Validation.validate(nil, Compel::Coercion::Integer, { required: true })
15
15
 
16
16
  expect(errors.empty?).to eq(false)
17
17
  expect(errors).to eq(['is required'])
@@ -22,7 +22,7 @@ describe Compel::Validation do
22
22
  context 'length' do
23
23
 
24
24
  it 'should validate without errors' do
25
- errors = Compel::Validation.validate(123, { length: 3 })
25
+ errors = Compel::Validation.validate(123, Compel::Coercion::Integer, { length: 3 })
26
26
 
27
27
  expect(errors.empty?).to eq(true)
28
28
  end
@@ -33,7 +33,7 @@ describe Compel::Validation do
33
33
 
34
34
  def expect_be_in_within_range(range, value)
35
35
  [:in, :within].each do |key|
36
- errors = Compel::Validation.validate(value, { key => range })
36
+ errors = Compel::Validation.validate(value, Compel::Coercion::String, { key => range })
37
37
  yield errors
38
38
  end
39
39
  end
@@ -46,28 +46,142 @@ describe Compel::Validation do
46
46
 
47
47
  it 'should validate with errors' do
48
48
  expect_be_in_within_range(['PT', 'UK'], 'US') do |errors|
49
- expect(errors).to eq(['must be within ["PT", "UK"]'])
49
+ expect(errors).to include('must be within ["PT", "UK"]')
50
50
  end
51
51
  end
52
52
 
53
53
  context 'range' do
54
54
 
55
55
  it 'should validate without errors' do
56
- errors = Compel::Validation.validate(2, range: (1..3))
56
+ errors = Compel::Validation.validate(2, Compel::Coercion::Integer, range: (1..3))
57
57
 
58
58
  expect(errors.empty?).to eq(true)
59
59
  end
60
60
 
61
61
  it 'should validate with errors' do
62
- errors = Compel::Validation.validate(4, range: (1..3))
62
+ errors = Compel::Validation.validate(4, Compel::Coercion::Integer, range: (1..3))
63
63
 
64
- expect(errors).to eq(['must be within 1..3'])
64
+ expect(errors).to include('must be within 1..3')
65
65
  end
66
66
 
67
67
  end
68
68
 
69
69
  end
70
70
 
71
+ context 'format' do
72
+
73
+ it 'should validate with errors' do
74
+ format = /^abcd/
75
+ errors = Compel::Validation.validate('acb', Compel::Coercion::String, format: format)
76
+
77
+ expect(errors).to include("must match format ^abcd")
78
+ end
79
+
80
+ it 'should validate with errors 1' do
81
+ format = /^abcd/
82
+ errors = Compel::Validation.validate(123, Compel::Coercion::Integer, format: format)
83
+
84
+ expect(errors).to include('must be a string if using the format validation')
85
+ end
86
+
87
+ it 'should validate with errors 2' do
88
+ format = /^abcd/
89
+ errors = Compel::Validation.validate(nil, Compel::Coercion::String, format: format)
90
+
91
+ expect(errors).to include('must be a string if using the format validation')
92
+ end
93
+
94
+ it 'should validate without errors' do
95
+ format = /abcd/
96
+ errors = Compel::Validation.validate('abcd', Compel::Coercion::String, format: format)
97
+ expect(errors.empty?).to eq(true)
98
+ end
99
+
100
+ end
101
+
102
+ context 'is' do
103
+
104
+ it 'should validate with errors' do
105
+ errors = Compel::Validation.validate('abcd', Compel::Coercion::Integer, is: 123)
106
+ expect(errors).to include('must be 123')
107
+ end
108
+
109
+ it 'should validate with errors 1' do
110
+ errors = Compel::Validation.validate(nil, Compel::Coercion::Integer, is: 123)
111
+ expect(errors).to include('must be 123')
112
+ end
113
+
114
+ it 'should validate without errors' do
115
+ errors = Compel::Validation.validate(123, Compel::Coercion::Integer, is: 123)
116
+ expect(errors.empty?).to eq(true)
117
+ end
118
+
119
+ end
120
+
121
+ context 'min' do
122
+
123
+ it 'should validate with errors' do
124
+ errors = Compel::Validation.validate(1, Compel::Coercion::Integer, min: 3)
125
+
126
+ expect(errors).to include('cannot be less than 3')
127
+ end
128
+
129
+ end
130
+
131
+ context 'max' do
132
+
133
+ it 'should validate with errors' do
134
+ errors = Compel::Validation.validate(3, Compel::Coercion::Integer, max: 2)
135
+
136
+ expect(errors).to include('cannot be greater than 2')
137
+ end
138
+
139
+ end
140
+
141
+ context 'min_length' do
142
+
143
+ it 'should validate with errors' do
144
+ errors = Compel::Validation.validate(3, Compel::Coercion::Integer, min_length: 2)
145
+
146
+ expect(errors).to include('must be a string if using the min_length validation')
147
+ end
148
+
149
+ it 'should validate with errors 1' do
150
+ errors = Compel::Validation.validate('a', Compel::Coercion::String, min_length: 2)
151
+
152
+ expect(errors).to include('cannot have length less than 2')
153
+ end
154
+
155
+ it 'should validate without errors' do
156
+ errors = Compel::Validation.validate('ab', Compel::Coercion::String, min_length: 2)
157
+
158
+ expect(errors.empty?).to eq(true)
159
+ end
160
+
161
+ end
162
+
163
+ context 'max_length' do
164
+
165
+ it 'should validate with errors' do
166
+ errors = Compel::Validation.validate(1, Compel::Coercion::Integer, max_length: 2)
167
+
168
+ expect(errors).to include('must be a string if using the max_length validation')
169
+ end
170
+
171
+ it 'should validate with errors 1' do
172
+ errors = Compel::Validation.validate('abcdef', Compel::Coercion::String, max_length: 5)
173
+
174
+ expect(errors).to include('cannot have length greater than 5')
175
+ end
176
+
177
+ it 'should validate without errors' do
178
+ errors = Compel::Validation.validate('abcde', Compel::Coercion::String, max_length: 5)
179
+
180
+ expect(errors.empty?).to eq(true)
181
+ end
182
+
183
+ end
184
+
71
185
  end
72
186
 
73
187
  end
data/spec/spec_helper.rb CHANGED
@@ -1 +1,20 @@
1
+ require 'simplecov'
2
+ require 'codeclimate-test-reporter'
3
+
4
+ SimpleCov.start do
5
+ formatter SimpleCov::Formatter::MultiFormatter[
6
+ SimpleCov::Formatter::HTMLFormatter,
7
+ CodeClimate::TestReporter::Formatter
8
+ ]
9
+ end
10
+
1
11
  require 'compel'
12
+
13
+ require 'rack/test'
14
+ require 'support/sinatra_app'
15
+
16
+ def app
17
+ SinatraApp
18
+ end
19
+
20
+ include Rack::Test::Methods
@@ -0,0 +1,43 @@
1
+ require 'sinatra/base'
2
+ require 'compel'
3
+
4
+ class SinatraApp < Sinatra::Base
5
+
6
+ set :show_exceptions, false
7
+ set :raise_errors, true
8
+
9
+ before do
10
+ content_type :json
11
+ end
12
+
13
+ helpers do
14
+
15
+ def compel(schema)
16
+ params.merge! Compel.run!(params, Compel.hash.keys(schema))
17
+ end
18
+
19
+ end
20
+
21
+ error Compel::InvalidHashError do |exception|
22
+ status 400
23
+ { errors: exception.errors }.to_json
24
+ end
25
+
26
+ configure :development do
27
+ set :show_exceptions, false
28
+ set :raise_errors, true
29
+ end
30
+
31
+ post '/api/posts' do
32
+ compel({
33
+ post: Compel.hash.keys({
34
+ title: Compel.string.required,
35
+ body: Compel.string,
36
+ published: Compel.boolean.default(false)
37
+ }).required
38
+ })
39
+
40
+ params.to_json
41
+ end
42
+
43
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joaquim Adráz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-23 00:00:00.000000000 Z
11
+ date: 2015-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie
@@ -82,6 +82,19 @@ files:
82
82
  - Rakefile
83
83
  - compel.gemspec
84
84
  - lib/compel.rb
85
+ - lib/compel/builder/boolean.rb
86
+ - lib/compel/builder/common.rb
87
+ - lib/compel/builder/common_value.rb
88
+ - lib/compel/builder/date.rb
89
+ - lib/compel/builder/datetime.rb
90
+ - lib/compel/builder/float.rb
91
+ - lib/compel/builder/hash.rb
92
+ - lib/compel/builder/integer.rb
93
+ - lib/compel/builder/json.rb
94
+ - lib/compel/builder/methods.rb
95
+ - lib/compel/builder/schema.rb
96
+ - lib/compel/builder/string.rb
97
+ - lib/compel/builder/time.rb
85
98
  - lib/compel/coercion.rb
86
99
  - lib/compel/coercion/boolean.rb
87
100
  - lib/compel/coercion/date.rb
@@ -90,24 +103,28 @@ files:
90
103
  - lib/compel/coercion/hash.rb
91
104
  - lib/compel/coercion/integer.rb
92
105
  - lib/compel/coercion/json.rb
106
+ - lib/compel/coercion/regexp.rb
93
107
  - lib/compel/coercion/string.rb
94
108
  - lib/compel/coercion/time.rb
95
109
  - lib/compel/coercion/type.rb
96
110
  - lib/compel/contract.rb
97
111
  - lib/compel/errors.rb
98
- - lib/compel/invalid_params_error.rb
99
- - lib/compel/param.rb
100
- - lib/compel/param_type_error.rb
101
- - lib/compel/param_validation_error.rb
112
+ - lib/compel/exceptions/invalid_hash_error.rb
113
+ - lib/compel/exceptions/type_error.rb
114
+ - lib/compel/exceptions/validation_error.rb
102
115
  - lib/compel/validation.rb
116
+ - lib/compel/validators/base.rb
117
+ - lib/compel/validators/hash_validator.rb
118
+ - lib/compel/validators/type_validator.rb
103
119
  - lib/compel/version.rb
120
+ - spec/compel/builder_spec.rb
104
121
  - spec/compel/coercion_spec.rb
105
122
  - spec/compel/compel_spec.rb
106
- - spec/compel/contract_spec.rb
107
123
  - spec/compel/errors_spec.rb
108
- - spec/compel/param_spec.rb
124
+ - spec/compel/sinatra_integration_spec.rb
109
125
  - spec/compel/validation_spec.rb
110
126
  - spec/spec_helper.rb
127
+ - spec/support/sinatra_app.rb
111
128
  homepage: https://github.com/joaquimadraz/compel
112
129
  licenses: []
113
130
  metadata: {}
@@ -132,10 +149,11 @@ signing_key:
132
149
  specification_version: 4
133
150
  summary: Ruby Hash Coercion and Validation
134
151
  test_files:
152
+ - spec/compel/builder_spec.rb
135
153
  - spec/compel/coercion_spec.rb
136
154
  - spec/compel/compel_spec.rb
137
- - spec/compel/contract_spec.rb
138
155
  - spec/compel/errors_spec.rb
139
- - spec/compel/param_spec.rb
156
+ - spec/compel/sinatra_integration_spec.rb
140
157
  - spec/compel/validation_spec.rb
141
158
  - spec/spec_helper.rb
159
+ - spec/support/sinatra_app.rb
@@ -1,9 +0,0 @@
1
- module Compel
2
-
3
- class InvalidParamsError < StandardError
4
-
5
- attr_accessor :params, :errors
6
-
7
- end
8
-
9
- end
data/lib/compel/param.rb DELETED
@@ -1,46 +0,0 @@
1
- module Compel
2
-
3
- class Param
4
-
5
- attr_reader :name,
6
- :type,
7
- :options,
8
- :conditions
9
-
10
- def initialize(name, type, value, options = {}, &conditions)
11
- @name = name
12
- @type = type
13
- @value = value
14
- @options = options
15
- @conditions = conditions
16
- end
17
-
18
- def value
19
- default_value = if options[:default].is_a?(Proc)
20
- options[:default].call
21
- else
22
- options[:default]
23
- end
24
-
25
- @value || default_value
26
- end
27
-
28
- def value=(value)
29
- @value = value
30
- end
31
-
32
- def required?
33
- !!@options[:required]
34
- end
35
-
36
- def hash?
37
- @type == Hash
38
- end
39
-
40
- def conditions?
41
- !!@conditions
42
- end
43
-
44
- end
45
-
46
- end
@@ -1,7 +0,0 @@
1
- module Compel
2
-
3
- class ParamTypeError < StandardError
4
-
5
- end
6
-
7
- end
@@ -1,7 +0,0 @@
1
- module Compel
2
-
3
- class ParamValidationError < StandardError
4
-
5
- end
6
-
7
- end
@@ -1,36 +0,0 @@
1
- describe Compel::Contract do
2
-
3
- context '#conditions' do
4
-
5
- it 'should build with conditions block option' do
6
- contract = \
7
- Compel::Contract.new({ first_name: 'Joaquim' }) do
8
- param :address, Hash, required: true do
9
- param :post_code, String
10
- end
11
- end
12
-
13
- expect(contract.conditions[:address].type).to eq(Hash)
14
- expect(contract.conditions[:address].required?).to eq(true)
15
- expect(contract.conditions[:address].conditions?).to eq(true)
16
- end
17
-
18
- end
19
-
20
- context 'params' do
21
-
22
- it 'should return coerced params' do
23
- contract = \
24
- Compel::Contract.new(date: '2015-12-22') { param :date, DateTime }
25
- .validate
26
-
27
- expect(contract.coerced_params.date).to be_a DateTime
28
- expect(contract.coerced_params.date.year).to eq(2015)
29
- expect(contract.coerced_params.date.month).to eq(12)
30
- expect(contract.coerced_params.date.day).to eq(22)
31
- end
32
-
33
- end
34
-
35
-
36
- end
@@ -1,25 +0,0 @@
1
- describe Compel::Param do
2
-
3
- context 'default value' do
4
-
5
- it 'should override default value when value is given' do
6
- param = Compel::Param.new(:number, Integer, nil, { default: 123 })
7
-
8
- expect(param.value).to eq(123)
9
- end
10
-
11
- it 'should use default value when not given a value' do
12
- param = Compel::Param.new(:number, Integer, 123, { default: 456 })
13
-
14
- expect(param.value).to eq(123)
15
- end
16
-
17
- it 'should allow proc as default value' do
18
- param = Compel::Param.new(:year, Integer, nil, { default: Proc.new { Time.now.year } })
19
-
20
- expect(param.value).to eq(Time.now.year)
21
- end
22
-
23
- end
24
-
25
- end