Skip to content

Instantly share code, notes, and snippets.

@mvodep
Created September 9, 2023 08:39
Show Gist options
  • Save mvodep/5ff9f5eab2940cc738fd886b5d5d8735 to your computer and use it in GitHub Desktop.
Save mvodep/5ff9f5eab2940cc738fd886b5d5d8735 to your computer and use it in GitHub Desktop.
using System;
using System.Text.RegularExpressions;
record ProductId
{
public string URN { get; }
private static readonly string URNPattern = @"^urn:ISBN:\d{1,5}-\d{1,7}-\d{1,7}-\d{1}$";
public ProductId(string urn)
{
if (!IsValidURN(urn))
{
throw new ArgumentException("Invalid URN: ISBN format is not valid.", nameof(urn));
}
URN = urn;
}
private static bool IsValidURN(string urn)
{
return Regex.IsMatch(urn, URNPattern);
}
}
class Program
{
static void Main()
{
try
{
ProductId book = new ProductId("urn:ISBN:12345-6789101-2345678-9");
Console.WriteLine($"URN: {book.URN}");
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment