Module: Einvoicing::Validators::Base
- Defined in:
- lib/einvoicing/validators/base.rb
Overview
Shared validation helpers for country-specific validators.
Each validator exposes a .validate(invoice) class method that returns
an array of error hashes (empty = valid).
Each error hash has the shape:
{ field: Symbol, error: Symbol, message: String }
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.format(value, field, pattern, message) ⇒ Object
Format check via regex — returns an error hash or nil.
- .included(base) ⇒ Object
-
.luhn_valid?(number_string) ⇒ Boolean
Luhn algorithm for SIREN (9 digits) and SIRET (14 digits).
-
.presence(value, field, message, error: :blank) ⇒ Object
Presence check — returns an error hash or nil.
Class Method Details
.format(value, field, pattern, message) ⇒ Object
Format check via regex — returns an error hash or nil.
44 45 46 |
# File 'lib/einvoicing/validators/base.rb', line 44 def self.format(value, field, pattern, ) { field: field, error: :invalid, message: } unless value.to_s.match?(pattern) end |
.included(base) ⇒ Object
11 12 13 |
# File 'lib/einvoicing/validators/base.rb', line 11 def self.included(base) base.extend(ClassMethods) end |
.luhn_valid?(number_string) ⇒ Boolean
Luhn algorithm for SIREN (9 digits) and SIRET (14 digits). Returns true if the number passes the Luhn check.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/einvoicing/validators/base.rb', line 27 def self.luhn_valid?(number_string) digits = number_string.chars.map(&:to_i) sum = 0 digits.reverse.each_with_index do |d, i| d = i.odd? ? d * 2 : d d -= 9 if d > 9 sum += d end (sum % 10).zero? end |
.presence(value, field, message, error: :blank) ⇒ Object
Presence check — returns an error hash or nil.
39 40 41 |
# File 'lib/einvoicing/validators/base.rb', line 39 def self.presence(value, field, , error: :blank) { field: field, error: error, message: } if value.nil? || value.to_s.strip.empty? end |