Commit 8512c49
Changed files (2)
lib
scim
kit
spec
scim
kit
lib/scim/kit/v2/attribute_type.rb
@@ -107,45 +107,37 @@ module Scim
end
def valid?(value)
- if complex?
- if multi_valued
- return false unless value.respond_to?(:each)
-
- value.each do |item|
- return false unless item.is_a?(Hash)
-
- 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
- else
- return false unless value.is_a?(Hash)
-
- value.keys.each do |key|
- attribute = attributes.find { |x| x.name.to_s.underscore == key.to_s.underscore }
- return false unless attribute.valid?(value[key])
- end
- true
+ return false if multi_valued && !value.respond_to?(:each)
+
+ if multi_valued
+ value.each do |x|
+ return false unless (complex? ? valid_complex?(x) : valid_simple?(x))
end
+ true
else
- if multi_valued
- return false unless value.respond_to?(:each)
-
- validation = VALIDATIONS[type]
- value.each do |x|
- return false unless validation&.call(x)
- end
- true
- else
- VALIDATIONS[type]&.call(value)
- end
+ complex? ? valid_complex?(value) : valid_simple?(value)
end
end
private
+ def valid_simple?(value)
+ VALIDATIONS[type]&.call(value)
+ end
+
+ def valid_complex?(item)
+ return false unless item.is_a?(Hash)
+
+ item.keys.each do |key|
+ return false unless type_for(key)&.valid?(item[key])
+ end
+ end
+
+ def type_for(name)
+ name = name.to_s.underscore
+ attributes.find { |x| x.name.to_s.underscore == name }
+ end
+
def string?
type_is?(:string)
end
spec/scim/kit/v2/attribute_type_spec.rb
@@ -134,6 +134,7 @@ RSpec.describe Scim::Kit::V2::AttributeType do
subject { described_class.new(name: :emails, type: :complex) }
let(:email) { FFaker::Internet.email }
+ let(:other_email) { FFaker::Internet.email }
before do
subject.multi_valued = true
@@ -142,6 +143,7 @@ RSpec.describe Scim::Kit::V2::AttributeType do
end
specify { expect(subject).to be_valid([value: email, primary: true]) }
+ specify { expect(subject).to be_valid([{ value: email, primary: true, }, { value: other_email, primary: false }]) }
specify { expect(subject).not_to be_valid(email) }
specify { expect(subject).not_to be_valid([email]) }
specify { expect(subject).not_to be_valid([value: 1, primary: true]) }