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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1eaf27c620c698b3f0d4648fa5e243a391aafe03
4
- data.tar.gz: e34cca3032930bbe5912ff95ff9c526a2a30ea1e
3
+ metadata.gz: fa1f5b2caa1ab2a81b5a1d16ab2c2c42061ca322
4
+ data.tar.gz: 593e35231041acbdb78ede30ef3b6d05a84e31d3
5
5
  SHA512:
6
- metadata.gz: 06b6732cf5f1d56cbdf792db56540407ad8139d3431fac24472cb693238004f74ef5ed71bc82e33563619b160ee5087f358ffd081db4c4d6b1ba9df5da5162d1
7
- data.tar.gz: 09893425824f9924f3fa23aa60821e9a8cf86b7ff2774dd0fbd1b0104f7f00a9aec07048173280dba0202443042a3a1c45465905335d6b20e198712a511056b2
6
+ metadata.gz: 58f77db673d54bbe9c01a31094cfaebdceaee8bc0c25720d5f0029576f3f8548f9dcc7ad7f7b5850be6a226914c7b73d142b9771cd2837a6d3c2e7c2c2bccd32
7
+ data.tar.gz: 5fcc86a1e6aad44874f912987dca182a5784d93589691a90b407a34ab78f07c322879d8c1012ed5ca14d2a4b89838cf453b0c699fa6c7e6829d999f13a1fb19a
@@ -1,3 +1,7 @@
1
+ ## v0.0.3
2
+ * Add a new constraint: required
3
+ * More DRY way for validator definition
4
+
1
5
  ## v0.0.2
2
6
  * Change key name: queryParameters -> parameters
3
7
  * Add a new constraint: only
@@ -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(message)
7
- @message = message
6
+ def initialize(validator)
7
+ @validator = validator
8
8
  end
9
9
 
10
10
  def message
11
- @message
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: @message }.to_json
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, error_message
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,7 +2,7 @@ module Rack
2
2
  class Spec
3
3
  module Validators
4
4
  class NullValidator < Base
5
- def validate!(env)
5
+ def validate!
6
6
  end
7
7
  end
8
8
  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
@@ -0,0 +1,15 @@
1
+ module Rack
2
+ class Spec
3
+ module Validators
4
+ class RequiredValidator < Base
5
+ register_as "required"
6
+
7
+ private
8
+
9
+ def valid?
10
+ value.nil? == !constraint
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -9,15 +9,17 @@ module Rack
9
9
  @patterns ||= Hash.new(//)
10
10
  end
11
11
 
12
- def register(name, pattern)
12
+ def pattern(name, pattern)
13
13
  patterns[name] = pattern
14
14
  end
15
15
  end
16
16
 
17
- register "boolean", /\A(?:true|false)\z/
18
- register "float", /\A-?\d+(?:\.\d+)*\z/
19
- register "integer", /\A-?\d+\z/
20
- register "iso8601", ->(value) { Time.iso8601(value) rescue false }
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
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class Spec
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -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.2
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