rest_my_case 1.9.3 → 1.9.4
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/lib/rest_my_case/accusation_attorneys/base.rb +4 -0
- data/lib/rest_my_case/accusation_attorneys/each.rb +4 -0
- data/lib/rest_my_case/accusation_attorneys/errors.rb +4 -5
- data/lib/rest_my_case/accusation_attorneys/format.rb +4 -0
- data/lib/rest_my_case/accusation_attorneys/length.rb +4 -0
- data/lib/rest_my_case/accusation_attorneys/numericality.rb +4 -0
- data/lib/rest_my_case/accusation_attorneys/presence.rb +4 -0
- data/lib/rest_my_case/context/http_status.rb +6 -0
- data/lib/rest_my_case/validator.rb +4 -4
- data/lib/rest_my_case/version.rb +1 -1
- data/spec/rest_my_case/accusation_attorneys/format_spec.rb +71 -0
- data/spec/rest_my_case/accusation_attorneys/length_spec.rb +46 -0
- data/spec/rest_my_case/accusation_attorneys/numericality_spec.rb +126 -0
- data/spec/rest_my_case/http_status_spec.rb +30 -16
- data/spec/rest_my_case/validator_spec.rb +162 -8
- data/spec/support/validator/models/ruby_post.rb +5 -1
- data/spec/support/validator/models/ruby_user.rb +9 -0
- data/spec/support/validator/usecases/custom_validator.rb +5 -1
- data/spec/support/validator/usecases/hierarchy_validation.rb +8 -2
- data/spec/support/validator/usecases/length_validator.rb +19 -0
- data/spec/support/validator/usecases/numericality_validator.rb +13 -0
- metadata +14 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2724bb942b9966ddca16a313cd92a21159752885
|
|
4
|
+
data.tar.gz: 2700a310ca133fadaca6673e2be21cdacea3a938
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d80ea3c0c01816e07a9a7f0bb7ca3d54796a14cc07e42635945fee101435a873f9c44356c271392ee0d9fb374a65f566fe68c14cecd6809caf4868074d0060f1
|
|
7
|
+
data.tar.gz: bcdabecbadd83fe88bc1b7082852fcf678d9b990e07cd2f8f0f62d75e4ab04b0ecbe2b4bd57f1dd0a308c5416231ad6467fe84259ea1d0591d7af208d27c8058
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
module RestMyCase
|
|
2
2
|
module AccusationAttorneys
|
|
3
3
|
|
|
4
|
+
# I DO NOT CLAIM OWNERSHIP OF THIS CODE, THIS CODE WAS TAKEN
|
|
5
|
+
# FROM "ActiveModel" GEM AND ADAPTED TO RUN WITHOUT "ActiveSupport"
|
|
6
|
+
# ORIGINAL SOURCE FILE: ActiveModel::Validator
|
|
7
|
+
|
|
4
8
|
class Base
|
|
5
9
|
|
|
6
10
|
attr_accessor :base
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
module RestMyCase
|
|
2
2
|
module AccusationAttorneys
|
|
3
3
|
|
|
4
|
+
# I DO NOT CLAIM OWNERSHIP OF THIS CODE, THIS CODE WAS TAKEN
|
|
5
|
+
# FROM "ActiveModel" GEM AND ADAPTED TO RUN WITHOUT "ActiveSupport"
|
|
6
|
+
# ORIGINAL SOURCE FILE: ActiveModel::EachValidator
|
|
7
|
+
|
|
4
8
|
class Each < Base
|
|
5
9
|
|
|
6
10
|
attr_reader :attributes
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
module RestMyCase
|
|
2
2
|
module AccusationAttorneys
|
|
3
3
|
|
|
4
|
+
# I DO NOT CLAIM OWNERSHIP OF THIS CODE, THIS CODE WAS TAKEN
|
|
5
|
+
# FROM "ActiveModel" GEM AND ADAPTED TO RUN WITHOUT "ActiveSupport"
|
|
6
|
+
# ORIGINAL SOURCE FILE: ActiveModel::Errors
|
|
7
|
+
|
|
4
8
|
class Errors
|
|
5
9
|
include Enumerable
|
|
6
10
|
|
|
@@ -20,11 +24,6 @@ module RestMyCase
|
|
|
20
24
|
@messages = {}
|
|
21
25
|
end
|
|
22
26
|
|
|
23
|
-
def initialize_dup(other) # :nodoc:
|
|
24
|
-
@messages = other.messages.dup
|
|
25
|
-
super
|
|
26
|
-
end
|
|
27
|
-
|
|
28
27
|
# Clear the error messages.
|
|
29
28
|
#
|
|
30
29
|
# person.errors.full_messages # => ["name can not be nil"]
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
module RestMyCase
|
|
2
2
|
module AccusationAttorneys
|
|
3
3
|
|
|
4
|
+
# I DO NOT CLAIM OWNERSHIP OF THIS CODE, THIS CODE WAS TAKEN
|
|
5
|
+
# FROM "ActiveModel" GEM AND ADAPTED TO RUN WITHOUT "ActiveSupport"
|
|
6
|
+
# ORIGINAL SOURCE FILE: ActiveModel::Validations::FormatValidator
|
|
7
|
+
|
|
4
8
|
class Format < Each
|
|
5
9
|
def validate_each(record, attribute, value)
|
|
6
10
|
if options[:with]
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
module RestMyCase
|
|
2
2
|
module AccusationAttorneys
|
|
3
3
|
|
|
4
|
+
# I DO NOT CLAIM OWNERSHIP OF THIS CODE, THIS CODE WAS TAKEN
|
|
5
|
+
# FROM "ActiveModel" GEM AND ADAPTED TO RUN WITHOUT "ActiveSupport"
|
|
6
|
+
# ORIGINAL SOURCE FILE: ActiveModel::Validations::LengthValidator
|
|
7
|
+
|
|
4
8
|
class Length < Each
|
|
5
9
|
MESSAGES = { is: :wrong_length, minimum: :too_short, maximum: :too_long }.freeze
|
|
6
10
|
CHECKS = { is: :==, minimum: :>=, maximum: :<= }.freeze
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
module RestMyCase
|
|
2
2
|
module AccusationAttorneys
|
|
3
3
|
|
|
4
|
+
# I DO NOT CLAIM OWNERSHIP OF THIS CODE, THIS CODE WAS TAKEN
|
|
5
|
+
# FROM "ActiveModel" GEM AND ADAPTED TO RUN WITHOUT "ActiveSupport"
|
|
6
|
+
# ORIGINAL SOURCE FILE: ActiveModel::Validations::NumericalityValidator
|
|
7
|
+
|
|
4
8
|
class Numericality < Each
|
|
5
9
|
CHECKS = { :greater_than => :>, :greater_than_or_equal_to => :>=,
|
|
6
10
|
:equal_to => :==, :less_than => :<, :less_than_or_equal_to => :<=,
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
module RestMyCase
|
|
2
2
|
module AccusationAttorneys
|
|
3
3
|
|
|
4
|
+
# I DO NOT CLAIM OWNERSHIP OF THIS CODE, THIS CODE WAS TAKEN
|
|
5
|
+
# FROM "ActiveModel" GEM AND ADAPTED TO RUN WITHOUT "ActiveSupport"
|
|
6
|
+
# ORIGINAL SOURCE FILE: ActiveModel::Validations::PresenceValidator
|
|
7
|
+
|
|
4
8
|
class Presence < Each
|
|
5
9
|
def validate_each(record, attr_name, value)
|
|
6
10
|
record.errors.add(attr_name, :blank, options) if Helpers.blank?(value)
|
|
@@ -86,10 +86,12 @@ module RestMyCase
|
|
|
86
86
|
|
|
87
87
|
def all_validations_green?(targets)
|
|
88
88
|
targets.map do |object_to_validate|
|
|
89
|
+
extend_errors_if_necessary(object_to_validate)
|
|
90
|
+
|
|
89
91
|
if Helpers.marked_for_destruction?(object_to_validate)
|
|
90
92
|
true
|
|
91
93
|
else
|
|
92
|
-
|
|
94
|
+
run_validations(object_to_validate)
|
|
93
95
|
|
|
94
96
|
object_to_validate.errors.empty?
|
|
95
97
|
end
|
|
@@ -98,9 +100,7 @@ module RestMyCase
|
|
|
98
100
|
|
|
99
101
|
private ########################### PRIVATE ################################
|
|
100
102
|
|
|
101
|
-
def
|
|
102
|
-
extend_errors_if_necessary object_to_validate
|
|
103
|
-
|
|
103
|
+
def run_validations(object_to_validate)
|
|
104
104
|
self.class.validators.values.flatten.uniq.each do |validator|
|
|
105
105
|
next if validator_condition_fails(validator, object_to_validate)
|
|
106
106
|
|
data/lib/rest_my_case/version.rb
CHANGED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe RestMyCase::AccusationAttorneys::Format do
|
|
4
|
+
|
|
5
|
+
describe "with options, 'with'" do
|
|
6
|
+
FormatValidator1 = Class.new(RestMyCase::Validator) do
|
|
7
|
+
target :post
|
|
8
|
+
|
|
9
|
+
validates_format_of :body, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
context "given a resource with a valid body" do
|
|
13
|
+
before do
|
|
14
|
+
@post = RubyPost.new(body: 'email@gmail.com')
|
|
15
|
+
@context = FormatValidator1.perform(post: @post)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it '@post should have no errors' do
|
|
19
|
+
expect(@post.errors.empty?).to be true
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
context "given a resource with an invalid body" do
|
|
24
|
+
before do
|
|
25
|
+
@post = RubyPost.new(body: 'emailmail.com')
|
|
26
|
+
@context = FormatValidator1.perform(post: @post)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it '@post should have an error' do
|
|
30
|
+
expect(@post.errors.include?(:body)).to be true
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
describe "with options, 'without'" do
|
|
36
|
+
FormatValidator2 = Class.new(RestMyCase::Validator) do
|
|
37
|
+
target :post
|
|
38
|
+
|
|
39
|
+
validates_format_of :body, without: /@/i
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
context "given a resource with a valid body" do
|
|
43
|
+
before do
|
|
44
|
+
@post = RubyPost.new(body: 'emailmailcom')
|
|
45
|
+
@context = FormatValidator2.perform(post: @post)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it '@post should have no errors' do
|
|
49
|
+
expect(@post.errors.empty?).to be true
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
context "given a resource with an invalid body" do
|
|
54
|
+
before do
|
|
55
|
+
@post = RubyPost.new(body: 'email@gmailcom')
|
|
56
|
+
@context = FormatValidator2.perform(post: @post)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it '@post should have an error' do
|
|
60
|
+
expect(@post.errors.include?(:body)).to be true
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
describe "without options" do
|
|
66
|
+
it 'should raise an error' do
|
|
67
|
+
expect { Class.new(RestMyCase::Validator) { validates_format_of :body } }.to raise_error
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe RestMyCase::AccusationAttorneys::Length do
|
|
4
|
+
|
|
5
|
+
describe "with options, 'with'" do
|
|
6
|
+
context "given a blank user" do
|
|
7
|
+
before do
|
|
8
|
+
@user = RubyUser.new
|
|
9
|
+
@context = LengthValidator.perform(user: @user)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it '@user should contain errors' do
|
|
13
|
+
expect(@user.errors.messages).to match a_hash_including({:user_name=>["pick a longer name"], :zip_code=>["please enter at least 5 characters"], :smurf_leader=>["papa is spelled with 4 characters... don't play me."]})
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
context "given a user with first_name and last_name > then 5 chars, fax = '', user_name > 7 chars, zip_code = 12345, smurf_leader = 'papas'" do
|
|
18
|
+
before do
|
|
19
|
+
@user = RubyUser.new(first_name: '123456', last_name: '123456', fax: '', phone: '', user_name: '12345678', zip_code: '12345', smurf_leader: 'papas')
|
|
20
|
+
@context = LengthValidator.perform(user: @user)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it '@user should contain errors' do
|
|
24
|
+
expect(@user.errors.messages).to match a_hash_including({:first_name=>[:too_long], :last_name=>["less than 5 if you don't mind"], :fax=>[:too_short], :user_name=>["pick a shorter name"], :smurf_leader=>["papa is spelled with 4 characters... don't play me."]})
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
context "change the user's attributes for valid ones, except the phone" do
|
|
28
|
+
before do
|
|
29
|
+
@user.errors.clear
|
|
30
|
+
@user.first_name = @user.last_name = '12345'
|
|
31
|
+
@user.fax = '12345678'
|
|
32
|
+
@user.phone = '123456789'
|
|
33
|
+
@user.user_name = '1234567'
|
|
34
|
+
@user.smurf_leader = 'papa'
|
|
35
|
+
@context = LengthValidator.perform(user: @user)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it '@user should contain errors' do
|
|
39
|
+
expect(@user.errors.messages).to match a_hash_including({:phone=>[:too_long]})
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
end
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe RestMyCase::AccusationAttorneys::Numericality do
|
|
4
|
+
|
|
5
|
+
describe "without options" do
|
|
6
|
+
context "given a resource with an integer phone" do
|
|
7
|
+
before do
|
|
8
|
+
@user = RubyUser.new(phone: 2, fax: 11)
|
|
9
|
+
@context = NumericalityValidator.perform(user: @user)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it '@user should have no errors' do
|
|
13
|
+
expect(@user.errors.empty?).to be true
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
context "given a fax smaller than a phone" do
|
|
18
|
+
before do
|
|
19
|
+
@user = RubyUser.new(phone: 10, fax: 2, validate_fax: true)
|
|
20
|
+
@context = NumericalityValidator.perform(user: @user)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it '@user should have a fax error' do
|
|
24
|
+
expect(@user.errors.count).to be 3
|
|
25
|
+
expect(@user.errors.messages).to match a_hash_including({:phone=>["should be less than fax"], :fax=>["should be greater than phone", "should be odd"]})
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
context "given a resource with a float phone" do
|
|
30
|
+
before do
|
|
31
|
+
@user = RubyUser.new(phone: 2.5, fax: 11)
|
|
32
|
+
@context = NumericalityValidator.perform(user: @user)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it '@user should have no errors' do
|
|
36
|
+
expect(@user.errors.empty?).to be true
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
context "given a resource with a string phone but integer nonetheless" do
|
|
41
|
+
before do
|
|
42
|
+
@user = RubyUser.new(phone: "2", fax: 11)
|
|
43
|
+
@context = NumericalityValidator.perform(user: @user)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it '@user should have no errors' do
|
|
47
|
+
expect(@user.errors.empty?).to be true
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
context "given a resource with a string phone but float nonetheless" do
|
|
52
|
+
before do
|
|
53
|
+
@user = RubyUser.new(phone: "2.5", fax: 11)
|
|
54
|
+
@context = NumericalityValidator.perform(user: @user)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it '@user should have no errors' do
|
|
58
|
+
expect(@user.errors.empty?).to be true
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
context "given a resource with a string phone but not numeric" do
|
|
63
|
+
before do
|
|
64
|
+
@user = RubyUser.new(phone: "2.s", fax: 11)
|
|
65
|
+
@context = NumericalityValidator.perform(user: @user)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it '@user should have an error' do
|
|
69
|
+
expect(@user.errors.include?(:phone)).to be true
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
describe "only_integer" do
|
|
75
|
+
NumericalityValidator2 = Class.new(RestMyCase::Validator) do
|
|
76
|
+
target :user
|
|
77
|
+
|
|
78
|
+
validates_numericality_of :phone, only_integer: true
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
context "given a resource with an integer phone" do
|
|
82
|
+
before do
|
|
83
|
+
@user = RubyUser.new(phone: 2, fax: 11)
|
|
84
|
+
@context = NumericalityValidator.perform(user: @user)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it '@user should have no errors' do
|
|
88
|
+
expect(@user.errors.empty?).to be true
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
context "given a resource with a float phone" do
|
|
93
|
+
before do
|
|
94
|
+
@user = RubyUser.new(phone: 2.5, fax: 11)
|
|
95
|
+
@context = NumericalityValidator2.perform(user: @user)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it '@user should have an error' do
|
|
99
|
+
expect(@user.errors.include?(:phone)).to be true
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
context "given a resource with a string phone but float nonetheless" do
|
|
104
|
+
before do
|
|
105
|
+
@user = RubyUser.new(phone: "2.5", fax: 11)
|
|
106
|
+
@context = NumericalityValidator2.perform(user: @user)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it '@user should have an error' do
|
|
110
|
+
expect(@user.errors.include?(:phone)).to be true
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
context "given a resource with a string phone but not numeric" do
|
|
115
|
+
before do
|
|
116
|
+
@user = RubyUser.new(phone: "2.s", fax: 11)
|
|
117
|
+
@context = NumericalityValidator.perform(user: @user)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
it '@user should have an error' do
|
|
121
|
+
expect(@user.errors.include?(:phone)).to be true
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
end
|
|
@@ -2,32 +2,46 @@ require 'spec_helper'
|
|
|
2
2
|
|
|
3
3
|
describe RestMyCase::HttpStatus do
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
context "when status.not_found! is used" do
|
|
6
|
+
NotFound = Class.new(RestMyCase::HttpStatus) { def perform; status.not_found!; end }
|
|
6
7
|
|
|
7
|
-
context
|
|
8
|
-
NotFound = Class.new(RestMyCase::HttpStatus) { def perform; status.not_found!; end }
|
|
8
|
+
before { @context = NotFound.perform }
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
end
|
|
10
|
+
it "@context.http_status should only list the class's dependencies" do
|
|
11
|
+
expect(@context.http_status).to be 404
|
|
13
12
|
end
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
it "@context.error_message should only list the class's dependencies" do
|
|
15
|
+
expect(@context.error_message).to match a_hash_including({:message=>"unknown error"})
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
context "when failure(:unprocessable_entity) is used" do
|
|
20
|
+
UnprocessableEntity = Class.new(RestMyCase::HttpStatus) { def perform; failure(:unprocessable_entity); end }
|
|
17
21
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
before { @context = UnprocessableEntity.perform }
|
|
23
|
+
|
|
24
|
+
it "@context.http_status should only list the class's dependencies" do
|
|
25
|
+
expect(@context.http_status).to be 422
|
|
21
26
|
end
|
|
22
27
|
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
it "@context.error_message should only list the class's dependencies" do
|
|
29
|
+
expect(@context.error_message).to match a_hash_including({:message=>"unprocessable_entity"})
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
context "when failure!(:internal_server_error) is used" do
|
|
34
|
+
InternalServerError = Class.new(RestMyCase::HttpStatus) { def perform; failure!(:internal_server_error, 'failed to save'); end }
|
|
25
35
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
36
|
+
before { @context = InternalServerError.perform }
|
|
37
|
+
|
|
38
|
+
it "@context.http_status should only list the class's dependencies" do
|
|
39
|
+
expect(@context.http_status).to be 500
|
|
29
40
|
end
|
|
30
41
|
|
|
42
|
+
it "@context.error_message should only list the class's dependencies" do
|
|
43
|
+
expect(@context.error_message).to match a_hash_including({:message=>"internal_server_error - failed to save"})
|
|
44
|
+
end
|
|
31
45
|
end
|
|
32
46
|
|
|
33
47
|
end
|
|
@@ -46,21 +46,175 @@ describe RestMyCase::Validator do
|
|
|
46
46
|
expect(@context.ok?).to be true
|
|
47
47
|
end
|
|
48
48
|
end
|
|
49
|
+
|
|
50
|
+
context "given a post with an invalid phone_number and a context preventing the validation from running" do
|
|
51
|
+
before do
|
|
52
|
+
@post = RubyPost.new(phone_number: 'asd')
|
|
53
|
+
@context = CustomValidator.perform(post: @post, should_not_validate_country_code: true)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "@context.ok? should be true" do
|
|
57
|
+
expect(@context.ok?).to be true
|
|
58
|
+
end
|
|
59
|
+
end
|
|
49
60
|
end
|
|
50
61
|
|
|
51
62
|
context "Given a class that inherits from a class that has its own dependencies" do
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
63
|
+
context "given an empty post" do
|
|
64
|
+
before do
|
|
65
|
+
@post = RubyPost.new
|
|
66
|
+
@context = HierarchyValidation::Son.perform(post: @post)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it "HierarchyValidation::Son should be able to inherit Father's validations and alter them" do
|
|
70
|
+
expect(@context.ok?).to be false
|
|
71
|
+
expect(@post.errors.added?(:title, :blank)).to be true
|
|
72
|
+
expect(@post.errors.added?(:body, :blank)).to be true
|
|
73
|
+
expect(@post.errors.size).to be 2
|
|
74
|
+
end
|
|
55
75
|
end
|
|
56
76
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
77
|
+
context "given an empty post and a context preventing the title validation" do
|
|
78
|
+
before do
|
|
79
|
+
@post = RubyPost.new(ignore_title: true)
|
|
80
|
+
@context = HierarchyValidation::Son.perform(post: @post)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it "HierarchyValidation::Son should only show the body error" do
|
|
84
|
+
expect(@context.ok?).to be false
|
|
85
|
+
expect(@post.errors.added?(:title, :blank)).to be false
|
|
86
|
+
expect(@post.errors.added?(:body, :blank)).to be true
|
|
87
|
+
expect(@post.errors.size).to be 1
|
|
88
|
+
end
|
|
62
89
|
end
|
|
63
90
|
end
|
|
64
91
|
|
|
92
|
+
context "When passing an array with 2 resources" do
|
|
93
|
+
|
|
94
|
+
context "and both of them are valid" do
|
|
95
|
+
before do
|
|
96
|
+
@post1 = RubyPost.new(title: 'title', body: 'body')
|
|
97
|
+
@post2 = RubyPost.new(title: 'title', body: 'body')
|
|
98
|
+
@context = HierarchyValidation::GrandSon.perform(posts: [@post1, @post2])
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
it "@context.ok? should be true" do
|
|
102
|
+
expect(@context.ok?).to be true
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it "@post1 should not cotain errors" do
|
|
106
|
+
expect(@post1.errors.count).to be 0
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it "@post2 should not cotain errors" do
|
|
110
|
+
expect(@post2.errors.count).to be 0
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
context "and one of them is invalid" do
|
|
115
|
+
before do
|
|
116
|
+
@post1 = RubyPost.new(title: 'title', body: 'body')
|
|
117
|
+
@post2 = RubyPost.new(title: 'title')
|
|
118
|
+
@context = HierarchyValidation::GrandSon.perform(posts: [@post1, @post2])
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
it "@context.ok? should be false" do
|
|
122
|
+
expect(@context.ok?).to be false
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
it "@post1 should not cotain errors" do
|
|
126
|
+
expect(@post1.errors.count).to be 0
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
it "@post2 should not cotain errors" do
|
|
130
|
+
expect(@post2.errors.count).to be 1
|
|
131
|
+
expect(@post2.errors.added?(:body, :blank)).to be true
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
context "and both of them are invalid" do
|
|
136
|
+
before do
|
|
137
|
+
@post1 = RubyPost.new(body: 'body')
|
|
138
|
+
@post2 = RubyPost.new(title: 'title')
|
|
139
|
+
@context = HierarchyValidation::GrandSon.perform(posts: [@post1, @post2])
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
it "@context.ok? should be false" do
|
|
143
|
+
expect(@context.ok?).to be false
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
it "@post1 should not cotain errors" do
|
|
147
|
+
expect(@post1.errors.count).to be 1
|
|
148
|
+
expect(@post1.errors.added?(:title, :blank)).to be true
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
it "@post2 should not cotain errors" do
|
|
152
|
+
expect(@post2.errors.count).to be 1
|
|
153
|
+
expect(@post2.errors.keys).to eq [:body]
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
context "and both of them are invalid and one is marked_for_destruction" do
|
|
158
|
+
before do
|
|
159
|
+
@post1 = RubyPost.new(body: 'body', _destroy: true)
|
|
160
|
+
@post2 = RubyPost.new(title: 'title')
|
|
161
|
+
@context = HierarchyValidation::GrandSon.perform(posts: [@post1, @post2])
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
it "@context.ok? should be false" do
|
|
165
|
+
expect(@context.ok?).to be false
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
it "@post1 should not cotain errors" do
|
|
169
|
+
expect(@post1.errors.count).to be 0
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
it "@post2 should not cotain errors" do
|
|
173
|
+
expect(@post2.errors.count).to be 1
|
|
174
|
+
expect(@post2.errors.include?(:body)).to be true
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
context "one is valid, the other isn't but is also marked_for_destruction" do
|
|
179
|
+
before do
|
|
180
|
+
@post1 = RubyPost.new(_destroy: true)
|
|
181
|
+
@post2 = RubyPost.new(title: 'title', body: 'body')
|
|
182
|
+
@context = HierarchyValidation::GrandSon.perform(posts: [@post1, @post2])
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
it "@context.ok? should be true" do
|
|
186
|
+
expect(@context.ok?).to be true
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
it "@post1 should not cotain errors" do
|
|
190
|
+
expect(@post1.errors.count).to be 0
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
it "@post2 should not cotain errors" do
|
|
194
|
+
expect(@post2.errors.count).to be 0
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
context "one is valid, the other isn't and both are marked_for_destruction" do
|
|
199
|
+
before do
|
|
200
|
+
@post1 = RubyPost.new(_destroy: true)
|
|
201
|
+
@post2 = RubyPost.new(title: 'title', body: 'body', _destroy: true)
|
|
202
|
+
@context = HierarchyValidation::GrandSon.perform(posts: [@post1, @post2])
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
it "@context.ok? should be true" do
|
|
206
|
+
expect(@context.ok?).to be true
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
it "@post1 should not cotain errors" do
|
|
210
|
+
expect(@post1.errors.count).to be 0
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
it "@post2 should not cotain errors" do
|
|
214
|
+
expect(@post2.errors.count).to be 0
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
end
|
|
65
219
|
end
|
|
66
220
|
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
class RubyPost
|
|
2
2
|
|
|
3
|
-
attr_accessor :title, :body, :phone_number
|
|
3
|
+
attr_accessor :title, :body, :phone_number, :ignore_title, :_destroy
|
|
4
4
|
|
|
5
5
|
def initialize(attributes = {})
|
|
6
6
|
(attributes || {}).each { |name, value| send("#{name}=", value) }
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
+
def marked_for_destruction?
|
|
10
|
+
_destroy
|
|
11
|
+
end
|
|
12
|
+
|
|
9
13
|
end
|
|
@@ -2,7 +2,7 @@ class CustomValidator < RestMyCase::Validator
|
|
|
2
2
|
|
|
3
3
|
target :post
|
|
4
4
|
|
|
5
|
-
validate :phone_number_country_code
|
|
5
|
+
validate :phone_number_country_code, if: :should_validate_country_code
|
|
6
6
|
|
|
7
7
|
def phone_number_country_code(post)
|
|
8
8
|
if post.phone_number.split(' ')[0] != '123'
|
|
@@ -14,4 +14,8 @@ class CustomValidator < RestMyCase::Validator
|
|
|
14
14
|
true
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
+
def should_validate_country_code(post)
|
|
18
|
+
!context.should_not_validate_country_code
|
|
19
|
+
end
|
|
20
|
+
|
|
17
21
|
end
|
|
@@ -4,13 +4,19 @@ module HierarchyValidation
|
|
|
4
4
|
|
|
5
5
|
target :post
|
|
6
6
|
|
|
7
|
-
validates_presence_of :title
|
|
7
|
+
validates_presence_of :title, if: ->(post) { !post.ignore_title }
|
|
8
8
|
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
class Son < Father
|
|
12
12
|
|
|
13
|
-
validates_presence_of :
|
|
13
|
+
validates_presence_of :body
|
|
14
|
+
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class GrandSon < Son
|
|
18
|
+
|
|
19
|
+
target :posts
|
|
14
20
|
|
|
15
21
|
end
|
|
16
22
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
class LengthValidator < RestMyCase::Validator
|
|
2
|
+
|
|
3
|
+
target :user
|
|
4
|
+
|
|
5
|
+
validates_length_of :first_name, maximum: 5
|
|
6
|
+
|
|
7
|
+
validates_length_of :last_name, maximum: 5, message: "less than 5 if you don't mind"
|
|
8
|
+
|
|
9
|
+
validates_length_of :fax, in: 7..8, allow_nil: true
|
|
10
|
+
|
|
11
|
+
validates_length_of :phone, in: 7..8, allow_blank: true
|
|
12
|
+
|
|
13
|
+
validates_length_of :user_name, within: 6..7, too_long: 'pick a shorter name', too_short: 'pick a longer name'
|
|
14
|
+
|
|
15
|
+
validates_length_of :zip_code, minimum: 5, too_short: 'please enter at least 5 characters'
|
|
16
|
+
|
|
17
|
+
validates_length_of :smurf_leader, is: 4, message: "papa is spelled with 4 characters... don't play me."
|
|
18
|
+
|
|
19
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
class NumericalityValidator < RestMyCase::Validator
|
|
2
|
+
|
|
3
|
+
target :user
|
|
4
|
+
|
|
5
|
+
validates_numericality_of :phone
|
|
6
|
+
|
|
7
|
+
validates_numericality_of :phone, less_than: ->(user) { user.fax }, message: 'should be less than fax'
|
|
8
|
+
|
|
9
|
+
validates_numericality_of :fax, greater_than: :phone, message: 'should be greater than phone', if: ->(user) { user.validate_fax }
|
|
10
|
+
|
|
11
|
+
validates_numericality_of :fax, odd: true, message: 'should be odd'
|
|
12
|
+
|
|
13
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rest_my_case
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.9.
|
|
4
|
+
version: 1.9.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- goncalvesjoao
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2015-07-
|
|
11
|
+
date: 2015-07-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: pry
|
|
@@ -144,6 +144,9 @@ files:
|
|
|
144
144
|
- rest_my_case.gemspec
|
|
145
145
|
- spec/rest_my_case/accusation_attorneys/base_spec.rb
|
|
146
146
|
- spec/rest_my_case/accusation_attorneys/each_spec.rb
|
|
147
|
+
- spec/rest_my_case/accusation_attorneys/format_spec.rb
|
|
148
|
+
- spec/rest_my_case/accusation_attorneys/length_spec.rb
|
|
149
|
+
- spec/rest_my_case/accusation_attorneys/numericality_spec.rb
|
|
147
150
|
- spec/rest_my_case/base_spec.rb
|
|
148
151
|
- spec/rest_my_case/context/status_spec.rb
|
|
149
152
|
- spec/rest_my_case/defense_attorney/base_spec.rb
|
|
@@ -157,8 +160,11 @@ files:
|
|
|
157
160
|
- spec/support/rest_my_case_base.rb
|
|
158
161
|
- spec/support/validator/models/ruby_post.rb
|
|
159
162
|
- spec/support/validator/models/ruby_post_with_comments.rb
|
|
163
|
+
- spec/support/validator/models/ruby_user.rb
|
|
160
164
|
- spec/support/validator/usecases/custom_validator.rb
|
|
161
165
|
- spec/support/validator/usecases/hierarchy_validation.rb
|
|
166
|
+
- spec/support/validator/usecases/length_validator.rb
|
|
167
|
+
- spec/support/validator/usecases/numericality_validator.rb
|
|
162
168
|
homepage: https://github.com/goncalvesjoao/rest_my_case
|
|
163
169
|
licenses:
|
|
164
170
|
- MIT
|
|
@@ -186,6 +192,9 @@ summary: Quick and light "The Clean Architecture" use case implementation.
|
|
|
186
192
|
test_files:
|
|
187
193
|
- spec/rest_my_case/accusation_attorneys/base_spec.rb
|
|
188
194
|
- spec/rest_my_case/accusation_attorneys/each_spec.rb
|
|
195
|
+
- spec/rest_my_case/accusation_attorneys/format_spec.rb
|
|
196
|
+
- spec/rest_my_case/accusation_attorneys/length_spec.rb
|
|
197
|
+
- spec/rest_my_case/accusation_attorneys/numericality_spec.rb
|
|
189
198
|
- spec/rest_my_case/base_spec.rb
|
|
190
199
|
- spec/rest_my_case/context/status_spec.rb
|
|
191
200
|
- spec/rest_my_case/defense_attorney/base_spec.rb
|
|
@@ -199,5 +208,8 @@ test_files:
|
|
|
199
208
|
- spec/support/rest_my_case_base.rb
|
|
200
209
|
- spec/support/validator/models/ruby_post.rb
|
|
201
210
|
- spec/support/validator/models/ruby_post_with_comments.rb
|
|
211
|
+
- spec/support/validator/models/ruby_user.rb
|
|
202
212
|
- spec/support/validator/usecases/custom_validator.rb
|
|
203
213
|
- spec/support/validator/usecases/hierarchy_validation.rb
|
|
214
|
+
- spec/support/validator/usecases/length_validator.rb
|
|
215
|
+
- spec/support/validator/usecases/numericality_validator.rb
|