Skip to content

Instantly share code, notes, and snippets.

@guilespi
Created August 5, 2014 16:00
Show Gist options
  • Save guilespi/bb53a2b615cdab4813ac to your computer and use it in GitHub Desktop.
Save guilespi/bb53a2b615cdab4813ac to your computer and use it in GitHub Desktop.
(defmethod validation/valid-field? :document-MX
[{value :value}]
(let [match (re-find #"(?i)(^[a-z]{2})([a-z])([a-z])(\d{6})(H|M)([a-z]{2})([a-z]{3})(\d{2})$" value)]
match))
(defmethod validation/valid-field? :document-UY
[{value :value}]
(when-let [[_ number digit] (re-find #"^(\d{7})-(\d)$" value)]
(let [m [2 9 8 7 6 3 4]
c (map #(utils/str->int (str %)) number)
v (apply + (map * m c))
validator (- 10 (mod v 10))]
(= (utils/str->int digit) validator))))
(defmethod validation/valid-field? :document-AR
[{value :value}]
(when-let [[_ str-number letter] (re-find #"^(\d{8})([a-zA-Z])$" value)]
(let [number (utils/str->int str-number)
index (mod number 23)
letters "TRWAGMYFPDXBNJZSQVHLCKET"]
(= (get letters index) (get (clojure.string/upper-case letter) 0)))))
;;BR http://codigofonte.uol.com.br/codigo/js-dhtml/validacao/validar-documento-de-identidade-[rg]
(def rules-by-country
{:UY {:document {:validations {:document-UY true}
:messages {:document-UY "La cédula ingresada no es correcta, ingrésela con la sintaxis 1234567-2"}}}
:MX {:document {:validations {:document-MX true}
:messages {:document-MX "El documento ingresado no es correcto, verifíquelo"}}}
:AR {:document {:validations {:document-AR true}
:messages {:document-AR "El DNI ingresado no es correcto, ingréselo con la sintaxis 12345678N"}}}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment