class

CrystalIBAN::Validator

Inherits Reference < Object

Validates IBAN strings against the ISO 13616 standard.

Constructors

new

Creates a validator pre-loaded with the bundled 49-country IBAN structures.

Source
new(*, registry : StructureRegistry)

Creates a validator using a shared StructureRegistry. Use this when you also have a Generator to avoid parsing the JSON twice.

registry = CrystalIBAN::StructureRegistry.new
gen = CrystalIBAN::Generator.new(registry: registry)
val = CrystalIBAN::Validator.new(registry: registry)
Source

Class methods

valid?(iban : String) : Bool

Returns true if iban is valid using the bundled default registry.

CrystalIBAN::Validator.valid?("LI05 0881 0061 8828 4") # => true
Source
validate!(iban : String) : String

Returns the normalized IBAN or raises ArgumentError, using the bundled default registry.

CrystalIBAN::Validator.validate!("LI05 0881 0061 8828 4") # => "LI050881006188284"
Source

Instance methods

valid?(iban : String) : Bool

Returns true if iban is structurally valid for its country and passes the ISO 13616 MOD-97 checksum. Leading/trailing whitespace and internal spaces are stripped; the string is uppercased before checking.

val = CrystalIBAN::Validator.new
val.valid?("LI05 0881 0061 8828 4") # => true
val.valid?("LI00 0000 0000 0000 0") # => false  (bad checksum)
val.valid?("XX123")                 # => false  (unknown country)
Source
validate!(iban : String) : String

Returns the normalized IBAN (uppercased, spaces removed) if it is valid, or raises ArgumentError with a human-readable explanation of the failure.

Checks performed in order:

  1. Minimum length (≥ 4 characters after stripping spaces)
  2. Country code is supported by the loaded structure data
  3. Total length matches the country's expected IBAN length
  4. MOD-97 checksum equals 1 (ISO 13616)
val = CrystalIBAN::Validator.new
val.validate!("LI05 0881 0061 8828 4") # => "LI050881006188284"
val.validate!("LI00 0000 0000 0000 0") # raises ArgumentError
Source