-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added
AcceptCountryRule
to restrict validation to a specific …
…set of countries.
- Loading branch information
Showing
6 changed files
with
156 additions
and
3 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
16 changes: 16 additions & 0 deletions
16
src/IbanNet/Validation/Results/CountryNotAcceptedResult.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,16 @@ | ||
using IbanNet.Registry; | ||
|
||
namespace IbanNet.Validation.Results; | ||
|
||
/// <inheritdoc /> | ||
public class CountryNotAcceptedResult : ErrorResult | ||
{ | ||
/// <summary> | ||
/// The result returned when the IBAN is not accepted because of restrictions by country. | ||
/// </summary> | ||
/// <param name="country">The country that was rejected.</param> | ||
public CountryNotAcceptedResult(IbanCountry country) | ||
: base(string.Format(Resources.CountryNotAcceptedResult_Bank_account_numbers_from_country_0_are_not_accepted, country.DisplayName)) | ||
{ | ||
} | ||
} |
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,39 @@ | ||
using IbanNet.Validation.Results; | ||
|
||
namespace IbanNet.Validation.Rules; | ||
|
||
/// <summary> | ||
/// A validation rule that accepts only specific countries. | ||
/// The rule can be used if your use case needs a limitation on countries to allow. | ||
/// </summary> | ||
/// <remarks>Returns <see cref="CountryNotAcceptedResult" /> on validation failures.</remarks> | ||
public class AcceptCountryRule : IIbanValidationRule | ||
{ | ||
private readonly ISet<string> _acceptedCountryCodes; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AcceptCountryRule" /> using specified <paramref name="acceptedCountryCodes" />. | ||
/// </summary> | ||
/// <param name="acceptedCountryCodes">An enumerable of accepted country codes (2 letter ISO region name)</param> | ||
public AcceptCountryRule(IEnumerable<string> acceptedCountryCodes) | ||
{ | ||
if (acceptedCountryCodes is null) | ||
{ | ||
throw new ArgumentNullException(nameof(acceptedCountryCodes)); | ||
} | ||
|
||
_acceptedCountryCodes = new HashSet<string>(acceptedCountryCodes.Select(cc => cc.ToUpperInvariant()), StringComparer.Ordinal); | ||
if (_acceptedCountryCodes.Count == 0) | ||
{ | ||
throw new ArgumentException(Resources.ArgumentException_At_least_one_country_code_must_be_provided, nameof(acceptedCountryCodes)); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public ValidationRuleResult Validate(ValidationRuleContext context) | ||
{ | ||
return _acceptedCountryCodes.Contains(context.Country!.TwoLetterISORegionName) | ||
? ValidationRuleResult.Success | ||
: new CountryNotAcceptedResult(context.Country); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
test/IbanNet.Tests/Validation/Rules/AcceptCountryRuleTests.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,72 @@ | ||
using IbanNet.Registry; | ||
using IbanNet.Validation.Results; | ||
|
||
namespace IbanNet.Validation.Rules; | ||
|
||
public class AcceptCountryRuleTests | ||
{ | ||
[Theory] | ||
[InlineData("NL91ABNA0417164300")] | ||
[InlineData("BE68539007547034")] | ||
public void Given_that_country_code_is_accepted_when_validating_it_should_return_success(string value) | ||
{ | ||
var sut = new AcceptCountryRule(new[] { "NL", "BE" }); | ||
IbanCountry ibanCountry = IbanRegistry.Default[value.Substring(0, 2)]; | ||
|
||
// Act | ||
ValidationRuleResult actual = sut.Validate(new ValidationRuleContext(value, ibanCountry)); | ||
|
||
// Assert | ||
actual.Should().Be(ValidationRuleResult.Success); | ||
} | ||
|
||
[Theory] | ||
[InlineData("NL91ABNA0417164300")] | ||
[InlineData("BE68539007547034")] | ||
public void Given_that_country_code_is_not_accepted_when_validating_it_should_return_error(string value) | ||
{ | ||
var sut = new AcceptCountryRule(new[] { "DE", "FR" }); | ||
IbanCountry ibanCountry = IbanRegistry.Default[value.Substring(0, 2)]; | ||
|
||
// Act | ||
ValidationRuleResult actual = sut.Validate(new ValidationRuleContext(value, ibanCountry)); | ||
|
||
// Assert | ||
actual.Should() | ||
.BeOfType<CountryNotAcceptedResult>() | ||
.Which.ErrorMessage.Should() | ||
.Be(string.Format(Resources.CountryNotAcceptedResult_Bank_account_numbers_from_country_0_are_not_accepted, ibanCountry.DisplayName)); | ||
} | ||
|
||
[Fact] | ||
public void Given_that_list_is_null_when_creating_rule_it_should_throw() | ||
{ | ||
IEnumerable<string> acceptedCountryCodes = null; | ||
|
||
// Act | ||
// ReSharper disable once AssignNullToNotNullAttribute | ||
Func<AcceptCountryRule> act = () => new AcceptCountryRule(acceptedCountryCodes); | ||
|
||
// Assert | ||
act.Should() | ||
.ThrowExactly<ArgumentNullException>() | ||
.Which.ParamName.Should() | ||
.Be(nameof(acceptedCountryCodes)); | ||
} | ||
|
||
[Fact] | ||
public void Given_that_list_is_empty_when_creating_rule_it_should_throw() | ||
{ | ||
IEnumerable<string> acceptedCountryCodes = Enumerable.Empty<string>(); | ||
|
||
// Act | ||
Func<AcceptCountryRule> act = () => new AcceptCountryRule(acceptedCountryCodes); | ||
|
||
// Assert | ||
act.Should() | ||
.ThrowExactly<ArgumentException>() | ||
.WithMessage(Resources.ArgumentException_At_least_one_country_code_must_be_provided + "*") | ||
.Which.ParamName.Should() | ||
.Be(nameof(acceptedCountryCodes)); | ||
} | ||
} |