Commit 65a4fa6

mokha <mokha@cisco.com>
2019-02-02 18:34:50
validate missing and required attributes
1 parent 2bcb798
lib/scim/kit/v2/attribute.rb
@@ -91,9 +91,7 @@ module Scim
         end
 
         def each_value(&block)
-          return unless _type.multi_valued
-
-          _value.each(&block)
+          Array(_value).each(&block)
         end
 
         def validate_multiple
lib/scim/kit/v2/unknown_attribute.rb
@@ -12,11 +12,11 @@ module Scim
           @name = name
         end
 
-        def _assign(*args)
+        def _assign(*_args)
           valid?
         end
 
-        def _value=(*args)
+        def _value=(*_args)
           raise Scim::Kit::UnknownAttributeError, name
         end
 
spec/scim/kit/v2/attribute_spec.rb
@@ -24,7 +24,7 @@ RSpec.describe Scim::Kit::V2::Attribute do
 
       specify { expect(subject._value).to match_array([]) }
 
-      context "when multiple valid values are added" do
+      context 'when multiple valid values are added' do
         before do
           subject._value = %w[superman batman]
         end
@@ -33,9 +33,9 @@ RSpec.describe Scim::Kit::V2::Attribute do
         specify { expect(subject).to be_valid }
       end
 
-      context "when multiple invalid values are added" do
+      context 'when multiple invalid values are added' do
         before do
-          subject._assign(["superman", {}], coerce: false)
+          subject._assign(['superman', {}], coerce: false)
         end
 
         specify { expect(subject).not_to be_valid }
@@ -236,7 +236,7 @@ RSpec.describe Scim::Kit::V2::Attribute do
 
     specify do
       subject.name = 'mo'
-      subject.age = { }
+      subject.age = {}
       expect(subject).not_to be_valid
     end
   end
@@ -271,7 +271,7 @@ RSpec.describe Scim::Kit::V2::Attribute do
 
       specify { expect(subject).not_to be_valid }
       specify { expect(subject.errors[:blah]).to be_present }
-      specify { expect(subject.errors[:emails]).to be_present }
+      specify { expect(subject.errors[:value]).to be_present }
     end
   end
 
spec/scim/kit/v2/resource_spec.rb
@@ -133,7 +133,9 @@ RSpec.describe Scim::Kit::V2::Resource do
     before do
       schema.add_attribute(name: 'emails', type: :complex) do |x|
         x.multi_valued = true
-        x.add_attribute(name: 'value')
+        x.add_attribute(name: 'value') do |y|
+          y.required = true
+        end
         x.add_attribute(name: 'primary', type: :boolean)
       end
       subject.emails = [
@@ -144,6 +146,18 @@ RSpec.describe Scim::Kit::V2::Resource do
 
     specify { expect(subject.emails).to match_array([{ value: email, primary: true }, { value: other_email, primary: false }]) }
     specify { expect(subject.as_json[:emails]).to match_array([{ value: email, primary: true }, { value: other_email, primary: false }]) }
+
+    specify do
+      subject.emails = [{ value: email, primary: 'q' }]
+      expect(subject).not_to be_valid
+      expect(subject.errors[:primary]).to be_present
+    end
+
+    specify do
+      subject.emails = [{ primary: true }]
+      expect(subject).not_to be_valid
+      expect(subject.errors[:value]).to be_present
+    end
   end
 
   context 'with multiple schemas' do
@@ -198,7 +212,7 @@ RSpec.describe Scim::Kit::V2::Resource do
       specify { expect(subject.errors[:hero]).to be_present }
     end
 
-    context "when validating a complex type" do
+    context 'when validating a complex type' do
       before do
         schema.add_attribute(name: :manager, type: :complex) do |x|
           x.multi_valued = false
@@ -232,7 +246,7 @@ RSpec.describe Scim::Kit::V2::Resource do
         end
       end
 
-      context "when valid" do
+      context 'when valid' do
         before do
           subject.manager.value = SecureRandom.uuid
           subject.manager.write_attribute('$ref', FFaker::Internet.uri('https'))
@@ -242,7 +256,7 @@ RSpec.describe Scim::Kit::V2::Resource do
         specify { expect(subject).to be_valid }
       end
 
-      context "when invalid" do
+      context 'when invalid' do
         before do
           subject.manager.value = SecureRandom.uuid
           subject.manager.write_attribute('$ref', SecureRandom.uuid)
@@ -475,10 +489,12 @@ RSpec.describe Scim::Kit::V2::Resource do
       end
 
       specify do
-        expect(subject.emails).to match_array([
-                                                { value: email, primary: true },
-                                                { value: other_email, primary: false }
-                                              ])
+        expect(subject.emails).to match_array(
+          [
+            { value: email, primary: true },
+            { value: other_email, primary: false }
+          ]
+        )
       end
 
       specify { expect(subject.emails[0][:value]).to eql(email) }