Class: Israeli::Validators::Id
- Inherits:
-
Object
- Object
- Israeli::Validators::Id
- Defined in:
- lib/israeli/validators/id.rb
Overview
Validates Israeli ID numbers (Mispar Zehut / Teudat Zehut).
Israeli ID numbers are 9-digit numbers where the last digit is a check digit calculated using the Luhn algorithm (mod 10).
Class Method Summary collapse
-
.format(value) ⇒ String?
Formats an Israeli ID to standard 9-digit format.
-
.invalid_reason(value) ⇒ Symbol?
Returns the reason why an ID is invalid.
-
.valid?(value) ⇒ Boolean
Validates an Israeli ID number.
Class Method Details
.format(value) ⇒ String?
Formats an Israeli ID to standard 9-digit format.
71 72 73 74 75 76 77 78 79 |
# File 'lib/israeli/validators/id.rb', line 71 def self.format(value) digits = Sanitizer.digits_only(value) return nil if digits.nil? || digits.empty? padded = digits.rjust(9, "0") return nil unless padded.match?(/\A\d{9}\z/) && Luhn.valid?(padded) padded end |
.invalid_reason(value) ⇒ Symbol?
Returns the reason why an ID is invalid.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/israeli/validators/id.rb', line 52 def self.invalid_reason(value) digits = Sanitizer.digits_only(value) return :blank if digits.nil? || digits.empty? padded = digits.rjust(9, "0") return :wrong_length unless padded.match?(/\A\d{9}\z/) return :invalid_checksum unless Luhn.valid?(padded) nil end |
.valid?(value) ⇒ Boolean
Validates an Israeli ID number.
Accepts formatted input (with hyphens/spaces) and handles leading zeros. IDs shorter than 9 digits are automatically left-padded with zeros.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/israeli/validators/id.rb', line 27 def self.valid?(value) digits = Sanitizer.digits_only(value) return false if digits.nil? || digits.empty? # Left-pad to 9 digits if shorter (handles IDs like "12345678") padded = digits.rjust(9, "0") # Must be exactly 9 digits after padding return false unless padded.match?(/\A\d{9}\z/) Luhn.valid?(padded) end |