Skip to content

Instantly share code, notes, and snippets.

@bortexz
Created August 10, 2022 09:02
Show Gist options
  • Save bortexz/a9e253ab7b7ba3815409bb869f91c846 to your computer and use it in GitHub Desktop.
Save bortexz/a9e253ab7b7ba3815409bb869f91c846 to your computer and use it in GitHub Desktop.
Malli select
(ns malli-select
(:require [malli.core :as m]
[malli.util :as mu]))
(defn select
[schema selection]
(let [ms (filter map? selection)
ks (filter keyword? selection)]
(mu/required-keys
(reduce (fn [s m]
(reduce-kv (fn [s k v]
(mu/assoc s k (select (mu/get s k) v)))
s
m))
schema
ms)
ks)))
(def Nested
(mu/optional-keys
[:map {:registry {::nested-a string?
::nested-b int?}}
::nested-a
::nested-b]))
(def Map
(mu/optional-keys
[:map [::top-level Nested]]))
(def Selection (select Map [::top-level {::top-level [::nested-a]}]))
(m/validate Selection {::top-level {}}) ; => false
(m/validate Selection {::top-level {::nested-a "a"}}) ; => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment