Skip to content

Commit

Permalink
join -lookup! and -recursive-lookup!
Browse files Browse the repository at this point in the history
  • Loading branch information
ikitommi committed Jul 28, 2022
1 parent a8a9b26 commit 51f75ff
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Malli is in well matured [alpha](README.md#alpha).

* Source-compatible with nbb, [#726](https://github.com/metosin/malli/pull/726)
* Switch Value and Errors in the pretty output order, [#720](https://github.com/metosin/malli/pull/720)
* Fix registry lookup in schema vector syntax, [#729](https://github.com/metosin/malli/pull/729), fixes [#451](https://github.com/metosin/malli/issues/451)
* Updated dependencies:

```clojure
Expand Down
19 changes: 7 additions & 12 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,11 @@
(or (mr/-schema registry ?schema)
(some-> registry (mr/-schema (c/type ?schema)) (-into-schema nil [?schema] options)))))

(defn- -lookup! [?schema f options]
(defn- -lookup! [?schema f rec options]
(or (and f (f ?schema) ?schema)
(-lookup ?schema options)
(-fail! ::invalid-schema {:schema ?schema})))

(defn- -recursive-lookup! [?schema options]
(if (into-schema? ?schema)
?schema
(some-> (-lookup ?schema options)
(recur options))))
(if-let [?schema (-lookup ?schema options)]
(cond-> ?schema rec (recur f rec options))
(-fail! ::invalid-schema {:schema ?schema}))))

(defn -properties-and-options [properties options f]
(if-let [r (:registry properties)]
Expand Down Expand Up @@ -1935,7 +1930,7 @@
r (when properties (properties :registry))
options (if r (-update options :registry #(mr/composite-registry r (or % (-registry options)))) options)
properties (if r (assoc properties :registry (-property-registry r options identity)) properties)]
(-into-schema (-lookup! type into-schema? options) properties children options))))
(-into-schema (-lookup! type into-schema? false options) properties children options))))

(defn type
"Returns the Schema type."
Expand Down Expand Up @@ -1991,15 +1986,15 @@
(schema? ?schema) ?schema
(into-schema? ?schema) (-into-schema ?schema nil nil options)
(vector? ?schema) (let [v #?(:clj ^IPersistentVector ?schema, :cljs ?schema)
t (-recursive-lookup! #?(:clj (.nth v 0), :cljs (nth v 0)) options)
t (-lookup! #?(:clj (.nth v 0), :cljs (nth v 0)) into-schema? true options)
n #?(:bb (count v) :clj (.count v), :cljs (count v))
?p (when (> n 1) #?(:clj (.nth v 1), :cljs (nth v 1)))]
(if (or (nil? ?p) (map? ?p))
(into-schema t ?p (when (< 2 n) (subvec ?schema 2 n)) options)
(into-schema t nil (when (< 1 n) (subvec ?schema 1 n)) options)))
:else (if-let [?schema' (and (-reference? ?schema) (-lookup ?schema options))]
(-pointer ?schema (schema ?schema' options) options)
(-> ?schema (-lookup! nil options) (recur options))))))
(-> ?schema (-lookup! nil false options) (recur options))))))

(defn form
"Returns the Schema form"
Expand Down
8 changes: 4 additions & 4 deletions test/malli/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@
#?(:clj
(testing "non-terminating functions DO NOT fail fast"
(let [schema [:fn '(fn [x] (< x (apply max (range))))]]
#?(:bb nil ;; Graalvm doesn't support calling .stop on Threads since that is deprecated
#?(:bb nil ;; Graalvm doesn't support calling .stop on Threads since that is deprecated
:clj (is (= ::miu/timeout (miu/-run (fn [] (m/validate schema 1)) 100))))
#_(is (false? (m/validate schema 1)))
#_(is (results= {:schema schema
Expand Down Expand Up @@ -2669,10 +2669,10 @@

(deftest issue-451-test
(testing "registry -in schema vector syntax"
(let [one-level-schema [:map {:registry {:my/string-like :string}}
[:entry [:my/string-like {:some "prop"}]]]]
(let [one-level-schema [:map {:registry {:my/string-like :string}}
[:entry [:my/string-like {:some "prop"}]]]]

(is (true? (m/validate one-level-schema {:entry "a"})))))
(is (true? (m/validate one-level-schema {:entry "a"})))))

(testing "testcase from #451"
(let [opts {:registry {:string (m/-string-schema)
Expand Down

0 comments on commit 51f75ff

Please sign in to comment.