Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hetu validation #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.boot
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[org.clojure/clojurescript "1.9.293" :scope "provided"]

[adzerk/boot-cljs "2.0.0-SNAPSHOT" :scope "test"]
[crisptrutski/boot-cljs-test "0.4.0-SNAPSHOT" :scope "test"]
[crisptrutski/boot-cljs-test "0.3.0" :scope "test"]
[doo "0.1.7" :scope "test"]
[metosin/boot-alt-test "0.3.0" :scope "test"]])

Expand Down
51 changes: 51 additions & 0 deletions src/clj_suomi/validation/hetu.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
(ns clj-suomi.validation.hetu
"Validator for Finnish personal identifaction codes (henkilötunnus/hetu).

> (require '[clj-suomi.validation.hetu :as hetu])
> (hetu/valid? \"010170-0205\")
true"
#?(:cljs (:require goog.date.Date))
#?(:clj (:import java.util.Calendar
java.util.GregorianCalendar)))

(defn- #?(:clj valid-date? :cljs ^boolean valid-date?) [d m y]
#?(:clj
(try
(doto (GregorianCalendar.)
(.setLenient false)
(.set Calendar/YEAR y)
(.set Calendar/MONTH (dec m))
(.set Calendar/DAY_OF_MONTH d)
(.getTime))
true
(catch IllegalArgumentException _
false))
:cljs
(let [date (goog.date.Date. y (dec m) d)]
(and (= d (.getDate date)) (= (dec m) (.getMonth date)) (= y (.getYear date))))))

(defn- parse-int [x] #?(:cljs (js/parseInt x) :clj (Integer/valueOf x)))

(let [->cent-number {"+" 1800, "-" 1900, "A" 2000}
checkchars "0123456789ABCDEFHJKLMNPRSTUVWXY"
checkchars-map (zipmap (range) (map str checkchars))
hetu-pattern (re-pattern (str "^((\\d{2})(\\d{2})(\\d{2}))([\\-\\+A])(\\d{3})([" checkchars "])$"))]

(defn- get-year [year-in-cent cent]
(let [cent-number (->cent-number cent)]
(+ cent-number (parse-int year-in-cent))))

(defn #?(:clj valid? :cljs ^boolean valid?)
"Check that s is a valid hetu string."
[s]
(boolean
(when-let [parts (and s (re-matches hetu-pattern s))]
(let [[_ date day month year-in-cent cent identifier checkchar] parts
year (get-year year-in-cent cent)
check-number (parse-int (str date identifier))
check-reminder (rem check-number 31)]
(and
(valid-date? (parse-int day) (parse-int month) year)
(= (checkchars-map check-reminder) checkchar)))))))


18 changes: 14 additions & 4 deletions test/clj_suomi/validation/hetu_test.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
(ns clj-suomi.validation.hetu-test
(:require [clojure.test :refer [deftest is testing]]))
(:require [clj-suomi.validation.hetu :as hetu]
[clojure.test :refer [deftest is testing]]))

;; placeholder
(deftest hetu-test
(is (true? false)))
(deftest validity-test
(testing "valid hetus"
(is (hetu/valid? "010170-0205"))
(is (hetu/valid? "311210A010K")))
(testing "invalid hetus"
(is (not (hetu/valid? "")))
(is (not (hetu/valid? "test string")))
(is (not (hetu/valid? "310270-0100"))) ; illegal day
(is (not (hetu/valid? "011370-0250"))) ; illegal month
(is (not (hetu/valid? "010170=0205"))) ; illegal century
(is (not (hetu/valid? "010170-0105"))) ; illegal checksum
(is (not (hetu/valid? nil)))))