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

Adds :pred option to m/-map-schema #767

Merged
merged 6 commits into from
Oct 18, 2022
Merged
Changes from 2 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
15 changes: 8 additions & 7 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -942,8 +942,8 @@

(defn -map-schema
([]
(-map-schema {:naked-keys true}))
([opts] ;; :naked-keys, :lazy
(-map-schema {:naked-keys true :pred map?}))
([opts] ;; :naked-keys, :lazy, :pred
^{:type ::into-schema}
(reify
AST
Expand All @@ -954,7 +954,8 @@
(-properties-schema [_ _])
(-children-schema [_ _])
(-into-schema [parent {:keys [closed] :as properties} children options]
(let [entry-parser (-create-entry-parser children opts options)
(let [pred? (:pred opts)
fr33m0nk marked this conversation as resolved.
Show resolved Hide resolved
entry-parser (-create-entry-parser children opts options)
form (delay (-create-entry-form parent properties entry-parser options))
cache (-create-cache options)
->parser (fn [this f]
Expand All @@ -975,7 +976,7 @@
(reduce
(fn [m k] (if (contains? keyset k) m (reduced (reduced ::invalid))))
m (keys m)))))]
(fn [x] (if (map? x) (reduce (fn [m parser] (parser m)) x parsers) ::invalid))))]
(fn [x] (if (pred? x) (reduce (fn [m parser] (parser m)) x parsers) ::invalid))))]
^{:type ::schema}
(reify
AST
Expand All @@ -993,7 +994,7 @@
(-children this))
closed (conj (fn [m] (reduce (fn [acc k] (if (contains? keyset k) acc (reduced false))) true (keys m)))))
validate (miu/-every-pred validators)]
(fn [m] (and (map? m) (validate m)))))
(fn [m] (and (pred? m) (validate m)))))
(-explainer [this path]
(let [keyset (-entry-keyset (-entry-parser this))
explainers (cond-> (-vmap
Expand All @@ -1014,7 +1015,7 @@
(conj acc (miu/-error (conj path k) (conj in k) this v ::extra-key))))
acc x))))]
(fn [x in acc]
(if-not (map? x)
(if-not (pred? x)
(conj acc (miu/-error path in this x ::invalid-type))
(reduce
(fn [acc explainer]
Expand All @@ -1028,7 +1029,7 @@
(let [t (-transformer s transformer method options)]
(cond-> acc t (conj [k t])))) [] (-entries this))
apply->children (when (seq ->children) (-map-transformer ->children))
apply->children (-guard map? apply->children)]
apply->children (-guard pred? apply->children)]
(-intercepting this-transformer apply->children)))
(-walk [this walker path options] (-walk-entries this walker path options))
(-properties [_] properties)
Expand Down