Module: SecID::Validatable

Included in:
Base
Defined in:
lib/sec_id/concerns/validatable.rb

Overview

Provides validation methods for identifier types.

Including classes should override #valid_format? and optionally #detect_errors for type-specific validation.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ERROR_MAP =

Maps error-code symbols to their exception classes; unmapped codes default to InvalidFormatError.

{
  invalid_checksum: InvalidChecksumError,
  invalid_prefix: InvalidStructureError,
  invalid_category: InvalidStructureError,
  invalid_group: InvalidStructureError,
  invalid_attribute: InvalidStructureError,
  invalid_bban: InvalidStructureError,
  invalid_date: InvalidStructureError,
  invalid_country: InvalidStructureError
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Extends the including identifier class with the concern's class methods.

Parameters:

  • base (Class)

    the identifier class including this concern



26
27
28
# File 'lib/sec_id/concerns/validatable.rb', line 26

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#errorsErrors

Returns an Errors object with error codes and human-readable messages.

Returns:



80
81
82
83
84
# File 'lib/sec_id/concerns/validatable.rb', line 80

def errors
  return @errors if defined?(@errors)

  @errors = Errors.new(error_codes.map { |code| build_error(code, validation_message(code)) })
end

#valid?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/sec_id/concerns/validatable.rb', line 65

def valid?
  valid_format?
end

#validateself

Eagerly triggers validation and caches errors.

Returns:

  • (self)


72
73
74
75
# File 'lib/sec_id/concerns/validatable.rb', line 72

def validate
  errors
  self
end

#validate!self

Validates and returns self if valid, raises an exception otherwise.



90
91
92
93
94
95
# File 'lib/sec_id/concerns/validatable.rb', line 90

def validate!
  return self if valid?

  detail = errors.details.first
  raise self.class.error_class_for(detail[:error]), detail[:message]
end