Commit 7da26f5
Changed files (2)
lib
scim
kit
spec
scim
kit
lib/scim/kit/v2/attribute_type.rb
@@ -28,7 +28,6 @@ module Scim
VALIDATIONS = {
binary: ->(x) { x.is_a?(String) && x.match?(B64) },
boolean: ->(x) { BOOLEAN_VALUES.include?(x) },
- # complex: ->(x) { },
datetime: ->(x) { x.is_a?(DateTime) },
decimal: ->(x) { x.is_a?(Float) },
integer: ->(x) { x&.integer? },
@@ -102,7 +101,22 @@ module Scim
end
def valid?(value)
- VALIDATIONS[type]&.call(value)
+ if complex?
+ return false unless value.is_a?(Hash)
+ 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
+ end
end
private
spec/scim/kit/v2/attribute_type_spec.rb
@@ -95,8 +95,37 @@ RSpec.describe Scim::Kit::V2::AttributeType do
specify { expect(described_class.new(name: :x, type: :reference)).not_to be_valid(1) }
specify { expect(described_class.new(name: :x, type: :reference)).not_to be_valid(['hello']) }
- specify { expect(described_class.new(name: :x)).to be_valid('name') }
- specify { expect(described_class.new(name: :x)).not_to be_valid(1) }
- specify { expect(described_class.new(name: :x)).not_to be_valid(['string']) }
+ specify { expect(described_class.new(name: :x, type: :string)).to be_valid('name') }
+ specify { expect(described_class.new(name: :x, type: :string)).not_to be_valid(1) }
+ specify { expect(described_class.new(name: :x, type: :string)).not_to be_valid(['string']) }
+
+ context 'when multi valued string type' do
+ subject { described_class.new(name: :emails, type: :string) }
+ let(:email) { FFaker::Internet.email }
+
+ before do
+ subject.multi_valued = true
+ end
+
+ specify { expect(subject).to be_valid([email]) }
+ specify { expect(subject).not_to be_valid([1]) }
+ specify { expect(subject).not_to be_valid(email) }
+ end
+
+ context 'when multi valued complex type' do
+ subject { described_class.new(name: :emails, type: :complex) }
+ let(:email) { FFaker::Internet.email }
+
+ before do
+ subject.multi_valued = true
+ subject.add_attribute(name: 'value', type: :string)
+ subject.add_attribute(name: 'primary', type: :boolean)
+ end
+
+ specify { expect(subject).to be_valid([value: email, primary: true]) }
+ specify { expect(subject).not_to be_valid([email]) }
+ specify { expect(subject).not_to be_valid([value: 1, primary: true]) }
+ specify { expect(subject).not_to be_valid([value: email, primary: 'true']) }
+ end
end
end