rack-spec 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/rack/spec.rb +3 -1
- data/lib/rack/spec/validation_error.rb +12 -4
- data/lib/rack/spec/validator_factory.rb +0 -7
- data/lib/rack/spec/validators/base.rb +10 -1
- data/lib/rack/spec/validators/maximum_length_validator.rb +2 -4
- data/lib/rack/spec/validators/maximum_validator.rb +2 -4
- data/lib/rack/spec/validators/minimum_length_validator.rb +2 -4
- data/lib/rack/spec/validators/minimum_validator.rb +2 -4
- data/lib/rack/spec/validators/null_validator.rb +1 -1
- data/lib/rack/spec/validators/only_validator.rb +2 -4
- data/lib/rack/spec/validators/required_validator.rb +15 -0
- data/lib/rack/spec/validators/type_validator.rb +7 -9
- data/lib/rack/spec/version.rb +1 -1
- data/spec/rack/spec_spec.rb +12 -4
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa1f5b2caa1ab2a81b5a1d16ab2c2c42061ca322
|
4
|
+
data.tar.gz: 593e35231041acbdb78ede30ef3b6d05a84e31d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58f77db673d54bbe9c01a31094cfaebdceaee8bc0c25720d5f0029576f3f8548f9dcc7ad7f7b5850be6a226914c7b73d142b9771cd2837a6d3c2e7c2c2bccd32
|
7
|
+
data.tar.gz: 5fcc86a1e6aad44874f912987dca182a5784d93589691a90b407a34ab78f07c322879d8c1012ed5ca14d2a4b89838cf453b0c699fa6c7e6829d999f13a1fb19a
|
data/CHANGELOG.md
CHANGED
data/lib/rack/spec.rb
CHANGED
@@ -4,6 +4,8 @@ require "rack/spec/spec"
|
|
4
4
|
require "rack/spec/validation"
|
5
5
|
require "rack/spec/validation_error"
|
6
6
|
require "rack/spec/validators/base"
|
7
|
+
require "rack/spec/validators/null_validator"
|
8
|
+
require "rack/spec/validator_factory"
|
7
9
|
require "rack/spec/validators/maximum_length_validator"
|
8
10
|
require "rack/spec/validators/maximum_validator"
|
9
11
|
require "rack/spec/validators/minimum_length_validator"
|
@@ -11,8 +13,8 @@ require "rack/spec/validators/minimum_validator"
|
|
11
13
|
require "rack/spec/validators/null_validator"
|
12
14
|
require "rack/spec/validators/only_validator"
|
13
15
|
require "rack/spec/validators/parameters_validator"
|
16
|
+
require "rack/spec/validators/required_validator"
|
14
17
|
require "rack/spec/validators/type_validator"
|
15
|
-
require "rack/spec/validator_factory"
|
16
18
|
require "rack/spec/version"
|
17
19
|
|
18
20
|
module Rack
|
@@ -3,12 +3,12 @@ require "json"
|
|
3
3
|
module Rack
|
4
4
|
class Spec
|
5
5
|
class ValidationError < StandardError
|
6
|
-
def initialize(
|
7
|
-
@
|
6
|
+
def initialize(validator)
|
7
|
+
@validator = validator
|
8
8
|
end
|
9
9
|
|
10
10
|
def message
|
11
|
-
|
11
|
+
"Invalid #{key} on `#{constraint_name}` constraint"
|
12
12
|
end
|
13
13
|
|
14
14
|
def to_rack_response
|
@@ -17,6 +17,14 @@ module Rack
|
|
17
17
|
|
18
18
|
private
|
19
19
|
|
20
|
+
def constraint_name
|
21
|
+
@validator.class.registered_name
|
22
|
+
end
|
23
|
+
|
24
|
+
def key
|
25
|
+
@validator.key
|
26
|
+
end
|
27
|
+
|
20
28
|
def status
|
21
29
|
400
|
22
30
|
end
|
@@ -26,7 +34,7 @@ module Rack
|
|
26
34
|
end
|
27
35
|
|
28
36
|
def body
|
29
|
-
{ message:
|
37
|
+
{ message: message }.to_json
|
30
38
|
end
|
31
39
|
end
|
32
40
|
end
|
@@ -14,13 +14,6 @@ module Rack
|
|
14
14
|
validator_classes[type].new(key, constraint, env)
|
15
15
|
end
|
16
16
|
end
|
17
|
-
|
18
|
-
register "maximum", Validators::MaximumValidator
|
19
|
-
register "maximumLength", Validators::MaximumLengthValidator
|
20
|
-
register "minimum", Validators::MinimumValidator
|
21
|
-
register "minimumLength", Validators::MinimumLengthValidator
|
22
|
-
register "only", Validators::OnlyValidator
|
23
|
-
register "type", Validators::TypeValidator
|
24
17
|
end
|
25
18
|
end
|
26
19
|
end
|
@@ -2,6 +2,15 @@ module Rack
|
|
2
2
|
class Spec
|
3
3
|
module Validators
|
4
4
|
class Base
|
5
|
+
class << self
|
6
|
+
attr_accessor :registered_name
|
7
|
+
|
8
|
+
def register_as(name)
|
9
|
+
self.registered_name = name
|
10
|
+
ValidatorFactory.register(name, self)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
5
14
|
attr_reader :constraint, :key, :env
|
6
15
|
|
7
16
|
def initialize(key, constraint, env)
|
@@ -12,7 +21,7 @@ module Rack
|
|
12
21
|
|
13
22
|
def validate!
|
14
23
|
unless valid?
|
15
|
-
raise ValidationError,
|
24
|
+
raise ValidationError, self
|
16
25
|
end
|
17
26
|
end
|
18
27
|
|
@@ -2,15 +2,13 @@ module Rack
|
|
2
2
|
class Spec
|
3
3
|
module Validators
|
4
4
|
class MaximumLengthValidator < Base
|
5
|
+
register_as "maximumLength"
|
6
|
+
|
5
7
|
private
|
6
8
|
|
7
9
|
def valid?
|
8
10
|
value.nil? || value.length <= constraint
|
9
11
|
end
|
10
|
-
|
11
|
-
def error_message
|
12
|
-
"Expected #{key} to be equal or shorter than #{constraint}, but in fact #{value.inspect}"
|
13
|
-
end
|
14
12
|
end
|
15
13
|
end
|
16
14
|
end
|
@@ -2,15 +2,13 @@ module Rack
|
|
2
2
|
class Spec
|
3
3
|
module Validators
|
4
4
|
class MaximumValidator < Base
|
5
|
+
register_as "maximum"
|
6
|
+
|
5
7
|
private
|
6
8
|
|
7
9
|
def valid?
|
8
10
|
value.nil? || value.to_f <= constraint
|
9
11
|
end
|
10
|
-
|
11
|
-
def error_message
|
12
|
-
"Expected #{key} to be equal or higher than #{constraint}, but in fact #{value.inspect}"
|
13
|
-
end
|
14
12
|
end
|
15
13
|
end
|
16
14
|
end
|
@@ -2,15 +2,13 @@ module Rack
|
|
2
2
|
class Spec
|
3
3
|
module Validators
|
4
4
|
class MinimumLengthValidator < Base
|
5
|
+
register_as "minimumLength"
|
6
|
+
|
5
7
|
private
|
6
8
|
|
7
9
|
def valid?
|
8
10
|
value.nil? || value.length >= constraint
|
9
11
|
end
|
10
|
-
|
11
|
-
def error_message
|
12
|
-
"Expected #{key} to be equal or longer than #{constraint}, but in fact #{value.inspect}"
|
13
|
-
end
|
14
12
|
end
|
15
13
|
end
|
16
14
|
end
|
@@ -2,15 +2,13 @@ module Rack
|
|
2
2
|
class Spec
|
3
3
|
module Validators
|
4
4
|
class MinimumValidator < Base
|
5
|
+
register_as "minimum"
|
6
|
+
|
5
7
|
private
|
6
8
|
|
7
9
|
def valid?
|
8
10
|
value.nil? || value.to_f >= constraint
|
9
11
|
end
|
10
|
-
|
11
|
-
def error_message
|
12
|
-
"Expected #{key} to be equal or higher than #{constraint}, but in fact #{value.inspect}"
|
13
|
-
end
|
14
12
|
end
|
15
13
|
end
|
16
14
|
end
|
@@ -2,15 +2,13 @@ module Rack
|
|
2
2
|
class Spec
|
3
3
|
module Validators
|
4
4
|
class OnlyValidator < Base
|
5
|
+
register_as "only"
|
6
|
+
|
5
7
|
private
|
6
8
|
|
7
9
|
def valid?
|
8
10
|
value.nil? || constraint.include?(value)
|
9
11
|
end
|
10
|
-
|
11
|
-
def error_message
|
12
|
-
"Expected #{key} to be included in #{constraint}, but in fact #{value.inspect}"
|
13
|
-
end
|
14
12
|
end
|
15
13
|
end
|
16
14
|
end
|
@@ -9,15 +9,17 @@ module Rack
|
|
9
9
|
@patterns ||= Hash.new(//)
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
12
|
+
def pattern(name, pattern)
|
13
13
|
patterns[name] = pattern
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
register_as "type"
|
18
|
+
|
19
|
+
pattern "boolean", /\A(?:true|false)\z/
|
20
|
+
pattern "float", /\A-?\d+(?:\.\d+)*\z/
|
21
|
+
pattern "integer", /\A-?\d+\z/
|
22
|
+
pattern "iso8601", ->(value) { Time.iso8601(value) rescue false }
|
21
23
|
|
22
24
|
private
|
23
25
|
|
@@ -25,10 +27,6 @@ module Rack
|
|
25
27
|
value.nil? || pattern === value
|
26
28
|
end
|
27
29
|
|
28
|
-
def error_message
|
29
|
-
"Expected #{key} to be #{constraint}, but in fact #{value.inspect}"
|
30
|
-
end
|
31
|
-
|
32
30
|
def pattern
|
33
31
|
self.class.patterns[constraint]
|
34
32
|
end
|
data/lib/rack/spec/version.rb
CHANGED
data/spec/rack/spec_spec.rb
CHANGED
@@ -37,6 +37,7 @@ describe Rack::Spec do
|
|
37
37
|
type: string
|
38
38
|
minimumLength: 3
|
39
39
|
maximumLength: 10
|
40
|
+
required: true
|
40
41
|
EOS
|
41
42
|
run ->(env) do
|
42
43
|
[200, {}, ["OK"]]
|
@@ -128,15 +129,15 @@ describe Rack::Spec do
|
|
128
129
|
end
|
129
130
|
|
130
131
|
describe "POST" do
|
132
|
+
before do
|
133
|
+
params[:title] = "test"
|
134
|
+
end
|
135
|
+
|
131
136
|
let(:verb) do
|
132
137
|
:post
|
133
138
|
end
|
134
139
|
|
135
140
|
context "with valid request" do
|
136
|
-
before do
|
137
|
-
params[:title] = "test"
|
138
|
-
end
|
139
|
-
|
140
141
|
it { should == 200 }
|
141
142
|
end
|
142
143
|
|
@@ -153,5 +154,12 @@ describe Rack::Spec do
|
|
153
154
|
end
|
154
155
|
it { should == 400 }
|
155
156
|
end
|
157
|
+
|
158
|
+
context "with request body parameter invalid on required" do
|
159
|
+
before do
|
160
|
+
params.delete(:title)
|
161
|
+
end
|
162
|
+
it { should == 400 }
|
163
|
+
end
|
156
164
|
end
|
157
165
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-spec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
@@ -150,6 +150,7 @@ files:
|
|
150
150
|
- lib/rack/spec/validators/null_validator.rb
|
151
151
|
- lib/rack/spec/validators/only_validator.rb
|
152
152
|
- lib/rack/spec/validators/parameters_validator.rb
|
153
|
+
- lib/rack/spec/validators/required_validator.rb
|
153
154
|
- lib/rack/spec/validators/type_validator.rb
|
154
155
|
- lib/rack/spec/version.rb
|
155
156
|
- rack-spec.gemspec
|