Coder Social home page Coder Social logo

turkishid's Introduction

NuGet Version Build status

TurkishId

Validator, model binder and generator tool for Turkish Republic's citizen ID numbers. I've decided to give this a shot last night while I was waiting for a download. And Turkish ID numbers were really popular in Turkish social media last week. The Id number is called "T.C. Kimlik No" (Turkish Republic Identity Number). I decided to use English translation to allow easier handling in international projects.

Usage

I wanted the TurkishIdNumber representation to be used anywhere in the code as a value to pass around when using id's allowing to assume an instance is already validated saving you from redundant checks.

When you want to use it as a representation of an ID number:

using TurkishId;
var id = new TurkishIdNumber("12345678901");
// throws ArgumentException when invalid parameter is passed

or if you just want to validate it:

using TurkishId;
bool reallyValid = TurkishIdNumber.IsValid("12345678901");

You can also use the classic TryParse:

using TurkishId;
if (TurkishIdNumber.TryParse(value, out var id))
{
    // ...
}

or with a nullable return value which can be used with pattern matching:

using TurkishId;
if (TurkishIdNumber.TryParse(value) is TurkishIdNumber id))
{
    // ...
}

NuGet package is here: https://www.nuget.org/packages/TurkishId/

TurkishId.ModelBinder

There is also a model binder package that you can install at https://www.nuget.org/packages/TurkishId.ModelBinder. It's a plug'n'play model binder which lets you to use TurkishIdNumber class directly in your model declarations.

It's not part of the original package because you may not want to have whole MVC as a dependency.

To set it up in an ASP.NET Core project, use this syntax in your ConfigureServices() method in your Startup class:

services.AddMvc(options =>
{
  options.ModelBinderProviders.Insert(0, new TurkishIdModelBinderProvider());
});

and you're done. If you'd like to use it in your Razor Pages app use AddMvcOptions instead:

services.AddRazorPages()
  .AddMvcOptions(options => options.ModelBinderProviders.Insert(0, new TurkishIdModelBinderProvider()));

or, alternatively, if you only use controllers, you can add it to your AddControllers options:

services.AddControllers(options =>
{
  options.ModelBinderProviders.Insert(0, new TurkishIdModelBinderProvider());
});

The model binder package will use ASP.NET Core's model binding message providers. You can now localize them like how you do any other model binder.

Performance

This is probably one of the fastest implementations on .NET. I didn't grind so much on performance but it can easily handle millions of validations per second on my Core i7.

Algorithm

Turkish Republic's ID structure and verification is simple. It's an eleven digit number. If we name each digit as d(n) where leftmost digit is called d1 and the rightmost d11, a given ID is valid if:

d1 > 0

and

n = (d1 + d3 + d5 + d7 + d9) * 7 - (d2 + d4 + d6 + d8)

if n < 0 then n = n + 10

d10 = n mod 10

and

d11 = sum(d1..d10) mod 10

turkishid's People

Contributors

skacurt avatar ssg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

turkishid's Issues

How do I use this?

Can you please give me a short tutorial on how do I use this? From start to finish, what software do I use, what do I do?

"true" value for <nullable> causes compilation to fail on VS2019

<Nullable>true</Nullable>

<Nullable>true</Nullable>

<Nullable>true</Nullable>

VS2019 is complaining about the trues.

CSC : error CS8636: Invalid option 'true' for /nullable; must be 'disable', 'enable', 'warnings' or 'annotations'

As you can see in the error message, according to https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references#nullable-contexts valid settings are enable, warnings, annotations and disable.

With <Nullable>enable</Nullable>s, all three projects are compiled successfully in both VS2017 and VS2019, and the test completed with no errors. So, it seems like enable is forward-compatible.

This recent .NET Framework/Core/Standard things confuses me a lot, don't know if there's and edge case here. What's your thoughts, would a PR be OK?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.