Skip to content

Commit

Permalink
Add validation attribute for Coordination numbers (#152)
Browse files Browse the repository at this point in the history
* 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 <tor.selden@nethouse.se>
Co-authored-by: Peter Örneholm <peter@orneholm.com>
  • Loading branch information
3 people committed Jan 3, 2024
1 parent acc3d50 commit 51e2381
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,21 @@ Therefore they are exposed as extension methods in the C# api and are suffixed w

#### ASP.NET Core MVC

CoordinationNumberAttribute: To be added.
If used to validate input in an ASP.NET Core MVC project, the `CoordinationNumberAttribute` can be used like this:

```c#
public class SampleDataModel
{
[CoordinationNumber]
public string CoordinationNumber { get; set; }
}
```

The `CoordinationNumberAttribute` attribute is available through a separate package:

```console
dotnet add package ActiveLogin.Identity.Swedish.AspNetCore
```

### 3. Use the library in your F# project

Expand Down
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;
}
}
}
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; }
}
}
}

0 comments on commit 51e2381

Please sign in to comment.