-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Validate all attributes as needed - Add associations with library type and data_type - Add pipeline validation for library_type/data_type
- Loading branch information
James Glover
committed
Jun 10, 2022
1 parent
ea797ab
commit 06c0348
Showing
6 changed files
with
164 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
# Validates that attribute belongs to the configured pipeline | ||
# @example | ||
# validates :library_type, pipeline: :ont | ||
# Requires that the associated attribute responds to | ||
class PipelineValidator < ActiveModel::EachValidator | ||
def initialize(options) | ||
super | ||
|
||
# We'll give some helpful errors if we configure it wrong | ||
raise ArgumentError, 'No target pipeline specified' if expected.nil? | ||
raise ArgumentError, "#{expected} is not a recognised pipeline" unless valid_pipeline? | ||
end | ||
|
||
def validate_each(record, attribute, value) | ||
return if value.blank? | ||
|
||
actual = value.pipeline | ||
return if actual == expected.to_s | ||
|
||
record.errors.add(attribute, :pipeline_invalid, { expected: expected, actual: actual }) | ||
end | ||
|
||
private | ||
|
||
def expected | ||
options[:with] | ||
end | ||
|
||
def valid_pipeline? | ||
Pipelines::ENUMS.key?(expected) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe PipelineValidator do | ||
describe '#validate_each' do | ||
let(:record_class) { Ont::Request } | ||
let(:record) { record_class.new } | ||
let(:attribute) { :library_type } | ||
let(:pipeline) { :ont } | ||
|
||
before do | ||
described_class.new(attributes: [attribute], with: pipeline).validate_each(record, attribute, value) | ||
end | ||
|
||
context 'with a nil value' do | ||
let(:value) { nil } | ||
|
||
# We'll allow nil here, and let the presence validator handle that if | ||
# required | ||
it 'does not add an error to the record' do | ||
expect(record.errors.full_messages).to be_empty | ||
end | ||
end | ||
|
||
context 'with the incorrect pipeline' do | ||
let(:value) { build :library_type, :pacbio } | ||
|
||
it 'adds an error to the record' do | ||
expect(record.errors.full_messages).to include('Library type is in pacbio not ont pipeline') | ||
end | ||
end | ||
|
||
context 'with the correct pipeline' do | ||
let(:value) { build :library_type, pipeline } | ||
|
||
it 'does not add an error to the record' do | ||
expect(record.errors.full_messages).to be_empty | ||
end | ||
end | ||
end | ||
end |