defmodule QRNBU.Validators.Reference do
@moduledoc """
Validator for payment reference number.
Validates reference strings used to identify specific invoices or transactions.
"""
@max_length 35
@doc """
Validates reference string.
## Rules
- Maximum 35 characters
- Any alphanumeric characters and common separators allowed
## Examples
iex> QRNBU.Validators.Reference.validate("INV-12345")
{:ok, "INV-12345"}
iex> QRNBU.Validators.Reference.validate("")
{:error, "Reference cannot be empty"}
iex> QRNBU.Validators.Reference.validate(String.duplicate("A", 36))
{:error, "Reference must not exceed 35 characters"}
"""
@spec validate(String.t()) :: {:ok, String.t()} | {:error, String.t()}
def validate(reference) when is_binary(reference) do
cond do
String.trim(reference) == "" ->
{:error, "Reference cannot be empty"}
String.length(reference) > @max_length ->
{:error, "Reference must not exceed #{@max_length} characters"}
true ->
{:ok, reference}
end
end
def validate(_), do: {:error, "Reference must be a string"}
end