Skip to content

Commit b474c2f

Browse files
committed
feat: Added additional identifier validation on negative numbers of the correct length for each type
1 parent 501b25b commit b474c2f

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

web/src/Web.App/Validators/OrganisationIdentifierValidator.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,24 @@ public OrganisationIdentifierValidator()
2323

2424
RuleFor(p => p.Value)
2525
.Length(6)
26+
.Must(p => p.All(char.IsDigit))
2627
.When(p => p.Type == OrganisationTypes.School)
2728
.When(p => p.ValueAsInt != null)
28-
.WithMessage("School URN must be 6 characters");
29+
.Configure(rule => rule.MessageBuilder = _ => "School URN must be 6 digits");
2930

3031
RuleFor(p => p.Value)
3132
.Length(8)
33+
.Must(p => p.All(char.IsDigit))
3234
.When(p => p.Type == OrganisationTypes.Trust)
3335
.When(p => p.ValueAsInt != null)
34-
.WithMessage("Trust company number must be 8 characters");
36+
.Configure(rule => rule.MessageBuilder = _ => "Trust company number must be 8 digits");
3537

3638
RuleFor(p => p.Value)
3739
.Length(3)
40+
.Must(p => p.All(char.IsDigit))
3841
.When(p => p.Type == OrganisationTypes.LocalAuthority)
3942
.When(p => p.ValueAsInt != null)
40-
.WithMessage("Local authority code must be 3 characters");
43+
.Configure(rule => rule.MessageBuilder = _ => "Local authority code must be 3 digits");
4144

4245
RuleFor(p => p.ValueAsInt)
4346
.NotEmpty()

web/tests/Web.Tests/Validators/GivenAnOrganisationIdentifierValidator.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public void ShouldFailValidationIfIdentifierInvalid(string type)
7272

7373
[Theory]
7474
[InlineData("1234")]
75+
[InlineData("-12345")]
7576
public void ShouldFailValidationIfUrnInvalid(string urn)
7677
{
7778
var identifier = new OrganisationIdentifier
@@ -82,7 +83,7 @@ public void ShouldFailValidationIfUrnInvalid(string urn)
8283

8384
string[] expected =
8485
[
85-
"School URN must be 6 characters"
86+
"School URN must be 6 digits"
8687
];
8788

8889
var results = _validator.Validate(identifier);
@@ -94,6 +95,7 @@ public void ShouldFailValidationIfUrnInvalid(string urn)
9495

9596
[Theory]
9697
[InlineData("1234")]
98+
[InlineData("-1234567")]
9799
public void ShouldFailValidationIfCompanyNumberInvalid(string companyNumber)
98100
{
99101
var identifier = new OrganisationIdentifier
@@ -104,7 +106,7 @@ public void ShouldFailValidationIfCompanyNumberInvalid(string companyNumber)
104106

105107
string[] expected =
106108
[
107-
"Trust company number must be 8 characters"
109+
"Trust company number must be 8 digits"
108110
];
109111

110112
var results = _validator.Validate(identifier);
@@ -116,6 +118,7 @@ public void ShouldFailValidationIfCompanyNumberInvalid(string companyNumber)
116118

117119
[Theory]
118120
[InlineData("1234")]
121+
[InlineData("-12")]
119122
public void ShouldFailValidationIfLaCodeInvalid(string code)
120123
{
121124
var identifier = new OrganisationIdentifier
@@ -126,7 +129,7 @@ public void ShouldFailValidationIfLaCodeInvalid(string code)
126129

127130
string[] expected =
128131
[
129-
"Local authority code must be 3 characters"
132+
"Local authority code must be 3 digits"
130133
];
131134

132135
var results = _validator.Validate(identifier);

0 commit comments

Comments
 (0)