This repository has been archived by the owner on Jan 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds unique validation to alternative identifiers
All alternate identifiers should be unique.
- Loading branch information
Showing
18 changed files
with
177 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module Validation | ||
class Unique < Base | ||
def validate(field_value) | ||
@errors = [] | ||
return true if field_value.blank? || unchanged_edit? | ||
|
||
Array.wrap(field_value).each do |value| | ||
member = Validation::Member.new(value) | ||
next if !member.exists_by_alternate_id? | ||
|
||
@errors << "#{value} already exists" | ||
end | ||
errors.empty? | ||
end | ||
|
||
private | ||
|
||
def unchanged_edit? | ||
return false unless change_set.resource.persisted? | ||
change_set.send(field) == change_set.resource.send(field) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Validation::Unique, type: :model do | ||
let(:validation_instance) { described_class.new(change_set: change_set, field: field) } | ||
|
||
describe 'validating alternate_ids' do | ||
let(:field) { :alternate_ids } | ||
let(:change_set) { Work::SubmissionChangeSet.new(work) } | ||
|
||
context 'when creating a new resource and the alternate id is unique or does not exist' do | ||
let(:work) { Work::Submission.new } | ||
|
||
it 'is valid' do | ||
expect(validation_instance.validate('i-am-special')).to be_truthy | ||
expect(validation_instance.errors).to be_empty | ||
end | ||
end | ||
|
||
context 'when creating a new resource and the alternate already exists' do | ||
let(:work) { Work::Submission.new } | ||
|
||
before { create(:work, alternate_ids: 'already-used') } | ||
|
||
it 'is valid' do | ||
expect(validation_instance.validate('already-used')).to be_falsey | ||
expect(validation_instance.errors).to contain_exactly('already-used already exists') | ||
end | ||
end | ||
|
||
context 'when updating an existing resource and the id already exists' do | ||
let(:work) { create(:work, alternate_ids: ['my-alt-id']) } | ||
|
||
before do | ||
create(:work, alternate_ids: 'my-updated-alt-id') | ||
change_set.alternate_ids = ['my-updated-alt-id'] | ||
end | ||
|
||
it 'is valid' do | ||
expect(validation_instance.validate('my-updated-alt-id')).to be_falsey | ||
expect(validation_instance.errors).to contain_exactly('my-updated-alt-id already exists') | ||
end | ||
end | ||
|
||
context 'with a blank id' do | ||
let(:work) { Work::Submission.new } | ||
|
||
it 'is valid' do | ||
expect(validation_instance.validate('')).to be_truthy | ||
expect(validation_instance.errors).to be_empty | ||
end | ||
end | ||
|
||
context 'with a nil id' do | ||
let(:work) { Work::Submission.new } | ||
|
||
it 'is valid' do | ||
expect(validation_instance.validate(nil)).to be_truthy | ||
expect(validation_instance.errors).to be_empty | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,4 +62,8 @@ | |
end | ||
end | ||
end | ||
|
||
trait :with_metadata do | ||
alternate_ids { ['alt-work-id'] } | ||
end | ||
end |