duck-hunt 0.0.3 → 0.0.5

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.
data/README.md CHANGED
@@ -10,7 +10,7 @@ What if you could define how an object should look, and check if you're getting
10
10
  So instead of:
11
11
 
12
12
  ~~~ruby
13
- def create
13
+ def search
14
14
  unless params[:user].present? and params[:user][:name].present? and params[:user][:age].present? and params[:user][:age].is_a? Integer and params[:user][:age] > 0 # ... you get the point
15
15
  head :bad_request and return
16
16
  end
@@ -20,11 +20,11 @@ def create
20
20
  end
21
21
  ~~~
22
22
 
23
- You had:
23
+ You have:
24
24
 
25
25
  ~~~ruby
26
26
  module UserSchemas
27
- def create
27
+ def search
28
28
  DuckHunt::Schemas::HashSchema.define :strict_mode => true do |user|
29
29
  user.string "name", :required => true, :allow_nil => false
30
30
  user.integer "age", :required => true, :allow_nil => false, :greater_than => 0
@@ -37,7 +37,7 @@ module UserSchemas
37
37
  end
38
38
 
39
39
  class UserAPI
40
- def create
40
+ def search
41
41
  head :bad_request and return unless UserSchemas.create.validate?(params[:user])
42
42
 
43
43
  # the rest of your API call
@@ -426,7 +426,7 @@ schema.errors
426
426
  #=> {"0" => "not an accepted value"}
427
427
  ~~~
428
428
 
429
- #### Accepted Values
429
+ #### Rejected Values
430
430
 
431
431
  This property cannot have any of the values in this list
432
432
 
@@ -12,5 +12,7 @@ module DuckHunt
12
12
  autoload :NotDivisibleBy, File.dirname(__FILE__) + "/validators/not_divisible_by.rb"
13
13
  autoload :AcceptedValues, File.dirname(__FILE__) + "/validators/accepted_values.rb"
14
14
  autoload :RejectedValues, File.dirname(__FILE__) + "/validators/rejected_values.rb"
15
+ autoload :AllowEmpty, File.dirname(__FILE__) + "/validators/allow_empty.rb"
16
+ autoload :AllowBlank, File.dirname(__FILE__) + "/validators/allow_blank.rb"
15
17
  end
16
18
  end
