compel 0.1.1 → 0.1.2
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/README.md +30 -19
- data/lib/compel/contract.rb +9 -3
- data/lib/compel/version.rb +1 -1
- data/spec/compel/compel_spec.rb +59 -25
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68ba3b85759b0fddf7579d70d30d62f311ce365e
|
4
|
+
data.tar.gz: a250017bbdf6de848efcbdae5cd4664bd6261501
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ce4e60243142f7f0b6312c4e80ff5e09068b029c54e989fc8149f84740d62383ed952a1db685e1a97f62a2842efa221c14562d67d8692260ca76fc97f545eb8
|
7
|
+
data.tar.gz: 728d7340542f63acea00f5e40da20c90ff93b591c07f132a2a18ff8c85bca7176df4527a2d83944455d1b603bbe742f8cfacf399e8014e4bdbc8bfa61bf287a4
|
data/README.md
CHANGED
@@ -11,16 +11,6 @@ The motivation was to create an integration for [RestMyCase](https://github.com/
|
|
11
11
|
|
12
12
|
Based on the same principle from [Grape](https://github.com/ruby-grape/grape) framework and [sinatra-param](https://github.com/mattt/sinatra-param) gem to validate request params.
|
13
13
|
|
14
|
-
###Installation
|
15
|
-
|
16
|
-
Add this line to your application's Gemfile:
|
17
|
-
|
18
|
-
gem 'compel'
|
19
|
-
|
20
|
-
And then execute:
|
21
|
-
|
22
|
-
$ bundle
|
23
|
-
|
24
14
|
### Example
|
25
15
|
|
26
16
|
```ruby
|
@@ -68,15 +58,13 @@ Will return an [Hashie::Mash](https://github.com/intridea/hashie) object:
|
|
68
58
|
}
|
69
59
|
```
|
70
60
|
|
71
|
-
There are 3 ways run validations:
|
61
|
+
There are 3 ways to run validations:
|
72
62
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
- Validates and returns true or false.
|
79
|
-
|
63
|
+
Method | Behaviour
|
64
|
+
------------- | -------------
|
65
|
+
`#run` | Validates and returns an Hash with coerced params plus a `:errors` key with a _Rails like_ Hash of errors if any.
|
66
|
+
`#run!` | Validates and raises `Compel::InvalidParamsError` exception with the coerced params and errors.
|
67
|
+
`#run?` | Validates and returns true or false.
|
80
68
|
|
81
69
|
### Types
|
82
70
|
|
@@ -103,7 +91,7 @@ class App < Sinatra::Base
|
|
103
91
|
...
|
104
92
|
|
105
93
|
def compel(&block)
|
106
|
-
Compel::run!(params, &block)
|
94
|
+
params.merge! Compel::run!(params, &block)
|
107
95
|
end
|
108
96
|
|
109
97
|
error Compel::InvalidParamsError do |exception|
|
@@ -116,9 +104,32 @@ class App < Sinatra::Base
|
|
116
104
|
set :raise_errors, true
|
117
105
|
end
|
118
106
|
|
107
|
+
...
|
108
|
+
|
109
|
+
post '/api/posts' do
|
110
|
+
compel do
|
111
|
+
param :post, Hash, required: true do
|
112
|
+
param :title, String, required: true
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
puts params[:post]
|
117
|
+
end
|
118
|
+
|
119
|
+
...
|
120
|
+
|
119
121
|
end
|
120
122
|
```
|
123
|
+
###Installation
|
121
124
|
|
125
|
+
Add this line to your application's Gemfile:
|
126
|
+
|
127
|
+
gem 'compel'
|
128
|
+
|
129
|
+
And then execute:
|
130
|
+
|
131
|
+
$ bundle
|
132
|
+
|
122
133
|
### TODO
|
123
134
|
|
124
135
|
- Write more Documentation (check specs for now ;)
|
data/lib/compel/contract.rb
CHANGED
@@ -4,7 +4,6 @@ module Compel
|
|
4
4
|
|
5
5
|
attr_reader :errors,
|
6
6
|
:conditions,
|
7
|
-
:coerced_params,
|
8
7
|
:serialized_errors
|
9
8
|
|
10
9
|
def initialize(params, &block)
|
@@ -44,7 +43,7 @@ module Compel
|
|
44
43
|
# raise exception to avoid validation
|
45
44
|
|
46
45
|
# If the param value has already been coerced from digging into child Hash
|
47
|
-
# use that value instead, so we don't
|
46
|
+
# use that value instead, so we don't lose the previous coerced values
|
48
47
|
|
49
48
|
coerced_value = Coercion.coerce! \
|
50
49
|
(@coerced_params[param.name].nil? ? param.value : @coerced_params[param.name]), param.type, param.options
|
@@ -67,7 +66,12 @@ module Compel
|
|
67
66
|
|
68
67
|
def param(name, type, options = {}, &block)
|
69
68
|
@conditions[name] = \
|
70
|
-
Param.new(name, type, @params
|
69
|
+
Param.new(name, type, @params.delete(name), options, &block)
|
70
|
+
end
|
71
|
+
|
72
|
+
def coerced_params
|
73
|
+
# @params has all params that are not affected by the validation
|
74
|
+
@params.merge(@coerced_params)
|
71
75
|
end
|
72
76
|
|
73
77
|
def serialize
|
@@ -94,6 +98,8 @@ module Compel
|
|
94
98
|
|
95
99
|
raise exception, 'params are invalid'
|
96
100
|
end
|
101
|
+
|
102
|
+
coerced_params
|
97
103
|
end
|
98
104
|
|
99
105
|
end
|
data/lib/compel/version.rb
CHANGED
data/spec/compel/compel_spec.rb
CHANGED
@@ -19,7 +19,7 @@ describe Compel do
|
|
19
19
|
raise_error Compel::InvalidParamsError, 'params are invalid'
|
20
20
|
end
|
21
21
|
|
22
|
-
it '
|
22
|
+
it 'should raise InvalidParamsError exception with errors' do
|
23
23
|
params = {
|
24
24
|
first_name: 'Joaquim'
|
25
25
|
}
|
@@ -61,38 +61,66 @@ describe Compel do
|
|
61
61
|
|
62
62
|
def make_the_call(method, params)
|
63
63
|
Compel.send(method, params) do
|
64
|
-
param :
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
param :
|
64
|
+
param :user, Hash, required: true do
|
65
|
+
param :first_name, String, required: true
|
66
|
+
param :last_name, String, required: true
|
67
|
+
param :birth_date, DateTime
|
68
|
+
param :age, Integer
|
69
|
+
param :admin, Compel::Boolean
|
70
|
+
param :blog_role, Hash do
|
71
|
+
param :admin, Compel::Boolean, required: true
|
72
|
+
end
|
71
73
|
end
|
72
74
|
end
|
73
75
|
end
|
74
76
|
|
75
77
|
it 'should compel returning coerced values' do
|
76
78
|
params = {
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
79
|
+
user: {
|
80
|
+
first_name: 'Joaquim',
|
81
|
+
last_name: 'Adráz',
|
82
|
+
birth_date: '1989-08-06T09:00:00',
|
83
|
+
age: '26',
|
84
|
+
admin: 'f',
|
85
|
+
blog_role: {
|
86
|
+
admin: '0'
|
87
|
+
}
|
84
88
|
}
|
85
89
|
}
|
86
90
|
|
87
91
|
expect(make_the_call(:run, params)).to eq \
|
88
92
|
Hashie::Mash.new({
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
93
|
+
user: {
|
94
|
+
first_name: 'Joaquim',
|
95
|
+
last_name: 'Adráz',
|
96
|
+
birth_date: DateTime.parse('1989-08-06T09:00:00'),
|
97
|
+
age: 26,
|
98
|
+
admin: false,
|
99
|
+
blog_role: {
|
100
|
+
admin: false
|
101
|
+
}
|
102
|
+
}
|
103
|
+
})
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should not compel and leave other params untouched' do
|
107
|
+
params = {
|
108
|
+
other_param: 1,
|
109
|
+
user: {
|
110
|
+
first_name: 'Joaquim'
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
expect(make_the_call(:run, params)).to eq \
|
115
|
+
Hashie::Mash.new({
|
116
|
+
other_param: 1,
|
117
|
+
user: {
|
118
|
+
first_name: 'Joaquim',
|
119
|
+
},
|
120
|
+
errors: {
|
121
|
+
user: {
|
122
|
+
last_name: ['is required']
|
123
|
+
}
|
96
124
|
}
|
97
125
|
})
|
98
126
|
end
|
@@ -109,14 +137,20 @@ describe Compel do
|
|
109
137
|
|
110
138
|
it 'should not compel' do
|
111
139
|
params = {
|
112
|
-
|
140
|
+
user: {
|
141
|
+
first_name: 'Joaquim'
|
142
|
+
}
|
113
143
|
}
|
114
144
|
|
115
145
|
expect(make_the_call(:run, params)).to eq \
|
116
146
|
Hashie::Mash.new({
|
117
|
-
|
147
|
+
user:{
|
148
|
+
first_name: 'Joaquim',
|
149
|
+
},
|
118
150
|
errors: {
|
119
|
-
|
151
|
+
user: {
|
152
|
+
last_name: ['is required']
|
153
|
+
}
|
120
154
|
}
|
121
155
|
})
|
122
156
|
end
|