Class CurrencyCode
- Namespace
- Trellis.Primitives
- Assembly
- Trellis.Primitives.dll
Represents an ISO 4217 currency code as a value object.
[JsonConverter(typeof(ParsableJsonConverter<CurrencyCode>))]
public class CurrencyCode : ScalarValueObject<CurrencyCode, string>, IComparable<ValueObject>, IComparable, IEquatable<ValueObject>, IConvertible, IFormattable, IScalarValue<CurrencyCode, string>, IParsable<CurrencyCode>
- Inheritance
-
CurrencyCode
- Implements
- Inherited Members
- Extension Methods
Remarks
Validation Rules (Opinionated):
Valid codes are three ASCII letters (e.g., USD, EUR, GBP). Input is case-insensitive —
"usd", "USD", and "Usd" all parse; the stored value is uppercase via
ToUpperInvariant(). Non-ASCII letters such as German umlauts, Greek, or Cyrillic are
rejected, and codes shorter or longer than three characters are rejected.
Scope of validation. Only the ISO 4217 format (three ASCII letters) is
enforced. The ISO 4217 active-code list is not consulted, so syntactically valid
but reserved or unassigned codes such as XXX, XTS, and ZZZ are accepted.
Applications that need to restrict to currencies actually supported by a payment processor,
reject ISO reserved/test codes, or otherwise impose a narrower policy should layer an
allow-list at the application boundary.
If these rules don't fit your domain (e.g., cryptocurrency ticker symbols longer than
three characters like USDT, DOGE, MATIC), create your own currency-code
value object using the ScalarValueObject<TSelf, T> base class.
Methods
Parse(string?, IFormatProvider?)
Parses a currency code.
public static CurrencyCode Parse(string? s, IFormatProvider? provider)
Parameters
sstringproviderIFormatProvider
Returns
TryCreate(string?, string?)
Attempts to create a currency code from a 3-letter ISO 4217 code.
If fieldName is not provided, validation errors use "currencyCode" as the field name.
public static Result<CurrencyCode> TryCreate(string? value, string? fieldName = null)
Parameters
valuestringThe string value to validate.
fieldNamestringOptional field name for validation error messages. If not provided, defaults to "currencyCode".
Returns
- Result<CurrencyCode>
Success with the CurrencyCode if valid; Failure with Error.InvalidInput otherwise.
TryParse(string?, IFormatProvider?, out CurrencyCode)
Tries to parse a currency code.
public static bool TryParse(string? s, IFormatProvider? provider, out CurrencyCode result)
Parameters
sstringproviderIFormatProviderresultCurrencyCode