Skip to content

Commit 8b99643

Browse files
authored
Merge pull request #3583 from nhsuk/batch-name-validation-import
Ensure batch name is validated on import
2 parents f06951d + 3d4e0f3 commit 8b99643

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

app/models/batch.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ class Batch < ApplicationRecord
4242
scope :not_expired,
4343
-> { where.not(expiry: nil).where("expiry > ?", Time.current) }
4444

45+
NAME_FORMAT = /\A[A-Za-z0-9]+\z/
46+
4547
validates :name,
4648
presence: true,
4749
format: {
48-
with: /\A[A-Za-z0-9]+\z/
50+
with: NAME_FORMAT
4951
},
5052
length: {
5153
minimum: 2,

app/models/immunisation_import_row.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ def validate_batch_name
487487
if batch_name.present?
488488
if batch_name.to_s.length > 100
489489
errors.add(batch_name.header, "is greater than 100 characters long")
490+
elsif batch_name.to_s !~ Batch::NAME_FORMAT
491+
errors.add(batch_name.header, "must be only letters and numbers")
490492
end
491493
elsif offline_recording?
492494
if batch_name.nil?

spec/models/immunisation_import_row_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,17 @@
190190
end
191191
end
192192

193+
context "with an invalid batch name" do
194+
let(:data) { { "VACCINATED" => "Y", "BATCH_NUMBER" => "[invalid]" } }
195+
196+
it "has errors" do
197+
expect(immunisation_import_row).to be_invalid
198+
expect(immunisation_import_row.errors["BATCH_NUMBER"]).to eq(
199+
["must be only letters and numbers"]
200+
)
201+
end
202+
end
203+
193204
context "with an invalid postcode" do
194205
let(:data) { { "PERSON_POSTCODE" => "ABC DEF" } }
195206

0 commit comments

Comments
 (0)