skwasjer / IbanNet

C# .NET IBAN validator, parser, builder and generator
Apache License 2.0
119 stars 31 forks source link

Generate regex pattern(s) from `IbanRegistry` country IBAN patterns. #128

Closed skwasjer closed 10 months ago

skwasjer commented 1 year ago

Is your feature request related to a problem? Please describe. Be able to generate simple regex patterns for client-side validation based on the definitions in IbanRegistry. While regex on its own will not fully satisfy strict IBAN validation (no check-digit validation or custom rules, etc.), it will be useful to perform client-side validation to avoid roundtrips. The idea being that we use the same configuration of the IbanRegistry for both client-side and server-side and not have to depend on another client library which may have a different validation behavior.

Describe the solution you'd like Initially just being able to generate a regex pattern from a Pattern. Perhaps later down the line the functionality could be reused to build/support/generate more advanced client-side library support (eg. unobtrustive, or a standalone JS/TS library, etc.).

API proposal

Pattern pattern = IbanRegistry.Default["NL"].Iban.Pattern;
string regexPattern = pattern.ToRegexPattern(); // Produces "^NL\\d{2}[A-Z]{4}\\d{10}$"

// Now we can do smth like:
Regex r = new Regex(regexPattern);
// Or for example generate JS-snippet in Blazor...