rack-spec 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 09c2e44eccf5df03af4ba733f212863feddc17f2
4
- data.tar.gz: c20330aae4f67a16a78157d68c5ef7f7eb9b5067
3
+ metadata.gz: 1eaf27c620c698b3f0d4648fa5e243a391aafe03
4
+ data.tar.gz: e34cca3032930bbe5912ff95ff9c526a2a30ea1e
5
5
  SHA512:
6
- metadata.gz: a1818d010e69c5bfac6dbea17970d6a5f9f117322de69d888695e24fdcf767c029320333d7c5203cdf758e2a088c1384e936c3e7cc98897dff9182356c9f08b2
7
- data.tar.gz: 77571272b0bfb76540da3507d88f8dbc89ff13d49f4d8562faa61e4d37e829e3fd30421101d8ad4b9cf1e414861af2702532a0b185c8d009ec4e14f32cf1cec3
6
+ metadata.gz: 06b6732cf5f1d56cbdf792db56540407ad8139d3431fac24472cb693238004f74ef5ed71bc82e33563619b160ee5087f358ffd081db4c4d6b1ba9df5da5162d1
7
+ data.tar.gz: 09893425824f9924f3fa23aa60821e9a8cf86b7ff2774dd0fbd1b0104f7f00a9aec07048173280dba0202443042a3a1c45465905335d6b20e198712a511056b2
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ ## v0.0.2
2
+ * Change key name: queryParameters -> parameters
3
+ * Add a new constraint: only
4
+ * Add a new constraint: minimumLength
5
+ * Add a new constraint: maxinumLength
6
+
7
+ ## v0.0.1
8
+ * Add a new constraint: type
9
+ * Add a new constraint: minimum
10
+ * Add a new constraint: maxinum
data/README.md CHANGED
@@ -12,7 +12,7 @@ require "rack"
12
12
  require "rack/spec"
13
13
  require "yaml"
14
14
 
15
- use Rack::Spec, spec: YAML.load("spec.yml")
15
+ use Rack::Spec, spec: YAML.load_file("spec.yml")
16
16
 
17
17
  run ->(env) do
18
18
  [200, {}, ["OK"]]
@@ -27,7 +27,7 @@ meta:
27
27
  endpoints:
28
28
  /recipes:
29
29
  GET:
30
- queryParameters:
30
+ parameters:
31
31
  page:
32
32
  type: integer
33
33
  minimum: 1
@@ -38,6 +38,18 @@ endpoints:
38
38
  type: float
39
39
  time:
40
40
  type: iso8601
