Skip to content

Commit 653d86e

Browse files
committed
Add length validations for name questions
1 parent 298b1eb commit 653d86e

4 files changed

Lines changed: 108 additions & 6 deletions

File tree

app/models/question/name.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@ class Name < Question::QuestionBase
66
attribute :middle_names
77
attribute :last_name
88

9+
validates :title, length: { maximum: 99 }, allow_blank: true
910
validate :full_name_valid?, if: :is_full_name?
10-
validate :first_and_last_name_valid?, unless: :is_full_name?
11-
12-
def validate_title
13-
needs_title? && !is_optional?
14-
end
11+
validate :first_middle_and_last_name_valid?, unless: :is_full_name?
1512

1613
def needs_title?
1714
answer_settings.present? && answer_settings&.title_needed == "true"
@@ -34,14 +31,18 @@ def full_name_valid?
3431
return if skipping_question?
3532

3633
errors.add(:full_name, :blank) if full_name.blank?
34+
errors.add(:full_name, :too_long) if full_name.present? && full_name.length > 499
3735
errors
3836
end
3937

40-
def first_and_last_name_valid?
38+
def first_middle_and_last_name_valid?
4139
return if skipping_question?
4240

4341
errors.add(:first_name, :blank) if first_name.blank?
42+
errors.add(:first_name, :too_long) if first_name.present? && first_name.length > 499
43+
errors.add(:middle_names, :too_long) if include_middle_name? && middle_names.present? && middle_names.length > 499
4444
errors.add(:last_name, :blank) if last_name.blank?
45+
errors.add(:last_name, :too_long) if last_name.present? && last_name.length > 499
4546
errors
4647
end
4748

config/locales/cy.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,17 @@ cy:
138138
attributes:
139139
first_name:
140140
blank: Rhowch enw cyntaf
141+
too_long: The first name must be shorter than 500 characters
141142
full_name:
142143
blank: Rhowch enw
144+
too_long: The name must be shorter than 500 characters
143145
last_name:
144146
blank: Rhowch enw olaf
147+
too_long: The last name must be shorter than 500 characters
148+
middle_names:
149+
too_long: The middle name must be shorter than 500 characters
150+
title:
151+
too_long: The title must be shorter than 100 characters
145152
question/national_insurance_number:
146153
attributes:
147154
national_insurance_number:

config/locales/en.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,17 @@ en:
138138
attributes:
139139
first_name:
140140
blank: Enter a first name
141+
too_long: The first name must be shorter than 500 characters
141142
full_name:
142143
blank: Enter a name
144+
too_long: The name must be shorter than 500 characters
143145
last_name:
144146
blank: Enter a last name
147+
too_long: The last name must be shorter than 500 characters
148+
middle_names:
149+
too_long: The middle name must be shorter than 500 characters
150+
title:
151+
too_long: The title must be shorter than 100 characters
145152
question/national_insurance_number:
146153
attributes:
147154
national_insurance_number:

spec/models/question/name_spec.rb

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@
1212

1313
it_behaves_like "a question model"
1414

15+
shared_examples "title length validation" do
16+
it "is valid when the title is less than 100 characters" do
17+
question.title = "a" * 99
18+
expect(question).to be_valid
19+
expect(question.errors).to be_empty
20+
end
21+
22+
it "is invalid when the title is more than 99 characters" do
23+
question.title = "a" * 100
24+
expect(question).not_to be_valid
25+
expect(question.errors[:title]).to include(I18n.t("activemodel.errors.models.question/name.attributes.title.too_long"))
26+
end
27+
end
28+
1529
context "when the name question is in full name format" do
1630
context "when the answer is empty" do
1731
it "returns invalid with blank full_name field" do
@@ -58,6 +72,20 @@
5872
expect(question.show_answer_in_csv).to eq({ "#{question_text} - Full name" => name })
5973
end
6074
end
75+
76+
describe "length validation" do
77+
it "is valid with length under 500 characters" do
78+
question.full_name = "a" * 499
79+
expect(question).to be_valid
80+
expect(question.errors[:full_name]).to be_empty
81+
end
82+
83+
it "is invalid with length over 499 characters" do
84+
question.full_name = "a" * 500
85+
expect(question).not_to be_valid
86+
expect(question.errors[:full_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.full_name.too_long"))
87+
end
88+
end
6189
end
6290

6391
context "when the name question is in first and last name format" do
@@ -147,6 +175,30 @@
147175
})
148176
end
149177
end
178+
179+
describe "length validation" do
180+
before do
181+
question.first_name = "a" * 499
182+
question.last_name = "a" * 499
183+
end
184+
185+
it "is valid when all fields have the maximum allowed length" do
186+
expect(question).to be_valid
187+
expect(question.errors).to be_empty
188+
end
189+
190+
it "is invalid when the first name is over 499 characters" do
191+
question.first_name = "a" * 500
192+
expect(question).not_to be_valid
193+
expect(question.errors[:first_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.first_name.too_long"))
194+
end
195+
196+
it "is invalid when the last name is over 499 characters" do
197+
question.last_name = "a" * 500
198+
expect(question).not_to be_valid
199+
expect(question.errors[:last_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.last_name.too_long"))
200+
end
201+
end
150202
end
151203

152204
context "when the name question is in first, middle and last name format" do
@@ -215,6 +267,37 @@
215267
})
216268
end
217269
end
270+
271+
describe "length validation" do
272+
before do
273+
question.first_name = "a" * 499
274+
question.middle_names = "a" * 499
275+
question.last_name = "a" * 499
276+
end
277+
278+
it "is valid when all fields have the maximum allowed length" do
279+
expect(question).to be_valid
280+
expect(question.errors).to be_empty
281+
end
282+
283+
it "is invalid when the first name is over 499 characters" do
284+
question.first_name = "a" * 500
285+
expect(question).not_to be_valid
286+
expect(question.errors[:first_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.first_name.too_long"))
287+
end
288+
289+
it "is invalid when the middle name is over 499 characters" do
290+
question.middle_names = "a" * 500
291+
expect(question).not_to be_valid
292+
expect(question.errors[:middle_names]).to include(I18n.t("activemodel.errors.models.question/name.attributes.middle_names.too_long"))
293+
end
294+
295+
it "is invalid when the last name is over 499 characters" do
296+
question.last_name = "a" * 500
297+
expect(question).not_to be_valid
298+
expect(question.errors[:last_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.last_name.too_long"))
299+
end
300+
end
218301
end
219302

220303
context "when title_needed is set to true" do
@@ -333,6 +416,8 @@
333416
"#{question_text} - Title" => title,
334417
})
335418
end
419+
420+
include_examples "title length validation"
336421
end
337422
end
338423

@@ -400,6 +485,8 @@
400485
"#{question_text} - Title" => title,
401486
})
402487
end
488+
489+
include_examples "title length validation"
403490
end
404491
end
405492
end

0 commit comments

Comments
 (0)