Skip to content

Commit

Permalink
Merge pull request #74 from s-andringa/multi-valued-simple-type-valid…
Browse files Browse the repository at this point in the history
…ation

Multi-valued simple type validation
  • Loading branch information
pond authored Oct 23, 2023
2 parents faba469 + 92b5042 commit 9114583
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
15 changes: 12 additions & 3 deletions app/models/scimitar/schema/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,21 @@ def valid_complex_type?(value)
end

def valid_simple_type?(value)
valid = (type == 'string' && value.is_a?(String)) ||
if multiValued
valid = value.is_a?(Array) && value.all? { |v| simple_type?(v) }
errors.add(self.name, "or one of its elements has the wrong type. It has to be an array of #{self.type}s.") unless valid
else
valid = simple_type?(value)
errors.add(self.name, "has the wrong type. It has to be a(n) #{self.type}.") unless valid
end
valid
end

def simple_type?(value)
(type == 'string' && value.is_a?(String)) ||
(type == 'boolean' && (value.is_a?(TrueClass) || value.is_a?(FalseClass))) ||
(type == 'integer' && (value.is_a?(Integer))) ||
(type == 'dateTime' && valid_date_time?(value))
errors.add(self.name, "has the wrong type. It has to be a(n) #{self.type}.") unless valid
valid
end

def valid_date_time?(value)
Expand Down
22 changes: 22 additions & 0 deletions spec/models/scimitar/schema/attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@
expect(attribute.errors.messages.to_h).to eql({userName: ['has the wrong type. It has to be a(n) string.']})
end

it 'is valid if multi-valued and type is string and given value is an array of strings' do
attribute = described_class.new(name: 'scopes', multiValued: true, type: 'string')
expect(attribute.valid?(['something', 'something else'])).to be(true)
end

it 'is valid if multi-valued and type is string and given value is an empty array' do
attribute = described_class.new(name: 'scopes', multiValued: true, type: 'string')
expect(attribute.valid?([])).to be(true)
end

it 'is invalid if multi-valued and type is string and given value is not an array' do
attribute = described_class.new(name: 'scopes', multiValued: true, type: 'string')
expect(attribute.valid?('something')).to be(false)
expect(attribute.errors.messages.to_h).to eql({scopes: ['or one of its elements has the wrong type. It has to be an array of strings.']})
end

it 'is invalid if multi-valued and type is string and given value is an array containing another type' do
attribute = described_class.new(name: 'scopes', multiValued: true, type: 'string')
expect(attribute.valid?(['something', 123])).to be(false)
expect(attribute.errors.messages.to_h).to eql({scopes: ['or one of its elements has the wrong type. It has to be an array of strings.']})
end

it 'is valid if type is boolean and given value is boolean' do
expect(described_class.new(name: 'name', type: 'boolean').valid?(false)).to be(true)
expect(described_class.new(name: 'name', type: 'boolean').valid?(true)).to be(true)
Expand Down

0 comments on commit 9114583

Please sign in to comment.