From 51f75ffbb8c50a72282516f54c6467813cf1b0fb Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Thu, 28 Jul 2022 12:42:09 +0300 Subject: [PATCH] join -lookup! and -recursive-lookup! --- CHANGELOG.md | 1 + src/malli/core.cljc | 19 +++++++------------ test/malli/core_test.cljc | 8 ++++---- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4b342308..13acfc2d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/malli/core.cljc b/src/malli/core.cljc index 95adbba45..453ad4a47 100644 --- a/src/malli/core.cljc +++ b/src/malli/core.cljc @@ -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)] @@ -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." @@ -1991,7 +1986,7 @@ (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)) @@ -1999,7 +1994,7 @@ (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" diff --git a/test/malli/core_test.cljc b/test/malli/core_test.cljc index 0411c8588..48dba098e 100644 --- a/test/malli/core_test.cljc +++ b/test/malli/core_test.cljc @@ -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 @@ -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)