From 7e631e015215ecef03b188d9727ddcf0e5853119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klaus=20N=C3=B6ske?= <40196862+klaus0x7c4@users.noreply.github.com> Date: Wed, 29 Jan 2025 20:05:07 +0100 Subject: [PATCH] fix wrong country code in validation error message --- .../Validators/BGVatValidator.cs | 4 ++-- .../Validators/LTVatValidator.cs | 6 +++--- src/vies-dotnet-api/VatValidatorAbstract.cs | 8 ++++---- tests/vies-dotnet-api-test/ViesEUUnitTests.cs | 20 ++++++++++++++++++- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/vies-dotnet-api/Validators/BGVatValidator.cs b/src/vies-dotnet-api/Validators/BGVatValidator.cs index 7090055..3083f6c 100644 --- a/src/vies-dotnet-api/Validators/BGVatValidator.cs +++ b/src/vies-dotnet-api/Validators/BGVatValidator.cs @@ -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. @@ -55,7 +55,7 @@ protected override VatValidationResult OnValidate(string vat) VatValidationResult.Failed($"Invalid {CountryCode} VAT number"); } - private static VatValidationResult Validate9DigitVat(ReadOnlySpan vatSpan) + private VatValidationResult Validate9DigitVat(ReadOnlySpan vatSpan) { var sum = vatSpan.Sum(MultipliersPhysicalPerson); diff --git a/src/vies-dotnet-api/Validators/LTVatValidator.cs b/src/vies-dotnet-api/Validators/LTVatValidator.cs index 0224ee8..bce386b 100644 --- a/src/vies-dotnet-api/Validators/LTVatValidator.cs +++ b/src/vies-dotnet-api/Validators/LTVatValidator.cs @@ -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. @@ -49,7 +49,7 @@ protected override VatValidationResult OnValidate(string vat) : ValidateTemporaryVat(vatSpan); } - private static VatValidationResult ValidateNineDigitVat(ReadOnlySpan vatSpan) + private VatValidationResult ValidateNineDigitVat(ReadOnlySpan vatSpan) { if (vatSpan[7] != '1') { @@ -78,7 +78,7 @@ private static VatValidationResult ValidateNineDigitVat(ReadOnlySpan vatSp return ValidateChecksumDigit(vatSpan[8].ToInt(), checkDigit); } - private static VatValidationResult ValidateTemporaryVat(ReadOnlySpan vatSpan) + private VatValidationResult ValidateTemporaryVat(ReadOnlySpan vatSpan) { if (vatSpan[10] != '1') { diff --git a/src/vies-dotnet-api/VatValidatorAbstract.cs b/src/vies-dotnet-api/VatValidatorAbstract.cs index 2aab635..20b7e77 100644 --- a/src/vies-dotnet-api/VatValidatorAbstract.cs +++ b/src/vies-dotnet-api/VatValidatorAbstract.cs @@ -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. @@ -20,7 +20,7 @@ namespace Padi.Vies; /// public abstract class VatValidatorAbstract : IVatValidator { - protected static string CountryCode { get; set; } + protected string CountryCode { get; set; } /// /// @@ -37,7 +37,7 @@ 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 @@ -45,7 +45,7 @@ protected static VatValidationResult ValidateChecksumDigit(int digit, int checkD : 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") diff --git a/tests/vies-dotnet-api-test/ViesEUUnitTests.cs b/tests/vies-dotnet-api-test/ViesEUUnitTests.cs index 4e29bb9..5092148 100644 --- a/tests/vies-dotnet-api-test/ViesEUUnitTests.cs +++ b/tests/vies-dotnet-api-test/ViesEUUnitTests.cs @@ -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. @@ -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 + { + { "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); + } + } }