-
-
Save mbuczko/929a170e8a79b228e4ce to your computer and use it in GitHub Desktop.
clojure / validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns sagaz-back.validation) | |
; Helper functions | |
(defn validate* [field value regexp] | |
(if-not (re-matches regexp value) | |
(keyword (str "invalid-" (name field))))) | |
(defn validate [data criteria] | |
(-> (reduce-kv (fn [acc k re] (conj acc (validate* k (k data) re))) | |
[] | |
criteria) | |
(#(remove nil? %)))) | |
; Domain specific | |
(defn user [user-data] | |
(validate user-data {:username #"^\w{6,10}$" | |
:email #"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$" | |
:password #"^\w{6,8}$"})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment