Commit bbcc51b
Changed files (3)
lib/scim/kit/v2/resource.rb
@@ -1,10 +1,12 @@
# frozen_string_literal: true
+require 'active_model'
module Scim
module Kit
module V2
# Represents a SCIM Resource
class Resource
+ include ::ActiveModel::Validations
include Attributable
include Templatable
@@ -12,6 +14,9 @@ module Scim
attr_reader :meta
attr_reader :schemas
+ validates_presence_of :id
+ validate :schema_validations
+
def initialize(schemas:, location:)
@meta = Meta.new(schemas[0].name, location)
@schemas = schemas
@@ -19,6 +24,22 @@ module Scim
define_attributes_for(schema.attributes)
end
end
+
+ private
+
+ def schema_validations
+ schemas.each do |schema|
+ schema.attributes.each do |type|
+ validate_attribute(type)
+ end
+ end
+ end
+
+ def validate_attribute(type)
+ attribute = read_attribute(type.name)
+
+ errors.add(type.name, "is required") if type.required && attribute.nil?
+ end
end
end
end
spec/scim/kit/v2/resource_spec.rb
@@ -116,4 +116,32 @@ RSpec.describe Scim::Kit::V2::Resource do
specify { expect(subject.department).to eql('voltron') }
specify { expect(subject.as_json[extension_id][:department]).to eql('voltron') }
end
+
+ describe "#valid?" do
+ context "when invalid" do
+ before { subject.valid? }
+
+ specify { expect(subject).not_to be_valid }
+ specify { expect(subject.errors[:id]).to be_present }
+ end
+
+ context "when valid" do
+ before { subject.id = SecureRandom.uuid }
+
+ specify { expect(subject).to be_valid }
+ end
+
+ context "when a required attribute is blank" do
+ before do
+ schema.add_attribute(name: 'userName') do |x|
+ x.required = true
+ end
+ subject.id = SecureRandom.uuid
+ subject.valid?
+ end
+
+ specify { expect(subject).not_to be_valid }
+ specify { expect(subject.errors[:user_name]).to be_present }
+ end
+ end
end
scim-kit.gemspec
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
end
spec.require_paths = ['lib']
+ spec.add_dependency 'activemodel', '>= 5.2.0'
spec.add_dependency 'tilt', '~> 2.0'
spec.add_dependency 'tilt-jbuilder', '~> 0.7'
spec.add_development_dependency 'bundler', '~> 1.17'