From a98b862ebefdf279dd03697ba04abdc2d9d604da Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Thu, 10 Apr 2025 18:26:23 -0500 Subject: [PATCH 1/3] fix lazy parsing of registries via from-ast --- src/malli/core.cljc | 10 ++++++---- test/malli/core_test.cljc | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/malli/core.cljc b/src/malli/core.cljc index fead6e24b..660bc8490 100644 --- a/src/malli/core.cljc +++ b/src/malli/core.cljc @@ -2568,10 +2568,12 @@ (map? ?ast) (if-let [s (-lookup (:type ?ast) options)] (let [r (when-let [r (:registry ?ast)] (-delayed-registry r from-ast)) options (cond-> options r (-update :registry #(mr/composite-registry r (or % (-registry options))))) - ast (cond-> ?ast r (-update :properties #(assoc % :registry (-property-registry r options identity))))] - (cond (and (into-schema? s) (-ast? s)) (-from-ast s ast options) - (into-schema? s) (-into-schema s (:properties ast) (-vmap #(from-ast % options) (:children ast)) options) - :else s)) + ast (cond-> ?ast r (-update :properties #(assoc % :registry r)))] + (if (into-schema? s) + (if (-ast? s) + (-from-ast s ast options) + (-into-schema s (:properties ast) (-vmap #(from-ast % options) (:children ast)) options)) + s)) (-fail! ::invalid-ast {:ast ?ast})) :else (-fail! ::invalid-ast {:ast ?ast})))) diff --git a/test/malli/core_test.cljc b/test/malli/core_test.cljc index 617943971..64936a3a5 100644 --- a/test/malli/core_test.cljc +++ b/test/malli/core_test.cljc @@ -3581,3 +3581,17 @@ (is (not (m/validate [:sequential {:min 11} :int] (eduction identity (range 10))))) (is (not (m/validate [:seqable {:min 11} :int] (eduction identity (range 10))))) (is (nil? (m/explain [:sequential {:min 9} :int] (eduction identity (range 10)))))) + +(deftest from-ast-delayed-registry-test + (let [lazy? (volatile! true) + original-delayed-registry m/-delayed-registry + _side-effect (with-redefs [m/-delayed-registry + (fn [m f] + (-> (original-delayed-registry m f) + (update-vals (fn [v] + (reify + m/IntoSchema (m/-into-schema [_ properties children options] + (vreset! lazy? false) + (m/-into-schema v properties children options)))))))] + (m/from-ast (m/ast [:int {:registry {::foo :int}}])))] + (is @lazy?))) From 0ce9ce6e81b040ee34178539a6dd64b858bdc236 Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Thu, 10 Apr 2025 18:32:12 -0500 Subject: [PATCH 2/3] refactor --- test/malli/core_test.cljc | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/test/malli/core_test.cljc b/test/malli/core_test.cljc index 64936a3a5..0b64dab9a 100644 --- a/test/malli/core_test.cljc +++ b/test/malli/core_test.cljc @@ -3583,15 +3583,19 @@ (is (nil? (m/explain [:sequential {:min 9} :int] (eduction identity (range 10)))))) (deftest from-ast-delayed-registry-test - (let [lazy? (volatile! true) + (let [form [:int {:registry {::foo :int}}] + forced? (volatile! false) original-delayed-registry m/-delayed-registry - _side-effect (with-redefs [m/-delayed-registry - (fn [m f] - (-> (original-delayed-registry m f) - (update-vals (fn [v] - (reify - m/IntoSchema (m/-into-schema [_ properties children options] - (vreset! lazy? false) - (m/-into-schema v properties children options)))))))] - (m/from-ast (m/ast [:int {:registry {::foo :int}}])))] - (is @lazy?))) + s (with-redefs [m/-delayed-registry + (fn [m f] + (-> (original-delayed-registry m f) + (update-vals (fn [v] + (reify + m/IntoSchema (m/-into-schema [_ properties children options] + (vreset! forced? true) + (m/-into-schema v properties children options)))))))] + (m/from-ast (m/ast form))) + _ (is (not @forced?)) + form' (m/form s) + _ (is @forced?)] + (is (= form form')))) From d9edcb61962a890526f35821f1f72adc2249a731 Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Thu, 10 Apr 2025 18:59:18 -0500 Subject: [PATCH 3/3] notes --- test/malli/core_test.cljc | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test/malli/core_test.cljc b/test/malli/core_test.cljc index 0b64dab9a..cba1042ee 100644 --- a/test/malli/core_test.cljc +++ b/test/malli/core_test.cljc @@ -3586,16 +3586,22 @@ (let [form [:int {:registry {::foo :int}}] forced? (volatile! false) original-delayed-registry m/-delayed-registry + ;;TODO refactor by providing custom IntoSchema constructor s (with-redefs [m/-delayed-registry (fn [m f] (-> (original-delayed-registry m f) (update-vals (fn [v] - (reify - m/IntoSchema (m/-into-schema [_ properties children options] - (vreset! forced? true) - (m/-into-schema v properties children options)))))))] + (reify m/IntoSchema + (m/-into-schema [_ properties children options] + (vreset! forced? true) + (m/-into-schema v properties children options)))))))] (m/from-ast (m/ast form))) _ (is (not @forced?)) form' (m/form s) _ (is @forced?)] (is (= form form')))) + +(comment + (m/from-ast (m/ast [:int {:registry {::foo :int}}])) + (m/form (m/from-ast (m/ast [:int {:registry {::foo :int}}]))) +)