Skip to content

Commit

Permalink
Merge pull request #3 from adia-technology/validation-result
Browse files Browse the repository at this point in the history
Introduced SsnValidator class; returning ValidationResult instead of bool
  • Loading branch information
michaldudak authored Apr 9, 2020
2 parents 398ac26 + 0ca8ea7 commit 10b0f34
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 35 deletions.
29 changes: 29 additions & 0 deletions Adia.SsnValidator.Tests/SsnValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using NUnit.Framework;

namespace Adia.SsnValidator.Tests
{
public class SsnValidatorTests
{
private SsnValidator _validator;

[SetUp]
public void Setup()
{
_validator = new SsnValidator();
}

[Test]
public void GivenCorrectSwissArguments_ShouldRunSwissSsnValidation()
{
var result = _validator.Validate("756.4089.0811.50", CountryCode.CH);
Assert.AreEqual(result, ValidationResult.Valid);
}

[Test]
public void GivenIncorrectCountryCode_ShouldThrowExceptiom()
{
Assert.Throws<NotSupportedException>(() => _validator.Validate("756.4089.0811.50", (CountryCode) 100));
}
}
}
51 changes: 29 additions & 22 deletions Adia.SsnValidator.Tests/Validators/AhvValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Adia.SsnValidator.Validators;
using System;
using Adia.SsnValidator;
using Adia.SsnValidator.Validators;
using NUnit.Framework;

