Commit 1082820

mokha <mokha@cisco.com>
2019-02-01 00:22:36
add validation for multi_valued attribute
1 parent 3c552a2
Changed files (2)
lib
scim
spec
lib/scim/kit/v2/attribute.rb
@@ -16,15 +16,17 @@ module Scim
         validate :inclusion_of_value, if: proc { |x| x._type.canonical_values }
         validate :validate_type, unless: proc { |x| x._type.complex? }
         validate :validate_complex, if: proc { |x| x._type.complex? }
+        validate :validate_multiple, if: proc { |x| x._type.multi_valued && !x._type.complex? }
 
         def initialize(resource:, type:, value: nil)
           @_type = type
           @_value = value || type.multi_valued ? [] : nil
           @_resource = resource
+
           define_attributes_for(resource, type.attributes)
         end
 
-        def _assign(new_value, coerce: true)
+        def _assign(new_value, coerce: false)
           @_value = coerce ? _type.coerce(new_value) : new_value
         end
 
@@ -78,6 +80,16 @@ module Scim
           end
         end
 
+        def validate_multiple
+          return unless _value.respond_to?(:to_a)
+
+          duped_type = _type.dup
+          duped_type.multi_valued = false
+          _value.to_a.each do |x|
+            errors.add(duped_type.name, I18n.t('errors.messages.invalid')) unless duped_type.valid?(x)
+          end
+        end
+
         def read_only?
           _type.mutability == Mutability::READ_ONLY
         end
spec/scim/kit/v2/attribute_spec.rb
@@ -35,7 +35,7 @@ RSpec.describe Scim::Kit::V2::Attribute do
 
       context "when multiple invalid values are added" do
         before do
-          subject._value = ["superman", {}]
+          subject._assign(["superman", {}], coerce: false)
         end
 
         specify { expect(subject).not_to be_valid }