-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation attribute for Coordination numbers (#152)
* Add validation attribute for Coordination numbers * Update README.md Correct error in README. * Update validation error message to correct coordination number format * Update test data and source link --------- Co-authored-by: Tor Seldén <[email protected]> Co-authored-by: Peter Örneholm <[email protected]>
- Loading branch information
1 parent
acc3d50
commit 51e2381
Showing
3 changed files
with
107 additions
and
1 deletion.
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
43 changes: 43 additions & 0 deletions
43
src/ActiveLogin.Identity.Swedish.AspNetCore/Validation/CoordinationNumberAttribute.cs
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,43 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace ActiveLogin.Identity.Swedish.AspNetCore.Validation | ||
{ | ||
/// <summary> | ||
/// Validates a Swedish Coordination Number using <see cref="CoordinationNumber"/>. | ||
/// </summary> | ||
public class CoordinationNumberAttribute : ValidationAttribute | ||
{ | ||
/// <summary>Initializes a new instance of the <see cref="CoordinationNumberAttribute"></see> class.</summary> | ||
public CoordinationNumberAttribute() | ||
: base("{0} is not a valid Swedish Coordination Number. It should follow a valid pattern such as YYMMDD-IIIC, YYMMDD+IIIC or YYYYMMDDIIIC.") | ||
{ } | ||
|
||
/// <summary>Initializes a new instance of the <see cref="CoordinationNumberAttribute"></see> class by using the function that enables access to validation resources.</summary> | ||
/// <param name="errorMessageAccessor">The function that enables access to validation resources.</param> | ||
/// <exception cref="T:System.ArgumentNullException"><paramref name="errorMessageAccessor">errorMessageAccessor</paramref> is null.</exception> | ||
public CoordinationNumberAttribute(Func<string> errorMessageAccessor) : base(errorMessageAccessor) | ||
{ } | ||
|
||
/// <summary>Initializes a new instance of the <see cref="CoordinationNumberAttribute"></see> class by using the error message to associate with a validation control.</summary> | ||
/// <param name="errorMessage">The error message to associate with a validation control.</param> | ||
public CoordinationNumberAttribute(string errorMessage) : base(errorMessage) | ||
{ } | ||
|
||
/// <summary>Validates the specified Swedish Coordination Number with respect to the current validation attribute.</summary> | ||
/// <param name="value">The Swedish Coordination Number to validate.</param> | ||
/// <param name="validationContext">The context information about the validation operation.</param> | ||
/// <returns>An instance of the <see cref="System.ComponentModel.DataAnnotations.ValidationResult"></see> class.</returns> | ||
protected override ValidationResult IsValid(object value, ValidationContext validationContext) | ||
{ | ||
var valueString = (string) value; | ||
if (!CoordinationNumber.TryParse(valueString, out _)) | ||
{ | ||
var errorMessage = FormatErrorMessage(validationContext.DisplayName); | ||
return new ValidationResult(errorMessage); | ||
} | ||
|
||
return ValidationResult.Success; | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...Identity.Swedish.AspNetCore.Test/Validation/SwedishCoordinationNumberAttribute_IsValid.cs
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,49 @@ | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using ActiveLogin.Identity.Swedish.AspNetCore.Validation; | ||
using Xunit; | ||
|
||
namespace ActiveLogin.Identity.Swedish.AspNetCore.Test.Validation | ||
{ | ||
/// <remarks> | ||
/// Tested with official test Coordination Numbers from Skatteverket: | ||
/// https://swedish.identityinfo.net/coordinationnumber/testdata | ||
/// </remarks> | ||
public class SwedishCoordinationNumberAttribute_IsValid | ||
{ | ||
[Fact] | ||
public void Returns_Valid_When_Valid_Coordination_Number() | ||
{ | ||
var model = new SampleValidationModel("480977-2389"); | ||
var context = new ValidationContext(model); | ||
var results = new List<ValidationResult>(); | ||
var isValid = Validator.TryValidateObject(model, context, results, true); | ||
|
||
Assert.True(isValid); | ||
Assert.Empty(results); | ||
} | ||
|
||
[Fact] | ||
public void Returns_Invalid_When_Invalid_Coordination_Number() | ||
{ | ||
var model = new SampleValidationModel("A"); | ||
var context = new ValidationContext(model); | ||
var results = new List<ValidationResult>(); | ||
var isValid = Validator.TryValidateObject(model, context, results, true); | ||
|
||
Assert.False(isValid); | ||
Assert.Single(results); | ||
} | ||
|
||
private class SampleValidationModel | ||
{ | ||
public SampleValidationModel(string swedishCoordinationNumber) | ||
{ | ||
SwedishCoordinationNumber = swedishCoordinationNumber; | ||
} | ||
|
||
[CoordinationNumber] | ||
public string SwedishCoordinationNumber { get; } | ||
} | ||
} | ||
} |