Skip to content

Instantly share code, notes, and snippets.

@yonatane
Created April 10, 2020 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yonatane/7bd1d38449f56b3aea62eb391964aee4 to your computer and use it in GitHub Desktop.
Save yonatane/7bd1d38449f56b3aea62eb391964aee4 to your computer and use it in GitHub Desktop.
Require either of multiple related keys with humanized error message
(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