Created
April 10, 2020 10:15
-
-
Save yonatane/7bd1d38449f56b3aea62eb391964aee4 to your computer and use it in GitHub Desktop.
Require either of multiple related keys with humanized error message
This file contains 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
(require '[malli.core :as m]) | |
(require '[malli.error :as me]) | |
(require '[malli.util :as mu]) | |
(defn require-either [m ks] | |
[:and | |
(mu/optional-keys m ks) | |
[:fn | |
{:error/message (str "Must contain either of " ks)} | |
`(fn [x#] (some x# ~ks))]]) | |
(def Conf | |
[:map | |
[:name string?] | |
[:password {:description "etc"} string?] | |
[:fingerprint {:optional true} int?] | |
[:retinal-scan any?]]) | |
(-> Conf | |
(require-either [:password :fingerprint :retinal-scan]) | |
(m/explain {:name "example"}) | |
(me/humanize)) | |
=> #:malli{:error ["Must contain either of [:password :fingerprint :retinal-scan]"]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment