Class: CurpMx::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/curp_mx.rb

Overview

Used to validate a CURPs format and a few data points in it

Constant Summary collapse

REGEX =

Basic CURP regex structure

/\A(?<father_initial>[A-Z]{2})
(?<mother_initial>[A-Z]{1})
(?<name_initial>[A-Z]{1})
(?<birth_year>[0-9]{2})
(?<birth_month>[0-1][0-9])
(?<birth_day>[0-3][0-9])
(?<sex>[HM])
(?<state>[A-Z]{2})
(?<father_consonant>[^AEIOU])
(?<mother_consonant>[^AEIOU])
(?<name_consonant>[^AEIOU])
(?<key>[0-9]{2})\z/x.freeze
STATES_RENAPO =

States’ initials as listed in Registro Nacional de Población (RENAPO)

%w[AS BC BS CC CS CH CL CM DF CX DG GT GR HG JC MC MN MS
NT NL OC PL QT QR SP SL SR TC TS TL VZ YN ZS].freeze
NAME_ISSUES =

Problematic name initials

%w[BACA LOCO BUEI BUEY MAME CACA MAMO
CAGA MEAS CAGO MEON CAKA MIAR CAKO MION COGE
MOCO COGI MOKO COJA MULA COJE MULO COJI NACA
COJO NACO COLA PEDA CULO PEDO FALO PENE FETO
PIPI GETA PITO GUEI POPO GUEY PUTA JETA PUTO
JOTO QULO KACA RATA KACO ROBA KAGA ROBE KAGO
ROBO KAKA RUIN KAKO SENO KOGE TETA KOGI VACA
KOJA VAGA KOJE VAGO KOJI VAKA KOJO VUEI KOLA
VUEY KULO WUEI LILO WUEY LOCA CACO MEAR].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(curp) ⇒ Validator

Returns a new instance of Validator.



46
47
48
49
50
51
# File 'lib/curp_mx.rb', line 46

def initialize(curp)
  @raw_input = curp
  @errors = {}

  validate
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



10
11
12
# File 'lib/curp_mx.rb', line 10

def errors
  @errors
end

#raw_inputObject (readonly)

Returns the value of attribute raw_input.



10
11
12
# File 'lib/curp_mx.rb', line 10

def raw_input
  @raw_input
end

Class Method Details

.valid?(curp) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/curp_mx.rb', line 42

def self.valid?(curp)
  new(curp).valid?
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/curp_mx.rb', line 53

def valid?
  @errors.empty?
end

#validateObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/curp_mx.rb', line 57

def validate
  @md = REGEX.match(@raw_input)

  if @md.nil?
    @errors[:format] ||= []
    @errors[:format] << 'Invalid format'
    return false
  end

  validate_state
  validate_name_initials
  validate_birth_date
  validate_date_exists
end