Skip to content

Commit

Permalink
fix wrong error code in validation error message
Browse files Browse the repository at this point in the history
  • Loading branch information
klaus0x7c4 committed Jan 29, 2025
1 parent ef07205 commit 22ae8b4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/vies-dotnet-api/Validators/BGVatValidator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Copyright 2017-2024 Adrian Popescu.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,7 +55,7 @@ protected override VatValidationResult OnValidate(string vat)
VatValidationResult.Failed($"Invalid {CountryCode} VAT number");
}

private static VatValidationResult Validate9DigitVat(ReadOnlySpan<char> vatSpan)
private VatValidationResult Validate9DigitVat(ReadOnlySpan<char> vatSpan)
{
var sum = vatSpan.Sum(MultipliersPhysicalPerson);

Expand Down
6 changes: 3 additions & 3 deletions src/vies-dotnet-api/Validators/LTVatValidator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Copyright 2017-2024 Adrian Popescu.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,7 +49,7 @@ protected override VatValidationResult OnValidate(string vat)
: ValidateTemporaryVat(vatSpan);
}

private static VatValidationResult ValidateNineDigitVat(ReadOnlySpan<char> vatSpan)
private VatValidationResult ValidateNineDigitVat(ReadOnlySpan<char> vatSpan)
{
if (vatSpan[7] != '1')
{
Expand Down Expand Up @@ -78,7 +78,7 @@ private static VatValidationResult ValidateNineDigitVat(ReadOnlySpan<char> vatSp
return ValidateChecksumDigit(vatSpan[8].ToInt(), checkDigit);
}

private static VatValidationResult ValidateTemporaryVat(ReadOnlySpan<char> vatSpan)
private VatValidationResult ValidateTemporaryVat(ReadOnlySpan<char> vatSpan)
{
if (vatSpan[10] != '1')
{
Expand Down
8 changes: 4 additions & 4 deletions src/vies-dotnet-api/VatValidatorAbstract.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Copyright 2017-2024 Adrian Popescu.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,7 @@ namespace Padi.Vies;
/// </summary>
public abstract class VatValidatorAbstract : IVatValidator
{
protected static string CountryCode { get; set; }
protected string CountryCode { get; set; }

/// <summary>
///
Expand All @@ -37,15 +37,15 @@ public VatValidationResult Validate(string vat)
}
protected abstract VatValidationResult OnValidate(string vat);

protected static VatValidationResult ValidateChecksumDigit(int digit, int checkDigit, string message = null)
protected VatValidationResult ValidateChecksumDigit(int digit, int checkDigit, string message = null)
{
var isValid = checkDigit == digit;
return !isValid
? VatValidationResult.Failed(message ?? $"Invalid {CountryCode} VAT: checkValue")
: VatValidationResult.Success();
}

protected static VatValidationResult ValidateChecksumDigit(bool isValid, string message = null)
protected VatValidationResult ValidateChecksumDigit(bool isValid, string message = null)
{
return !isValid
? VatValidationResult.Failed(message ?? $"Invalid {CountryCode} VAT: checkValue")
Expand Down
20 changes: 19 additions & 1 deletion tests/vies-dotnet-api-test/ViesEUUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Copyright 2017-2024 Adrian Popescu.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -435,4 +435,22 @@ public void Should_Validate_Vat(string vatNumber)
VatValidationResult result = ViesManager.IsValid(vatNumber);
Assert.True(result.IsValid, $"VAT '{vatNumber}' is invalid");
}

[Fact]
public void ErrorMessage_Should_Contain_CountryCode()
{
var data = new TheoryData<string, string>
{
{ "IT00000010210", "IT" },
{ "DE000000206", "DE" },
{ "IT00000010210", "IT" } // test duplicate
};

foreach (var item in data)
{
VatValidationResult result = ViesManager.IsValid((string)item[0]);
Assert.False(result.IsValid, $"VAT '{item[0]}' is VALID");
Assert.Contains((string)item[1], result.Error, System.StringComparison.OrdinalIgnoreCase);
}
}
}

0 comments on commit 22ae8b4

Please sign in to comment.