@@ -0,0 +1,31 @@
1
+ module DuckHunt
2
+ module Validators
3
+ class AllowBlank < Validator
4
+ UNICODE_WHITESPACE_CHARS = [9, 10, 11, 12, 13, 32, 133, 160, 5760, 6158, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288].pack("U*")
5
+ WHITESPACE_RE = /[#{UNICODE_WHITESPACE_CHARS}]+/u
6
+ attr_reader :value
7
+ def initialize(*args)
8
+ @value = args[0]
9
+ raise ArgumentError, "a value must be provided" if @value.nil?
10
+ end
11
+
12
+ def valid?(value)
13
+ # return true if empty values are allowed
14
+ if @value
15
+ return true
16
+ else
17
+ return false if value.empty?
18
+ return value !~ /^#{WHITESPACE_RE}$/u
19
+ end
20
+ end
21
+
22
+ def error_message
23
+ if @value
24
+ return "blank values allowed"
25
+ else
26
+ return "blank values not allowed"
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ module DuckHunt
2
+ module Validators
3
+ class AllowEmpty < Validator
4
+ attr_reader :value
5
+ def initialize(*args)
6
+ @value = args[0]
7
+ raise ArgumentError, "a value must be provided" if @value.nil?
8
+ end
9
+
10
+ def valid?(value)
11
+ # return true if empty values are allowed
12
+ return true if @value
13
+ return !value.empty?
14
+ end
15
+
16
+ def error_message
17
+ if @value
18
+ return "empty values allowed"
19
+ else
20
+ return "empty values not allowed"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module DuckHunt
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -0,0 +1,71 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe DuckHunt::Validators::AllowBlank, "initialization" do
4
+ it "should create an instance with the provided value" do
5
+ validator = DuckHunt::Validators::AllowBlank.new(true)
6
+ validator.value.must_equal true
7
+ end
8
+
9
+ it "should raise an exception if a value is not provided" do
10
+ lambda{
11
+ DuckHunt::Validators::AllowBlank.new
12
+ }.must_raise ArgumentError
13
+ end
14
+ end
15
+
16
+ describe DuckHunt::Validators::AllowBlank, "Validation (allow blank)" do
17
+ before do
18
+ @validator = DuckHunt::Validators::AllowBlank.new(true)
19
+ end
20
+
21
+ it "returns true if the value provided is empty" do
22
+ @validator.valid?("").must_equal true
23
+ end
24
+
25
+ it "returns true if the value provided is only whitespace" do
26
+ @validator.valid?(" \t\t\n").must_equal true
27
+ end
28
+
29
+ it "returns true if the value provided is only weird whitespace" do
30
+ @validator.valid?("\t\n\v\f\r …  ᠎           

   ").must_equal true
31
+ end
32
+
33
+ it "returns true if the value provided is not blank" do
34
+ @validator.valid?("abcde").must_equal true
35
+ end
36
+ end
37
+
38
+ describe DuckHunt::Validators::AllowBlank, "Validation (don't allow blank)" do
39
+ before do
40
+ @validator = DuckHunt::Validators::AllowBlank.new(false)
41
+ end
42
+
43
+ it "returns false if the value provided is empty" do
44
+ @validator.valid?("").must_equal false
45
+ end
46
+
47
+ it "returns false if the value provided is only whitespace" do
48
+ @validator.valid?(" \t\t\n").must_equal false
49
+ end
50
+
51
+ it "returns false if the value provided is only weird whitespace" do
52
+ @validator.valid?("\t\n\v\f\r …  ᠎           

   ").must_equal false
53
+ end
54
+
55
+ it "returns true if the value provided is not blank" do
56
+ @validator.valid?("abcde").must_equal true
57
+ end
58
+
59
+ it "returns true if the value provided contains whitespace" do
60
+ @validator.valid?("abcde fghjik\tlmnop\t\tqrst").must_equal true
61
+ end
62
+ end
63
+
64
+
65
+
66
+ describe DuckHunt::Validators::AllowBlank, "error message" do
67
+ it "should have the correct error message based on the value provided" do
68
+ validator = DuckHunt::Validators::AllowBlank.new(false)
69
+ validator.error_message.must_equal "blank values not allowed"
70
+ end
71
+ end
@@ -0,0 +1,55 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe DuckHunt::Validators::AllowEmpty, "initialization" do
4
+ it "should create an instance with the provided value" do
5
+ validator = DuckHunt::Validators::AllowEmpty.new(true)
6
+ validator.value.must_equal true
7
+ end
8
+
9
+ it "should raise an exception if a value is not provided" do
10
+ lambda{
11
+ DuckHunt::Validators::AllowEmpty.new
12
+ }.must_raise ArgumentError
13
+ end
14
+ end
15
+
16
+ describe DuckHunt::Validators::AllowEmpty, "Validation (allow empty)" do
17
+ before do
18
+ @validator = DuckHunt::Validators::AllowEmpty.new(true)
19
+ end
20
+
21
+ it "returns true if the value provided is empty" do
22
+ @validator.valid?([]).must_equal true
23
+ @validator.valid?("").must_equal true
24
+ end
25
+
26
+ it "returns true if the value provided is not empty" do
27
+ @validator.valid?([1,2,3]).must_equal true
28
+ @validator.valid?("abcde").must_equal true
29
+ end
30
+ end
31
+
32
+ describe DuckHunt::Validators::AllowEmpty, "Validation (don't allow empty)" do
33
+ before do
34
+ @validator = DuckHunt::Validators::AllowEmpty.new(false)
35
+ end
36
+
37
+ it "returns false if the value provided is empty" do
38
+ @validator.valid?([]).must_equal false
39
+ @validator.valid?("").must_equal false
40
+ end
41
+
42
+ it "returns true if the value provided is not empty" do
43
+ @validator.valid?([1,2,3]).must_equal true
44
+ @validator.valid?("abcde").must_equal true
45
+ end
46
+ end
47
+
48
+
49
+
50
+ describe DuckHunt::Validators::AllowEmpty, "error message" do
51
+ it "should have the correct error message based on the value provided" do
52
+ validator = DuckHunt::Validators::AllowEmpty.new(false)
53
+ validator.error_message.must_equal "empty values not allowed"
54
+ end
55
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duck-hunt
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Thomas Cannon
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2014-07-12 00:00:00 -04:00
18
+ date: 2014-10-12 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -88,6 +88,8 @@ files:
88
88
  - lib/duck-hunt/schemas.rb
89
89
  - lib/duck-hunt/string_helpers.rb
90
90
  - lib/duck-hunt/validators/accepted_values.rb
91
+ - lib/duck-hunt/validators/allow_blank.rb
92
+ - lib/duck-hunt/validators/allow_empty.rb
91
93
  - lib/duck-hunt/validators/divisible_by.rb
92
94
  - lib/duck-hunt/validators/equal_to.rb
93
95
  - lib/duck-hunt/validators/greater_than.rb
@@ -121,6 +123,8 @@ files:
121
123
  - test/test_helper/test_classes.rb
122
124
  - test/test_helper.rb
123
125
  - test/validators/accepted_values_test.rb
126
+ - test/validators/allow_blank_test.rb
127
+ - test/validators/allow_empty_test.rb
124
128
  - test/validators/divisible_by_test.rb
125
129
  - test/validators/equal_to_test.rb
126
130
  - test/validators/greater_than_or_equal_to_test.rb
@@ -183,6 +187,8 @@ test_files:
183
187
  - test/test_helper/test_classes.rb
184
188
  - test/test_helper.rb
185
189
  - test/validators/accepted_values_test.rb
190
+ - test/validators/allow_blank_test.rb
191
+ - test/validators/allow_empty_test.rb
186
192
  - test/validators/divisible_by_test.rb
187
193
  - test/validators/equal_to_test.rb
188
194
  - test/validators/greater_than_or_equal_to_test.rb