namespace Adia.Ssn.Tests.Validators
{
[TestFixture]
public class AhvValidatorTests
{
private AhvValidator _validator;
Expand All @@ -14,28 +15,34 @@ public void Setup()
_validator = new AhvValidator();
}

[Test]
[TestCase("756.4089.0811.50", true)]
[TestCase("756.5220.1643.81", true)]
[TestCase("756.5837.4745.52", true)]
[TestCase("756.1656.9672.13", true)]
[TestCase("756.6168.0333.64", true)]
[TestCase("756.0246.1057.45", true)]
[TestCase("756.8478.9370.66", true)]
[TestCase("756.0298.4726.97", true)]
[TestCase("756.5113.6381.28", true)]
[TestCase("756.5466.9658.89", true)]
[TestCase("756.5466.965889", false)]
[TestCase("755:5466.9658.89", false)]
[TestCase("7561656967213", false)]
[TestCase("756.1234.5678.95", false)]
[TestCase("7561234567895", false)]
[TestCase("75612345648955", false)]
public void ValidateAhvNumberAsExpected(string ssn, bool expectedResult)
[TestCase("756.4089.0811.50", ValidationResult.Valid)]
[TestCase("756.5220.1643.81", ValidationResult.Valid)]
[TestCase("756.5837.4745.52", ValidationResult.Valid)]
[TestCase("756.1656.9672.13", ValidationResult.Valid)]
[TestCase("756.6168.0333.64", ValidationResult.Valid)]
[TestCase("756.0246.1057.45", ValidationResult.Valid)]
[TestCase("756.8478.9370.66", ValidationResult.Valid)]
[TestCase("756.0298.4726.97", ValidationResult.Valid)]
[TestCase("756.5113.6381.28", ValidationResult.Valid)]
[TestCase("756.5466.9658.89", ValidationResult.Valid)]
[TestCase("756.1234.5678.95", ValidationResult.InvalidChecksum)]
[TestCase("756.5466.965889", ValidationResult.InvalidFormat)]
[TestCase("755:5466.9658.89", ValidationResult.InvalidFormat)]
[TestCase("7561656967213", ValidationResult.InvalidFormat)]
[TestCase("7561234567895", ValidationResult.InvalidFormat)]
[TestCase("75612345648955", ValidationResult.InvalidFormat)]
[TestCase("000.4089.0811.50", ValidationResult.InvalidFormat)]
[TestCase("", ValidationResult.InvalidFormat)]
public void GivenNonNullAhv_ShouldValidateIt(string ssn, ValidationResult expectedResult)
{
var result = _validator.Validate(ssn);

Assert.That(result, Is.EqualTo(expectedResult));
}

[Test]
public void GivenNullAhv_ShouldThrowArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => _validator.Validate(null));
}
}
}
}
4 changes: 2 additions & 2 deletions Adia.SsnValidator/Adia.SsnValidator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.0.1</Version>
<Version>0.1.0</Version>
<Authors>PiotrSzperka; doodack</Authors>
<Company>Adia</Company>
<Description>C# library for validating SSN number for countries supported by Adia</Description>
<Description>C# library for validating Social Security Numbers for countries supported by Adia</Description>
<Copyright>Adia</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/adia-technology/Adia.SsnValidator</PackageProjectUrl>
Expand Down
1 change: 1 addition & 0 deletions Adia.SsnValidator/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly:System.Runtime.CompilerServices.InternalsVisibleTo("Adia.SsnValidator.Tests")]
12 changes: 12 additions & 0 deletions Adia.SsnValidator/SsnValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Adia.SsnValidator
{
public class SsnValidator
{
public ValidationResult Validate(string ssn, CountryCode country)
{
var factory = new SsnValidatorFactory();
var validator = factory.Create(country);
return validator.Validate(ssn);
}
}
}
4 changes: 2 additions & 2 deletions Adia.SsnValidator/SsnValidatorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Adia.SsnValidator
{
public class SsnValidatorFactory
internal class SsnValidatorFactory
{
public IValidator Create(CountryCode country)
{
Expand All @@ -13,7 +13,7 @@ public IValidator Create(CountryCode country)
return new AhvValidator();

default:
throw new NotSupportedException("Your country is not supported");
throw new NotSupportedException($"The provided country ({country}) is not supported.");
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions Adia.SsnValidator/ValidationResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Adia.SsnValidator
{
public enum ValidationResult
{
Valid,
InvalidFormat,
InvalidChecksum
}
}
18 changes: 12 additions & 6 deletions Adia.SsnValidator/Validators/AhvValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

namespace Adia.SsnValidator.Validators
{
public class AhvValidator : IValidator
internal sealed class AhvValidator : IValidator
{
public bool Validate(string ahv)
public ValidationResult Validate(string ahv)
{
if (ahv == null)
{
throw new ArgumentNullException(nameof(ahv));
}

var regex = new Regex(@"^756\.\d{4}\.\d{4}\.\d{2}$");
if(!regex.IsMatch(ahv))
if (!regex.IsMatch(ahv))
{
return false;
return ValidationResult.InvalidFormat;
}

var ahv13 = ahv.Where(char.IsDigit).ToArray();
Expand All @@ -33,7 +38,8 @@ public bool Validate(string ahv)
var nextTimesTen = Math.Ceiling(totalChecksum / 10.0) * 10;
var checksumDigit = (int)nextTimesTen - totalChecksum;

return checksumDigit == int.Parse(ahv.Last().ToString());
var isChecksumCorrect = checksumDigit == int.Parse(ahv.Last().ToString());
return isChecksumCorrect ? ValidationResult.Valid : ValidationResult.InvalidChecksum;
}
}
}
}
6 changes: 3 additions & 3 deletions Adia.SsnValidator/Validators/IValidator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Adia.SsnValidator.Validators
{
public interface IValidator
internal interface IValidator
{
bool Validate(string ssn);
ValidationResult Validate(string ssn);
}
}
}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.1.0

* **BREAKING CHANGE** Changed the main validation class to be `SsnValidator`. This is the only publicly available class in the library.
* `SsnValidator.Validate` returns a `ValidationResult` instance that can be used to determine why an SSN is invalid.

## 0.0.1

Initial version with support for Swiss SSN (AHV).

0 comments on commit 10b0f34

Please sign in to comment.