Commit eed78fa

mokha <mokha@cisco.com>
2019-01-11 16:27:14
validate all types.
1 parent c25adfb
lib/scim/kit/v2/attribute.rb
@@ -13,7 +13,7 @@ module Scim
 
         validate :presence_of_value, if: proc { |x| x.type.required }
         validate :inclusion_of_value, if: proc { |x| x.type.canonical_values }
-        validate :validate_array, if: proc { |x| x.type.multi_valued }
+        validate :validate_type
 
         def initialize(type:, value: nil)
           @type = type
@@ -39,9 +39,8 @@ module Scim
           errors.add(type.name, I18n.t('errors.messages.inclusion'))
         end
 
-        def validate_array
-          return if _value.respond_to?(:each) &&
-                    _value.all? { |x| type.valid?(x) }
+        def validate_type
+          return if type.valid?(_value)
 
           errors.add(type.name, I18n.t('errors.messages.invalid'))
         end
lib/scim/kit/v2/attribute_type.rb
@@ -30,7 +30,13 @@ module Scim
           boolean: ->(x) { BOOLEAN_VALUES.include?(x) },
           datetime: ->(x) { x.is_a?(DateTime) },
           decimal: ->(x) { x.is_a?(Float) },
-          integer: ->(x) { x&.integer? rescue false },
+          integer: lambda { |x|
+            begin
+              x&.integer?
+            rescue StandardError
+              false
+            end
+          },
           reference: ->(x) { x =~ /\A#{URI.regexp(%w[http https])}\z/ },
           string: ->(x) { x.is_a?(String) }
         }.freeze
@@ -111,6 +117,7 @@ module Scim
 
                 item.keys.each do |key|
                   attribute = attributes.find { |x| x.name.to_s.underscore == key.to_s.underscore }
+                  return false unless attribute
                   return false unless attribute.valid?(item[key])
                 end
               end
spec/scim/kit/v2/attribute_spec.rb
@@ -200,6 +200,25 @@ RSpec.describe Scim::Kit::V2::Attribute do
     specify { expect(subject.as_json[:name][:givenName]).to eql('Tsuyoshi') }
   end
 
+  context 'with single valued complex type' do
+    let(:type) do
+      x = Scim::Kit::V2::AttributeType.new(name: :person, type: :complex)
+      x.add_attribute(name: :name)
+      x.add_attribute(name: :age, type: :integer)
+      x
+    end
+
+    before { subject._value = { name: 'mo', age: 34 } }
+
+    specify { expect(subject).to be_valid }
+
+    context 'when invalid sub attribute' do
+      before { subject._value = { name: 34, age: 'wrong' } }
+
+      specify { expect(subject).not_to be_valid }
+    end
+  end
+
   context 'with multi valued complex type' do
     let(:type) do
       x = Scim::Kit::V2::AttributeType.new(name: 'emails', type: :complex)
spec/scim/kit/v2/attribute_type_spec.rb
@@ -103,6 +103,7 @@ RSpec.describe Scim::Kit::V2::AttributeType do
 
     context 'when multi valued string type' do
       subject { described_class.new(name: :emails, type: :string) }
+
       let(:email) { FFaker::Internet.email }
 
       before do
@@ -131,6 +132,7 @@ RSpec.describe Scim::Kit::V2::AttributeType do
 
     context 'when multi valued complex type' do
       subject { described_class.new(name: :emails, type: :complex) }
+
       let(:email) { FFaker::Internet.email }
 
       before do