validates_serialized 0.0.7 → 0.0.8

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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OTRkMjAxMTM5MDY1MDg1ZjhhNzAwYTRiYjdlNmM1OTA5NmUxZjk1NA==
4
+ NmQ3OTUxMDE5ODExYjA2YWI3M2YwZTk2Yzk2NmYwYWVjNmIzOWM3Yg==
5
5
  data.tar.gz: !binary |-
6
- M2Q2NWM0MmYxMGUwNGIzMjc0ZWFmYzY1MjM4MDViMGQ2ZGY3MWMyYQ==
6
+ YWIzOWE3ODJiMWYzNGI5ODhiMDAzNTU5YWM1OGZiMDA0NjliMmJmMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NzdjZjA2NTEwYTc5MjhhOTAxNmEyMTk2MGIxZTFiODA1OTBmYTcyZDEzNGNj
10
- NDg4MGJhM2QzYTRmODBjMzJkMjFkMzQ0ZDdhZDc0ZDliN2RiNTRlZWFiMTI4
11
- Y2JhNDk5MmY0NjRmMjQ2NDNkZTdiMTI3ZDhmYmMwNzdhNmEzZmQ=
9
+ NjFhYmM3OWM2ZDMwOWFjMWMwN2Y3NDIyMDA0NzgxNmI4ZjI3NzMzYjI3NzMx
10
+ MzdlZDkzMDFiMmNjMDZjZTE2NjhjYTI4MWI5Y2M2YzNmYWI4MDM0NDc3ZWU4
11
+ MTk0MjAxMzJjY2IyMjQ5M2Y2OTNjZDAwODViNWRjNjY4Y2JmZWU=
12
12
  data.tar.gz: !binary |-
