Commit 9ef75fe

mo <mo.khan@gmail.com>
2019-01-10 05:00:45
fix some linter errors
1 parent 79620de
Changed files (3)
lib/scim/kit/v2/attribute.rb
@@ -40,7 +40,8 @@ module Scim
         end
 
         def validate_array
-          return if _value.respond_to?(:each) && _value.all? { |x| type.valid?(x) }
+          return if _value.respond_to?(:each) &&
+                    _value.all? { |x| type.valid?(x) }
 
           errors.add(type.name, I18n.t('errors.messages.invalid'))
         end
lib/scim/kit/v2/attribute_type.rb
@@ -6,7 +6,8 @@ module Scim
       # Represents a scim Attribute type
       class AttributeType
         include Templatable
-        BASE64_FORMAT = %r(\A([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\Z).freeze
+        B64 = %r(\A([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\Z).freeze
+        BOOLEAN_VALUES = [true, false].freeze
         DATATYPES = {
           string: 'string',
           boolean: 'boolean',
@@ -25,12 +26,15 @@ module Scim
           binary: ->(x) { Base64.strict_encode64(x) }
         }.freeze
         VALIDATIONS = {
-          string: ->(x) { x.is_a?(String) },
+          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? },
-          datetime: ->(x) { x.is_a?(DateTime) },
-          binary: ->(x) { x.is_a?(String) && !!x.match(BASE64_FORMAT) }
-        }
+          reference: ->(x) { URI.parse(x) },
+          string: ->(x) { x.is_a?(String) }
+        }.freeze
         attr_accessor :canonical_values
         attr_accessor :case_exact
         attr_accessor :description
@@ -88,7 +92,7 @@ module Scim
         end
 
         def coerce(value)
-          if type_is?(:boolean) && ![true, false].include?(value)
+          if type_is?(:boolean) && !BOOLEAN_VALUES.include?(value)
             raise ArgumentError, value
           end
           return value if multi_valued
spec/scim/kit/v2/attribute_spec.rb
@@ -23,28 +23,28 @@ RSpec.describe Scim::Kit::V2::Attribute do
       end
 
       specify { expect(subject._value).to match_array(%w[superman batman]) }
+    end
 
-      context "when a single value is provided" do
-        before do
-          type.multi_valued = true
-          subject._value = 'batman'
-          subject.valid?
-        end
-
-        specify { expect(subject).not_to be_valid }
-        specify { expect(subject.errors[:user_name]).to be_present }
+    context 'when a single value is provided' do
+      before do
+        type.multi_valued = true
+        subject._value = 'batman'
+        subject.valid?
       end
 
-      context "when the wrong type is used" do
-        before do
-          type.multi_valued = true
-          subject._value = [1.0, 2.0]
-          subject.valid?
-        end
+      specify { expect(subject).not_to be_valid }
+      specify { expect(subject.errors[:user_name]).to be_present }
+    end
 
-        specify { expect(subject).not_to be_valid }
-        specify { expect(subject.errors[:user_name]).to be_present }
+    context 'when the wrong type is used' do
+      before do
+        type.multi_valued = true
+        subject._value = [1.0, 2.0]
+        subject.valid?
       end
+
+      specify { expect(subject).not_to be_valid }
+      specify { expect(subject.errors[:user_name]).to be_present }
     end
 
     context 'when integer' do
@@ -222,7 +222,12 @@ RSpec.describe Scim::Kit::V2::Attribute do
     specify { expect(subject.as_json[:emails]).to match_array([{ value: email, primary: true }, { value: other_email, primary: false }]) }
 
     context 'when the hash is invalid' do
-      xspecify { expect { subject._value = [{ blah: 'blah' }] }.to raise_error(ArgumentError) }
+      before do
+        subject._value = [{ blah: 'blah' }]
+      end
+
+      specify { expect(subject).not_to be_valid }
+      xspecify { expect(subject.errors[:emails]).to be_present }
     end
   end
 end