Overview
Currencies are fundamental entities in the Currency Exchange API. Each currency is represented by theCurrency model, which enforces strict validation rules to ensure data integrity and compliance with international standards.
Currency Model Structure
TheCurrency class uses an immutable design pattern with a private constructor and a public factory method for creation.
CurrencyExchange.Core/Models/Currency.cs
Properties
Unique identifier for the currency
ISO 4217 currency code (e.g., “USD”, “EUR”, “RUB”)
Full name of the currency (e.g., “US Dollar”, “Euro”)
Currency symbol (e.g., ”$”, ”€”, ”₽“)
Creating Currencies
Currencies are created using the staticCreate method, which performs all necessary validations:
The currency code is automatically converted to uppercase before validation, ensuring consistency across the system.
Validation Rules
The Currency model enforces strict validation rules using regular expressions and length constraints:Currency Code Validation
Currency Code Validation
Length: 3-5 charactersPattern: Only uppercase letters (A-Z)Regex:
[^A-Z] (rejects any non-uppercase letter)Examples:- ✅ Valid: “USD”, “EUR”, “RUB”, “GBP”
- ❌ Invalid: “us” (too short), “usd” (lowercase), “U$D” (special character)
Full Name Validation
Full Name Validation
Length: 3-60 charactersPattern: Letters (Latin and Cyrillic), parentheses, and spacesRegex:
[^A-Za-zА-Яа-яЁё() ] (rejects anything except allowed characters)Examples:- ✅ Valid: “US Dollar”, “Euro”, “Российский рубль”
- ❌ Invalid: ”$” (too short), “Currency@123” (special characters)
Currency Sign Validation
Currency Sign Validation
Length: 1-3 charactersPattern: No spaces or newlinesRegex:
[ \n] (rejects spaces and newlines)Examples:- ✅ Valid: ”$”, ”€”, ”₽”, ”£”
- ❌ Invalid: "" (empty), ”$ ” (contains space)
Validation Implementation
The validation logic is centralized in a static method that checks for null/empty values, length constraints, and pattern matching:CurrencyExchange.Core/Models/Currency.cs
ISO 4217 Currency Codes
The Currency Exchange API follows ISO 4217 standards for currency codes. Common examples include:| Code | Currency | Symbol |
|---|---|---|
| USD | US Dollar | $ |
| EUR | Euro | € |
| RUB | Russian Ruble | ₽ |
| GBP | British Pound | £ |
| JPY | Japanese Yen | ¥ |
| CNY | Chinese Yuan | ¥ |
While ISO 4217 standard codes are typically 3 characters, the API allows up to 5 characters to accommodate custom or future currency codes.
Error Handling
When validation fails, theCreate method throws an ArgumentException with descriptive error messages:
Best Practices
Always Use the Create Method
Never attempt to instantiate
Currency directly. Always use the static Create method to ensure all validation rules are applied.Handle Validation Errors
Wrap currency creation in try-catch blocks to handle validation errors gracefully and provide user-friendly error messages.
Use Standard ISO Codes
Stick to official ISO 4217 currency codes whenever possible to ensure compatibility with external systems and services.