13
- ODE1Y2E5Yjk5NDA0YjVmNWQyMWJiNTVmZTJlMTZkODcyNTk5OGJiOGExMzQ1
14
- MDYwNTIzZDVlOGQ2MmQ2ZDRhOGI1MzMzZTQzNGM2MjQyYzc5ZWY4NjVhNDY4
15
- ZDJjYzRmOGEwYjRkNDYzY2NiMDg1MWU5M2Q3MjJmZWRmOTIwMWY=
13
+ NzA3MTI5NmY5MDVhMjQ4ZGU5NDQwZTQ0MjhmODM0ZjRjYzA5ZjEwOWU3Nzk4
14
+ ZDc1OGY5YjI5NzA3OWU1MzA4NzBiNDIyYjA3ODYwN2U3YzgyZWI5NGZhYzZh
15
+ M2Y1YjZmZDE1YWQxZGEzN2JkNDdiODY3ZjNjNzJhMGZhZjdkN2I=
data/Gemfile.lock CHANGED
@@ -1,26 +1,26 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validates_serialized (0.0.5)
4
+ validates_serialized (0.0.7)
5
5
  activemodel (>= 3.0, < 5.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activemodel (4.1.8)
11
- activesupport (= 4.1.8)
10
+ activemodel (4.2.0)
11
+ activesupport (= 4.2.0)
12
12
  builder (~> 3.1)
13
- activesupport (4.1.8)
14
- i18n (~> 0.6, >= 0.6.9)
13
+ activesupport (4.2.0)
14
+ i18n (~> 0.7)
15
15
  json (~> 1.7, >= 1.7.7)
16
16
  minitest (~> 5.1)
17
- thread_safe (~> 0.1)
17
+ thread_safe (~> 0.3, >= 0.3.4)
18
18
  tzinfo (~> 1.1)
19
19
  builder (3.2.2)
20
20
  diff-lcs (1.2.5)
21
- i18n (0.6.11)
22
- json (1.8.1)
23
- minitest (5.5.0)
21
+ i18n (0.7.0)
22
+ json (1.8.2)
23
+ minitest (5.5.1)
24
24
  rake (10.4.2)
25
25
  rspec (2.99.0)
26
26
  rspec-core (~> 2.99.0)
@@ -2,7 +2,6 @@ module ActiveModel
2
2
  module Validations
3
3
  class ArrayBlockValidator < BlockValidator #:nodoc:
4
4
  def initialize(options, &block)
5
- @block = block
6
5
  @options = options
7
6
  super
8
7
  end
@@ -14,10 +13,21 @@ module ActiveModel
14
13
  add_errors_to_record(record, attribute, errors)
15
14
  end
16
15
 
16
+ def build_serialized_object(value)
17
+ #TODO: For the Rails 4 version, I can just clear_validators! on the ValidateableHash
18
+ temp_class = Class.new(ValidateableArrayValue)
19
+ temp_class_name = "ValidateableArrayValue_#{SecureRandom.hex}"
20
+ if self.class.constants.include?(temp_class_name)
21
+ self.class.send(:remove_const, temp_class_name)
22
+ end
23
+ self.class.const_set(temp_class_name, temp_class)
24
+ temp_class.new(value)
25
+ end
26
+
17
27
  def get_serialized_object_errors(array)
18
28
  messages = []
19
29
  array.each do |value|
20
- serialized_object = ValidateableArrayValue.new(value)
30
+ serialized_object = build_serialized_object(value)
21
31
  serialized_object.class_eval &@block
22
32
  serialized_object.valid?
23
33
  message = serialized_object.errors.messages[:value]
@@ -29,8 +39,7 @@ module ActiveModel
29
39
  def add_errors_to_record(record, attribute, error_array)
30
40
  error_array.each do |value|
31
41
  text = value.join(", ")
32
- message = "#{attribute} has a value that #{text}"
33
- record.errors.add(attribute, message)
42
+ record.errors.add(attribute, "#{attribute} has a value that #{text}")
34
43
  end
35
44
  if exception = options[:strict]
36
45
  exception = ActiveModel::StrictValidationFailed if exception == true
@@ -38,14 +47,6 @@ module ActiveModel
38
47
  raise exception, exception_message unless exception_message.blank?
39
48
  end
40
49
  end
41
-
42
- # def get_message_from_error_hash(error_hash)
43
- # message = nil
44
- # error_hash.each_pair do |key, array|
45
- # message = array.join(", ")
46
- # end
47
- # message
48
- # end
49
50
  end
50
51
 
51
52
  module ClassMethods
@@ -2,7 +2,6 @@ module ActiveModel
2
2
  module Validations
3
3
  class HashBlockValidator < BlockValidator #:nodoc:
4
4
  def initialize(options, &block)
5
- @block = block
6
5
  @options = options
7
6
  super
8
7
  end
@@ -14,8 +13,19 @@ module ActiveModel
14
13
  add_errors_to_record(record, attribute, error_hash)
15
14
  end
16
15
 
16
+ def build_serialized_object(value)
17
+ #TODO: For the Rails 4 version, I can just clear_validators! on the ValidateableHash
18
+ temp_class = Class.new(ValidateableHash)
19
+ temp_class_name = "TempValidateableHash_#{SecureRandom.hex}"
20
+ if self.class.constants.include?(temp_class_name)
21
+ self.class.send(:remove_const, temp_class_name)
22
+ end
23
+ self.class.const_set(temp_class_name, temp_class)
24
+ temp_class.new(value)
25
+ end
26
+
17
27
  def get_serialized_object_errors(value)
18
- serialized_object = ValidateableHash.new(value)
28
+ serialized_object = build_serialized_object(value)
19
29
  serialized_object.class_eval &@block
20
30
  serialized_object.valid?
21
31
  serialized_object.errors.messages
@@ -13,8 +13,19 @@ module ActiveModel
13
13
  add_errors_to_record(record, attribute, error_hash)
14
14
  end
15
15
 
16
+ def build_serialized_object(value)
17
+ #TODO: For the Rails 4 version, I can just clear_validators! on the ValidateableHash
18
+ temp_class = Class.new(ValidateableObject)
19
+ temp_class_name = "ValidateableObject_#{SecureRandom.hex}"
20
+ if self.class.constants.include?(temp_class_name)
21
+ self.class.send(:remove_const, temp_class_name)
22
+ end
23
+ self.class.const_set(temp_class_name, temp_class)
24
+ temp_class.new(value)
25
+ end
26
+
16
27
  def get_serialized_object_errors(value)
17
- serialized_object = ValidateableObject.new(value)
28
+ serialized_object = build_serialized_object(value)
18
29
  serialized_object.class_eval &@block
19
30
  serialized_object.valid?
20
31
  serialized_object.errors.messages
@@ -1,3 +1,3 @@
1
1
  module ValidatesSerialized
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -71,15 +71,15 @@ class Blog
71
71
  validates :value, length: { in: 4..20 }
72
72
  end
73
73
 
74
- validates_hash_keys :comments, allow_blank: true do
75
- validates :admin, presence: true
76
- end
77
-
78
74
  validates_each_in_array :metadata, if: :metadata do
79
75
  validates_hash_keys :value do
80
76
  validates :timestamp, presence: true
81
77
  end
82
78
  end
79
+
80
+ validates_hash_keys :comments, allow_blank: true do
81
+ validates :admin, presence: true
82
+ end
83
83
  end
84
84
 
85
85
  describe ValidatesSerialized do
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+
3
+ class Consumer
4
+ include ActiveModel::Validations
5
+
6
+ def initialize(h={})
7
+ h.each {|k,v| send("#{k}=",v)}
8
+ end
9
+
10
+ def income_sources
11
+ @income_sources || []
12
+ end
13
+
14
+ def income_sources=(val)
15
+ @income_sources = val
16
+ end
17
+
18
+ def credit_card
19
+ @credit_card || {}
20
+ end
21
+
22
+ def credit_card=(val)
23
+ @credit_card = val
24
+ end
25
+
26
+ validates_hash_keys :credit_card do
27
+ validates :brand, presence: true, inclusion: { in: ['visa', 'mastercard'] }
28
+ validates :number, presence: true, length: { minimum: 10 }
29
+ end
30
+
31
+ validates :income_sources, presence: true
32
+ validates_each_in_array :income_sources do
33
+ validates_hash_keys :value do
34
+ validates :name, presence: true
35
+ validates :amount, presence: true, numericality: { greater_than: 0 }
36
+ end
37
+ end
38
+ end
39
+
40
+ describe Consumer do
41
+ it "does not add error for valid income_sources" do
42
+ model = described_class.new(income_sources: [{ amount: 40, name: "Widget Co." }])
43
+ model.valid?
44
+ model.errors[:income_sources].should be_empty
45
+ end
46
+
47
+ it "adds correct income_sources errors" do
48
+ model = described_class.new(income_sources: [{ amount: -40 }])
49
+ model.valid?
50
+ model.errors[:income_sources].should eq(["income_sources has a value that name can't be blank, amount must be greater than 0"])
51
+ end
52
+
53
+ it "does not add error for valid credit_card" do
54
+ model = described_class.new(credit_card: { brand: "visa", number: "4111333322221111" }, income_sources: [{name: nil}])
55
+ model.valid?
56
+ model.errors[:credit_card].should be_empty
57
+ end
58
+
59
+ it "adds correct credit_card errors" do
60
+ model = described_class.new(credit_card: { number: "112233" })
61
+ model.valid?
62
+ model.errors[:credit_card].should eq(["brand can't be blank, is not included in the list", "number is too short (minimum is 10 characters)"])
63
+ end
64
+
65
+ # it "is valid without comments" do
66
+ # model = Blog.new(ratings: [1, 3, 1], author: Author.new(name: "Tom"))
67
+ # model.should be_valid
68
+ # end
69
+
70
+ # it "is valid with valid tags" do
71
+ # model = Blog.new(ratings: [1, 3], author: Author.new(name: "Tom"), comments: { admin: "This is great!" }, tags: ["sweet", "awesome"])
72
+ # model.should be_valid
73
+ # end
74
+
75
+ # it "is valid with valid metadata" do
76
+ # model = Blog.new(ratings: [1, 3], author: Author.new(name: "Tom"), comments: { admin: "This is great!" }, metadata: [{timestamp: Time.new(2014, 1, 2)}, {timestamp: Time.new(2014, 2, 2)}])
77
+ # model.should be_valid
78
+ # end
79
+
80
+ # it "is invalid without Serialized author" do
81
+ # model = Blog.new(ratings: [1, 3, 1], comments: { admin: "This is great!" })
82
+ # model.should_not be_valid
83
+ # end
84
+
85
+ # it "is invalid without author name" do
86
+ # model = Blog.new(ratings: [1, 3, 1], author: Author.new, comments: { admin: "This is great!" })
87
+ # model.should_not be_valid
88
+ # model.errors[:author].should eq(["name can't be blank"])
89
+ # end
90
+
91
+ # it "is invalid without comment admin key" do
92
+ # model = Blog.new(ratings: [1, 3, 1], author: Author.new(name: "Tom"), comments: { other: "This is great!" })
93
+ # model.should_not be_valid
94
+ # model.errors[:comments].should eq(["admin can't be blank"])
95
+ # end
96
+
97
+ # it "raises error without ratings" do
98
+ # model = Blog.new(ratings: nil, author: Author.new(name: "Tom"), comments: { admin: "This is great!" })
99
+ # expect { model.valid? }.to raise_error
100
+ # end
101
+
102
+ # it "is invalid with invalid ratings value" do
103
+ # model = Blog.new(ratings: [1, 8], author: Author.new(name: "Tom"), comments: { admin: "This is great!" })
104
+ # model.should_not be_valid
105
+ # model.errors[:ratings].should eq(["is not included in the list"])
106
+ # end
107
+
108
+ # it "is invalid with invalid tags" do
109
+ # model = Blog.new(ratings: [1, 3], author: Author.new(name: "Tom"), comments: { admin: "This is great!" }, tags: ["sweet", "awesome", "i"])
110
+ # model.should_not be_valid
111
+ # model.errors[:tags].should eq(["tags has a value that is too short (minimum is 4 characters)"])
112
+ # end
113
+ end
@@ -23,27 +23,37 @@ describe ActiveModel::Validations::ArrayBlockValidator do
23
23
  end
24
24
  end
25
25
 
26
- it "does not raise error for valid value" do
27
- record = ValidatorBlockArrayTestOne.new(my_attr: [2, 4])
28
- record.valid?
29
- record.errors[:my_attr].should eq([])
30
- end
26
+ describe "validating" do
27
+ it "does not raise error for valid value" do
28
+ record = ValidatorBlockArrayTestOne.new(my_attr: [2, 4])
29
+ record.valid?
30
+ record.errors[:my_attr].should eq([])
31
+ end
31
32
 
32
- it "adds error for invalid value" do
33
- record = ValidatorBlockArrayTestOne.new(my_attr: [nil, 4])
34
- record.valid?
35
- record.errors[:my_attr].should eq(["my_attr has a value that can't be blank"])
36
- end
33
+ it "adds error for invalid value" do
34
+ record = ValidatorBlockArrayTestOne.new(my_attr: [nil, 4])
35
+ record.valid?
36
+ record.errors[:my_attr].should eq(["my_attr has a value that can't be blank"])
37
+ end
37
38
 
38
- it "adds multiple errors for multiple invalid value" do
39
- record = ValidatorBlockArrayTestOne.new(my_attr: [nil, 4, nil])
40
- record.valid?
41
- record.errors[:my_attr].should eq(["my_attr has a value that can't be blank", "my_attr has a value that can't be blank"])
39
+ it "adds multiple errors for multiple invalid value" do
40
+ record = ValidatorBlockArrayTestOne.new(my_attr: [nil, 4, nil])
41
+ record.valid?
42
+ record.errors[:my_attr].should eq(["my_attr has a value that can't be blank", "my_attr has a value that can't be blank"])
43
+ end
44
+
45
+ it "raises error for non-array" do
46
+ record = ValidatorBlockArrayTestOne.new(my_attr: 4)
47
+ expect { record.valid? }.to raise_error(TypeError, 'my_attr is not an Array')
48
+ end
42
49
  end
43
50
 
44
- it "raises error for non-array" do
45
- record = ValidatorBlockArrayTestOne.new(my_attr: 4)
46
- expect { record.valid? }.to raise_error(TypeError, 'my_attr is not an Array')
51
+ describe "clearing ValidateableArrayValue validators" do
52
+ it "clears validators after validation" do
53
+ record = ValidatorBlockArrayTestOne.new(my_attr: [2, 4])
54
+ record.valid?
55
+ ValidateableArrayValue.validators.should be_empty
56
+ end
47
57
  end
48
58
  end
49
59
 
@@ -68,25 +78,35 @@ describe ActiveModel::Validations::ArrayBlockValidator do
68
78
  end
69
79
  end
70
80
 
71
- it "does not raise error for valid value" do
72
- record = ValidatorBlockArrayTestStrict.new(my_attr: [2, 3])
73
- record.valid?
74
- record.errors[:my_attr].should eq([])
75
- end
81
+ describe "validating" do
82
+ it "does not raise error for valid value" do
83
+ record = ValidatorBlockArrayTestStrict.new(my_attr: [2, 3])
84
+ record.valid?
85
+ record.errors[:my_attr].should eq([])
86
+ end
76
87
 
77
- it "raises error for invalid value" do
78
- record = ValidatorBlockArrayTestStrict.new(my_attr: [2, 5])
79
- expect { record.valid? }.to raise_error(ActiveModel::StrictValidationFailed, 'my_attr has a value that is not included in the list')
80
- end
88
+ it "raises error for invalid value" do
89
+ record = ValidatorBlockArrayTestStrict.new(my_attr: [2, 5])
90
+ expect { record.valid? }.to raise_error(ActiveModel::StrictValidationFailed, 'my_attr has a value that is not included in the list')
91
+ end
81
92
 
82
- it "raises error for multiple invalid value" do
83
- record = ValidatorBlockArrayTestStrict.new(my_attr: [nil, 9])
84
- expect { record.valid? }.to raise_error(ActiveModel::StrictValidationFailed, "my_attr has a value that is not included in the list, my_attr has a value that is not included in the list")
93
+ it "raises error for multiple invalid value" do
94
+ record = ValidatorBlockArrayTestStrict.new(my_attr: [nil, 9])
95
+ expect { record.valid? }.to raise_error(ActiveModel::StrictValidationFailed, "my_attr has a value that is not included in the list, my_attr has a value that is not included in the list")
96
+ end
97
+
98
+ it "raises error for non-array" do
99
+ record = ValidatorBlockArrayTestStrict.new(my_attr: 4)
100
+ expect { record.valid? }.to raise_error(TypeError, 'my_attr is not an Array')
101
+ end
85
102
  end
86
103
 
87
- it "raises error for non-array" do
88
- record = ValidatorBlockArrayTestStrict.new(my_attr: 4)
89
- expect { record.valid? }.to raise_error(TypeError, 'my_attr is not an Array')
104
+ describe "clearing ValidateableArrayValue validators" do
105
+ it "clears validators after validation" do
106
+ record = ValidatorBlockArrayTestStrict.new(my_attr: [2, 3])
107
+ record.valid?
108
+ ValidateableArrayValue.validators.should be_empty
109
+ end
90
110
  end
91
111
  end
92
112
  end
@@ -48,6 +48,14 @@ describe ActiveModel::Validations::HashBlockValidator do
48
48
  expect { record.valid? }.to raise_error(TypeError, 'my_attr is not a Hash')
49
49
  end
50
50
  end
51
+
52
+ describe "clearing ValidateableHash validators" do
53
+ it "clears validators after validation" do
54
+ record = ValidatorBlockHashTestOne.new(my_attr: { first_key: 2, second_key: 4 })
55
+ record.valid?
56
+ ValidateableHash.validators.should be_empty
57
+ end
58
+ end
51
59
  end
52
60
 
53
61
  context "#validates_hash_keys!" do
@@ -94,5 +102,13 @@ describe ActiveModel::Validations::HashBlockValidator do
94
102
  expect { record.valid? }.to raise_error(TypeError, 'my_attr is not a Hash')
95
103
  end
96
104
  end
105
+
106
+ describe "clearing ValidateableHash validators" do
107
+ it "clears validators after validation" do
108
+ record = ValidatorBlockHashTestStrict.new(my_attr: {first_key: 2, second_key: 3})
109
+ record.valid?
110
+ ValidateableHash.validators.should be_empty
111
+ end
112
+ end
97
113
  end
98
114
  end
@@ -65,6 +65,14 @@ describe ActiveModel::Validations::ObjectBlockValidator do
65
65
  record.errors[:my_attr].should eq(["name can't be blank", "age is not included in the list"])
66
66
  end
67
67
  end
68
+
69
+ describe "clearing ValidateableObject validators" do
70
+ it "clears validators after validation" do
71
+ record = ValidatorBlockObjectTestOne.new(my_attr: Foo.new(name: nil, age: 9))
72
+ record.valid?
73
+ ValidateableObject.validators.should be_empty
74
+ end
75
+ end
68
76
  end
69
77
 
70
78
  context "#validates_serialized!" do
@@ -106,5 +114,13 @@ describe ActiveModel::Validations::ObjectBlockValidator do
106
114
  expect { record.valid? }.to raise_error(ActiveModel::StrictValidationFailed, "name can't be blank")
107
115
  end
108
116
  end
117
+
118
+ describe "clearing ValidateableObject validators" do
119
+ it "clears validators after validation" do
120
+ record = ValidatorBlockObjectTestStrict.new(my_attr: Foo.new(name: "Jim", age: 3))
121
+ record.valid?
122
+ ValidateableObject.validators.should be_empty
123
+ end
124
+ end
109
125
  end
110
126
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_serialized
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - brycesenz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-06 00:00:00.000000000 Z
11
+ date: 2015-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -114,6 +114,7 @@ files:
114
114
  - notes.md
115
115
  - spec/acceptance_spec.rb
116
116
  - spec/each_validator_spec.rb
117
+ - spec/new_acceptance_spec.rb
117
118
  - spec/spec_helper.rb
118
119
  - spec/validateable_array_value_spec.rb
119
120
  - spec/validateable_hash_spec.rb
@@ -152,6 +153,7 @@ summary: An ActiveModel enhancement that allows for better validation of seriali
152
153
  test_files:
153
154
  - spec/acceptance_spec.rb
154
155
  - spec/each_validator_spec.rb
156
+ - spec/new_acceptance_spec.rb
155
157
  - spec/spec_helper.rb
156
158
  - spec/validateable_array_value_spec.rb
157
159
  - spec/validateable_hash_spec.rb