-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from DentallApp/patch-21
Validate if the identity document is Ecuadorian
- Loading branch information
Showing
24 changed files
with
413 additions
and
40 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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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
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
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
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
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,6 @@ | ||
namespace DentallApp.Infrastructure.Services; | ||
|
||
public class FakeIdentityDocument : IIdentityDocumentValidator | ||
{ | ||
public Result IsValid(string document) => Result.Success(); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Plugins/IdentityDocumentEcuador/DependencyServicesRegisterer.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,11 @@ | ||
[assembly: Plugin(typeof(DependencyServicesRegisterer))] | ||
|
||
namespace Plugin.IdentityDocument.Ecuador; | ||
|
||
public class DependencyServicesRegisterer : IDependencyServicesRegisterer | ||
{ | ||
public void RegisterServices(IServiceCollection services, IConfiguration configuration) | ||
{ | ||
services.AddSingleton<IIdentityDocumentValidator, IdentityDocumentValidator>(); | ||
} | ||
} |
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,5 @@ | ||
global using DentallApp.Shared.Interfaces; | ||
global using DentallApp.Shared.Resources.ApiResponses; | ||
global using SimpleResults; | ||
global using CPlugin.Net; | ||
global using Plugin.IdentityDocument.Ecuador; |
68 changes: 68 additions & 0 deletions
68
src/Plugins/IdentityDocumentEcuador/IdentityDocumentValidator.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,68 @@ | ||
namespace Plugin.IdentityDocument.Ecuador; | ||
|
||
/// <summary> | ||
/// Represents a validator for Ecuadorian identity documents. | ||
/// </summary> | ||
/// <remarks> | ||
/// Link to the algorithm: <see href="https://www.skypack.dev/view/udv-ec"/> | ||
/// </remarks> | ||
public class IdentityDocumentValidator : IIdentityDocumentValidator | ||
{ | ||
public Result IsValid(string document) | ||
{ | ||
if (string.IsNullOrWhiteSpace(document)) | ||
return Result.Invalid(Messages.DocumentIsEmpty); | ||
|
||
if (document.Length != 10) | ||
return Result.Invalid(string.Format(Messages.DocumentMaxCharacters, 10)); | ||
|
||
if(IsNotNumeric(document)) | ||
return Result.Invalid(Messages.DocumentMustBeNumeric); | ||
|
||
if(HasInvalidRegionDigit(document)) | ||
return Result.Invalid(Messages.DocumentIsInvalid); | ||
|
||
int verificationDigit = document[^1].ToInt(); | ||
int total = 0; | ||
bool isEvenPosition = false; | ||
// The verification digit is not considered. | ||
int len = document.Length - 1; | ||
for (int position = 0; position < len; position++) | ||
{ | ||
isEvenPosition = !isEvenPosition; | ||
int coefficient = isEvenPosition ? 2 : 1; | ||
int digit = document[position].ToInt() * coefficient; | ||
int result = digit > 9 ? digit - 9 : digit; | ||
total += result; | ||
} | ||
|
||
bool isValidDocument = | ||
(total % 10 == 0 && verificationDigit == 0) || | ||
(10 - (total % 10) == verificationDigit); | ||
|
||
return isValidDocument ? | ||
Result.Success() : | ||
Result.Invalid(Messages.DocumentIsInvalid); | ||
} | ||
|
||
private static bool IsNotNumeric(string document) | ||
=> document.Where(c => c is < '0' or > '9').Any(); | ||
|
||
private static bool HasInvalidRegionDigit(string document) | ||
=> !HasValidRegionDigit(document); | ||
|
||
private static bool HasValidRegionDigit(string document) | ||
{ | ||
// The digit of the region is obtained, which are the first two digits. | ||
int regionDigit = int.Parse(document[..2]); | ||
|
||
// It is validated if the region digit exists in Ecuador, which is divided into 24 regions. | ||
// 30 is assigned for Ecuadorians abroad. | ||
return regionDigit is (>= 1 and <= 24) or 30; | ||
} | ||
} | ||
|
||
public static class CharExtensions | ||
{ | ||
public static int ToInt(this char c) => c - '0'; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Plugins/IdentityDocumentEcuador/Plugin.IdentityDocument.Ecuador.csproj
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,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Library</OutputType> | ||
<RootNamespace>Plugin.IdentityDocument.Ecuador</RootNamespace> | ||
</PropertyGroup> | ||
|
||
</Project> |
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,13 @@ | ||
namespace DentallApp.Shared.Interfaces; | ||
|
||
/// <summary> | ||
/// Represents a validator to validate identity documents. | ||
/// </summary> | ||
public interface IIdentityDocumentValidator | ||
{ | ||
/// <summary> | ||
/// Checks if an identity document is valid. | ||
/// </summary> | ||
/// <param name="document">The document to validate.</param> | ||
Result IsValid(string document); | ||
} |
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
Oops, something went wrong.