Skip to content

Commit a36524a

Browse files
committed
Add length validations for name questions
1 parent 191a997 commit a36524a

4 files changed

Lines changed: 85 additions & 2 deletions

File tree

app/models/question/name.rb

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

99
validate :full_name_valid?, if: :is_full_name?
10-
validate :first_and_last_name_valid?, unless: :is_full_name?
10+
validate :first_middle_and_last_name_valid?, unless: :is_full_name?
1111

1212
def validate_title
1313
needs_title? && !is_optional?
@@ -34,14 +34,18 @@ def full_name_valid?
3434
return if skipping_question?
3535

3636
errors.add(:full_name, :blank) if full_name.blank?
37+
errors.add(:full_name, :too_long) if full_name.present? && full_name.length > 499
3738
errors
3839
end
3940

40-
def first_and_last_name_valid?
41+
def first_middle_and_last_name_valid?
4142
return if skipping_question?
4243

4344
errors.add(:first_name, :blank) if first_name.blank?
45+
errors.add(:first_name, :too_long) if first_name.present? && first_name.length > 499
4446
errors.add(:last_name, :blank) if last_name.blank?
47+
errors.add(:last_name, :too_long) if last_name.present? && last_name.length > 499
48+
errors.add(:middle_names, :too_long) if include_middle_name? && middle_names.present? && middle_names.length > 499
4549
errors
4650
end
4751

config/locales/cy.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,15 @@ cy:
139139
attributes:
140140
first_name:
141141
blank: Rhowch enw cyntaf
142+
too_long: The first name must be shorter than 500 characters
142143
full_name:
143144
blank: Rhowch enw
145+
too_long: The name must be shorter than 500 characters
144146
last_name:
145147
blank: Rhowch enw olaf
148+
too_long: The last name must be shorter than 500 characters
149+
middle_names:
150+
too_long: The middle name must be shorter than 500 characters
146151
question/national_insurance_number:
147152
attributes:
148153
national_insurance_number:

config/locales/en.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,15 @@ en:
139139
attributes:
140140
first_name:
141141
blank: Enter a first name
142+
too_long: The first name must be shorter than 500 characters
142143
full_name:
143144
blank: Enter a name
145+
too_long: The name must be shorter than 500 characters
144146
last_name:
145147
blank: Enter a last name
148+
too_long: The last name must be shorter than 500 characters
149+
middle_names:
150+
too_long: The middle name must be shorter than 500 characters
146151
question/national_insurance_number:
147152
attributes:
148153
national_insurance_number:

spec/models/question/name_spec.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@
5858
expect(question.show_answer_in_csv).to eq({ "#{question_text} - Full name" => name })
5959
end
6060
end
61+
62+
describe "length validation" do
63+
it "is valid with length under 500 characters" do
64+
question.full_name = "a" * 499
65+
expect(question).to be_valid
66+
expect(question.errors[:full_name]).to be_empty
67+
end
68+
69+
it "is invalid with length over 499 characters" do
70+
question.full_name = "a" * 500
71+
expect(question).not_to be_valid
72+
expect(question.errors[:full_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.full_name.too_long"))
73+
end
74+
end
6175
end
6276

6377
context "when the name question is in first and last name format" do
@@ -147,6 +161,30 @@
147161
})
148162
end
149163
end
164+
165+
describe "length validation" do
166+
before do
167+
question.first_name = "a" * 499
168+
question.last_name = "a" * 499
169+
end
170+
171+
it "is valid when all fields have the maximum allowed length" do
172+
expect(question).to be_valid
173+
expect(question.errors).to be_empty
174+
end
175+
176+
it "is invalid when the first name is over 499 characters" do
177+
question.first_name = "a" * 500
178+
expect(question).not_to be_valid
179+
expect(question.errors[:first_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.first_name.too_long"))
180+
end
181+
182+
it "is invalid when the last name is over 499 characters" do
183+
question.last_name = "a" * 500
184+
expect(question).not_to be_valid
185+
expect(question.errors[:last_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.last_name.too_long"))
186+
end
187+
end
150188
end
151189

152190
context "when the name question is in first, middle and last name format" do
@@ -215,6 +253,37 @@
215253
})
216254
end
217255
end
256+
257+
describe "length validation" do
258+
before do
259+
question.first_name = "a" * 499
260+
question.middle_names = "a" * 499
261+
question.last_name = "a" * 499
262+
end
263+
264+
it "is valid when all fields have the maximum allowed length" do
265+
expect(question).to be_valid
266+
expect(question.errors).to be_empty
267+
end
268+
269+
it "is invalid when the first name is over 499 characters" do
270+
question.first_name = "a" * 500
271+
expect(question).not_to be_valid
272+
expect(question.errors[:first_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.first_name.too_long"))
273+
end
274+
275+
it "is invalid when the middle name is over 499 characters" do
276+
question.middle_names = "a" * 500
277+
expect(question).not_to be_valid
278+
expect(question.errors[:middle_names]).to include(I18n.t("activemodel.errors.models.question/name.attributes.middle_names.too_long"))
279+
end
280+
281+
it "is invalid when the last name is over 499 characters" do
282+
question.last_name = "a" * 500
283+
expect(question).not_to be_valid
284+
expect(question.errors[:last_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.last_name.too_long"))
285+
end
286+
end
218287
end
219288

220289
context "when title_needed is set to true" do

0 commit comments

Comments
 (0)