41
+ kind:
42
+ type: string
43
+ only:
44
+ - mono
45
+ - di
46
+ - tri
47
+ POST:
48
+ parameters:
49
+ title:
50
+ type: string
51
+ minimumLength: 3
52
+ maximumLength: 10
41
53
  ```
42
54
 
43
55
  ## Development
data/lib/rack/spec.rb CHANGED
@@ -4,10 +4,13 @@ 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/maximum_length_validator"
7
8
  require "rack/spec/validators/maximum_validator"
9
+ require "rack/spec/validators/minimum_length_validator"
8
10
  require "rack/spec/validators/minimum_validator"
9
11
  require "rack/spec/validators/null_validator"
10
- require "rack/spec/validators/query_parameters_validator"
12
+ require "rack/spec/validators/only_validator"
13
+ require "rack/spec/validators/parameters_validator"
11
14
  require "rack/spec/validators/type_validator"
12
15
  require "rack/spec/validator_factory"
13
16
  require "rack/spec/version"
@@ -7,7 +7,7 @@ module Rack
7
7
  end
8
8
 
9
9
  def call(env)
10
- query_parameters_validator.validate!(env)
10
+ parameters_validator.validate!(env)
11
11
  @app.call(env)
12
12
  end
13
13
 
@@ -17,8 +17,8 @@ module Rack
17
17
  Spec.new(@options[:spec])
18
18
  end
19
19
 
20
- def query_parameters_validator
21
- @query_parameters_validator ||= Validators::QueryParametersValidator.new(spec)
20
+ def parameters_validator
21
+ @parameters_validator ||= Validators::ParametersValidator.new(spec)
22
22
  end
23
23
  end
24
24
  end
@@ -10,13 +10,16 @@ module Rack
10
10
  validator_classes[name] = klass
11
11
  end
12
12
 
13
- def build(key, type, constraint)
14
- validator_classes[type].new(key, constraint)
13
+ def build(key, type, constraint, env)
14
+ validator_classes[type].new(key, constraint, env)
15
15
  end
16
16
  end
17
17
 
18
18
  register "maximum", Validators::MaximumValidator
19
+ register "maximumLength", Validators::MaximumLengthValidator
19
20
  register "minimum", Validators::MinimumValidator
21
+ register "minimumLength", Validators::MinimumLengthValidator
22
+ register "only", Validators::OnlyValidator
20
23
  register "type", Validators::TypeValidator
21
24
  end
22
25
  end
@@ -2,20 +2,36 @@ module Rack
2
2
  class Spec
3
3
  module Validators
4
4
  class Base
5
- def initialize(key, constraint)
5
+ attr_reader :constraint, :key, :env
6
+
7
+ def initialize(key, constraint, env)
6
8
  @key = key
7
9
  @constraint = constraint
10
+ @env = env
8
11
  end
9
12
 
10
- def validate!(env)
11
- raise NotImplementedError
13
+ def validate!
14
+ unless valid?
15
+ raise ValidationError, error_message
16
+ end
12
17
  end
13
18
 
14
19
  private
15
20
 
16
- def extract_value(env)
21
+ def valid?
22
+ raise NotImplementedError
23
+ end
24
+
25
+ def error_message
26
+ raise NotImplementedError
27
+ end
28
+
29
+ def value
30
+ @value ||= request.params[@key]
31
+ end
32
+
33
+ def request
17
34
  env["rack-spec.request"] ||= Rack::Request.new(env)
18
- env["rack-spec.request"].params[@key]
19
35
  end
20
36
  end
21
37
  end
@@ -0,0 +1,17 @@
1
+ module Rack
2
+ class Spec
3
+ module Validators
4
+ class MaximumLengthValidator < Base
5
+ private
6
+
7
+ def valid?
8
+ value.nil? || value.length <= constraint
9
+ end
10
+
11
+ def error_message
12
+ "Expected #{key} to be equal or shorter than #{constraint}, but in fact #{value.inspect}"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -2,17 +2,14 @@ module Rack
2
2
  class Spec
3
3
  module Validators
4
4
  class MaximumValidator < Base
5
- def validate!(env)
6
- value = extract_value(env)
7
- if value && value.to_f > maximum
8
- raise ValidationError, "Expected #@key to be equal or less than #@maximum, but in fact #{value.inspect}"
9
- end
10
- end
11
-
12
5
  private
13
6
 
14
- def maximum
15
- @constraint
7
+ def valid?
8
+ value.nil? || value.to_f <= constraint
9
+ end
10
+
11
+ def error_message
12
+ "Expected #{key} to be equal or higher than #{constraint}, but in fact #{value.inspect}"
16
13
  end
17
14
  end
18
15
  end
@@ -0,0 +1,17 @@
1
+ module Rack
2
+ class Spec
3
+ module Validators
4
+ class MinimumLengthValidator < Base
5
+ private
6
+
7
+ def valid?
8
+ value.nil? || value.length >= constraint
9
+ end
10
+
11
+ def error_message
12
+ "Expected #{key} to be equal or longer than #{constraint}, but in fact #{value.inspect}"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -2,17 +2,14 @@ module Rack
2
2
  class Spec
3
3
  module Validators
4
4
  class MinimumValidator < Base
5
- def validate!(env)
6
- value = extract_value(env)
7
- if value && value.to_f < minimum
8
- raise ValidationError, "Expected #@key to be equal or higher than #@minimum, but in fact #{value.inspect}"
9
- end
10
- end
11
-
12
5
  private
13
6
 
14
- def minimum
15
- @constraint
7
+ def valid?
8
+ value.nil? || value.to_f >= constraint
9
+ end
10
+
11
+ def error_message
12
+ "Expected #{key} to be equal or higher than #{constraint}, but in fact #{value.inspect}"
16
13
  end
17
14
  end
18
15
  end
@@ -0,0 +1,17 @@
1
+ module Rack
2
+ class Spec
3
+ module Validators
4
+ class OnlyValidator < Base
5
+ private
6
+
7
+ def valid?
8
+ value.nil? || constraint.include?(value)
9
+ end
10
+
11
+ def error_message
12
+ "Expected #{key} to be included in #{constraint}, but in fact #{value.inspect}"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,16 +1,16 @@
1
1
  module Rack
2
2
  class Spec
3
3
  module Validators
4
- class QueryParametersValidator
4
+ class ParametersValidator
5
5
  def initialize(spec)
6
6
  @spec = spec
7
7
  end
8
8
 
9
9
  def validate!(env)
10
- parameters = @spec.reach("endpoints", env["PATH_INFO"], env["REQUEST_METHOD"], "queryParameters") || {}
10
+ parameters = @spec.reach("endpoints", env["PATH_INFO"], env["REQUEST_METHOD"], "parameters") || {}
11
11
  parameters.each do |key, hash|
12
12
  hash.each do |type, constraint|
13
- ValidatorFactory.build(key, type, constraint).validate!(env)
13
+ ValidatorFactory.build(key, type, constraint, env).validate!
14
14
  end
15
15
  end
16
16
  end
@@ -19,17 +19,18 @@ module Rack
19
19
  register "integer", /\A-?\d+\z/
20
20
  register "iso8601", ->(value) { Time.iso8601(value) rescue false }
21
21
 
22
- def validate!(env)
23
- value = extract_value(env) or return
24
- unless pattern === value
25
- raise ValidationError, "Expected #@key to be #@constraint, but in fact #{value.inspect}"
26
- end
22
+ private
23
+
24
+ def valid?
25
+ value.nil? || pattern === value
27
26
  end
28
27
 
29
- private
28
+ def error_message
29
+ "Expected #{key} to be #{constraint}, but in fact #{value.inspect}"
30
+ end
30
31
 
31
32
  def pattern
32
- self.class.patterns[@constraint]
33
+ self.class.patterns[constraint]
33
34
  end
34
35
  end
35
36
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class Spec
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -7,42 +7,43 @@ describe Rack::Spec do
7
7
  include Rack::Test::Methods
8
8
 
9
9
  let(:app) do
10
- described_class.new(original_app, spec: spec)
11
- end
12
-
13
- let(:original_app) do
14
- ->(env) do
15
- [200, {}, ["OK"]]
10
+ Rack::Builder.app do
11
+ use Rack::Spec, spec: YAML.load(<<-EOS.strip_heredoc)
12
+ meta:
13
+ baseUri: http://api.example.com/
14
+ endpoints:
15
+ /recipes:
16
+ GET:
17
+ parameters:
18
+ page:
19
+ type: integer
20
+ minimum: 1
21
+ maximum: 10
22
+ private:
23
+ type: boolean
24
+ rank:
25
+ type: float
26
+ time:
27
+ type: iso8601
28
+ kind:
29
+ type: string
30
+ only:
31
+ - mono
32
+ - di
33
+ - tri
34
+ POST:
35
+ parameters:
36
+ title:
37
+ type: string
38
+ minimumLength: 3
39
+ maximumLength: 10
40
+ EOS
41
+ run ->(env) do
42
+ [200, {}, ["OK"]]
43
+ end
16
44
  end
17
45
  end
18
46
 
19
- let(:spec) do
20
- YAML.load(yaml)
21
- end
22
-
23
- let(:yaml) do
24
- <<-EOS.strip_heredoc
25
- ---
26
- meta:
27
- baseUri: http://api.example.com/
28
-
29
- endpoints:
30
- /recipes:
31
- GET:
32
- queryParameters:
33
- page:
34
- type: integer
35
- minimum: 1
36
- maximum: 10
37
- private:
38
- type: boolean
39
- rank:
40
- type: float
41
- time:
42
- type: iso8601
43
- EOS
44
- end
45
-
46
47
  let(:path) do
47
48
  "/recipes"
48
49
  end
@@ -56,17 +57,22 @@ describe Rack::Spec do
56
57
  end
57
58
 
58
59
  subject do
59
- get path, params, env
60
+ send verb, path, params, env
60
61
  last_response.status
61
62
  end
62
63
 
63
- describe "#call" do
64
+ describe "GET" do
65
+ let(:verb) do
66
+ :get
67
+ end
68
+
64
69
  context "with valid request" do
65
70
  before do
66
71
  params[:page] = 5
67
72
  params[:private] = "false"
68
73
  params[:rank] = 2.0
69
74
  params[:time] = "2000-01-01T00:00:00+00:00"
75
+ params[:kind] = "mono"
70
76
  end
71
77
  it { should == 200 }
72
78
  end
@@ -112,5 +118,40 @@ describe Rack::Spec do
112
118
  end
113
119
  it { should == 400 }
114
120
  end
121
+
122
+ context "with query parameter invalid on only" do
123
+ before do
124
+ params[:kind] = "tetra"
125
+ end
126
+ it { should == 400 }
127
+ end
128
+ end
129
+
130
+ describe "POST" do
131
+ let(:verb) do
132
+ :post
133
+ end
134
+
135
+ context "with valid request" do
136
+ before do
137
+ params[:title] = "test"
138
+ end
139
+
140
+ it { should == 200 }
141
+ end
142
+
143
+ context "with request body parameter invalid on minimumLength" do
144
+ before do
145
+ params[:title] = "te"
146
+ end
147
+ it { should == 400 }
148
+ end
149
+
150
+ context "with request body parameter invalid on maximumLength" do
151
+ before do
152
+ params[:title] = "toooooolong"
153
+ end
154
+ it { should == 400 }
155
+ end
115
156
  end
116
157
  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.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -130,6 +130,7 @@ extensions: []
130
130
  extra_rdoc_files: []
131
131
  files:
132
132
  - .gitignore
133
+ - CHANGELOG.md
133
134
  - Gemfile
134
135
  - LICENSE.txt
135
136
  - README.md
@@ -142,10 +143,13 @@ files:
142
143
  - lib/rack/spec/validation_error.rb
143
144
  - lib/rack/spec/validator_factory.rb
144
145
  - lib/rack/spec/validators/base.rb
146
+ - lib/rack/spec/validators/maximum_length_validator.rb
145
147
  - lib/rack/spec/validators/maximum_validator.rb
148
+ - lib/rack/spec/validators/minimum_length_validator.rb
146
149
  - lib/rack/spec/validators/minimum_validator.rb
147
150
  - lib/rack/spec/validators/null_validator.rb
148
- - lib/rack/spec/validators/query_parameters_validator.rb
151
+ - lib/rack/spec/validators/only_validator.rb
152
+ - lib/rack/spec/validators/parameters_validator.rb
149
153
  - lib/rack/spec/validators/type_validator.rb
150
154
  - lib/rack/spec/version.rb
151
155
  - rack-spec.gemspec