From d634b6d0b73cb7ef4e26d295b84c32d81add0f0c Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Wed, 27 Mar 2024 12:45:22 -0500 Subject: [PATCH 1/4] error handling --- src/malli/core.cljc | 23 ++++++++++++++++++++--- test/malli/core_test.cljc | 10 ++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/malli/core.cljc b/src/malli/core.cljc index e353d7ec8..24b68400d 100644 --- a/src/malli/core.cljc +++ b/src/malli/core.cljc @@ -255,7 +255,16 @@ (defn -property-registry [m options f] (let [options (assoc options ::allow-invalid-refs true)] - (reduce-kv (fn [acc k v] (assoc acc k (f (schema v options)))) {} m))) + (reduce-kv (fn [acc k v] + (let [s (try (schema v options) + (catch Exception e + (if (= ::error-creating-schema-from-into-schema-without-children (:type (ex-data e))) + (-fail! ::schema-definition-must-be-in-options-not-properties + {:bad-property-registry-entry k + :bad-registry m}) + (throw e))))] + (assoc acc k (f s)))) + {} m))) (defn -delayed-registry [m f] (reduce-kv (fn [acc k v] (assoc acc k (reify IntoSchema (-into-schema [_ _ _ options] (f v options))))) {} m)) @@ -2038,7 +2047,7 @@ "Creates a Schema object from any of the following: - Schema instance (just returns it) - - IntoSchema instance + - IntoSchema instance (calls -into-schema with no children or properties) - Schema vector syntax, e.g. [:string {:min 1}] - Qualified Keyword or String, using a registry lookup" ([?schema] @@ -2046,7 +2055,15 @@ ([?schema options] (cond (schema? ?schema) ?schema - (into-schema? ?schema) (-into-schema ?schema nil nil options) + (into-schema? ?schema) (try (-into-schema ?schema nil nil options) + (catch Exception e + (if (= ::child-error (:type (ex-data e))) + (-fail! ::error-creating-schema-from-into-schema-without-children + {:schema ?schema + :message (str "When passing an IntoSchema to m/schema, a schema is constructed" + " without children. " (-type ?schema) + " does not seem to support zero children.")}) + (throw e)))) (vector? ?schema) (let [v #?(:clj ^IPersistentVector ?schema, :cljs ?schema) t (-lookup! #?(:clj (.nth v 0), :cljs (nth v 0)) v into-schema? true options) n #?(:bb (count v) :clj (.count v), :cljs (count v)) diff --git a/test/malli/core_test.cljc b/test/malli/core_test.cljc index 06ee7954c..d06ea93e1 100644 --- a/test/malli/core_test.cljc +++ b/test/malli/core_test.cljc @@ -3224,3 +3224,13 @@ ::xymap [:merge ::xmap ::ymap]}} ::xymap] {:registry registry, ::m/ref-key :id})))))))) + +(deftest property-registry-cannot-take-into-schema-test + (is (thrown-with-msg? + #?(:clj Exception, :cljs js/Error) + #":malli\.core/error-creating-schema-from-into-schema-without-children" + (m/schema (:enum (m/default-schemas))))) + (is (thrown-with-msg? + #?(:clj Exception, :cljs js/Error) + #":malli\.core/schema-definition-must-be-in-options-not-properties" + (m/schema [:= {:registry {:enum (:enum (m/default-schemas))}} "foo"])))) From 4893ae9e37e94d1f4d9667b3fffa2aede80c15a1 Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Wed, 27 Mar 2024 12:48:06 -0500 Subject: [PATCH 2/4] cljs --- src/malli/core.cljc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/malli/core.cljc b/src/malli/core.cljc index 24b68400d..9a60728a7 100644 --- a/src/malli/core.cljc +++ b/src/malli/core.cljc @@ -257,7 +257,7 @@ (let [options (assoc options ::allow-invalid-refs true)] (reduce-kv (fn [acc k v] (let [s (try (schema v options) - (catch Exception e + (catch #?(:clj Exception, :cljs js/Error) e (if (= ::error-creating-schema-from-into-schema-without-children (:type (ex-data e))) (-fail! ::schema-definition-must-be-in-options-not-properties {:bad-property-registry-entry k @@ -2056,7 +2056,7 @@ (cond (schema? ?schema) ?schema (into-schema? ?schema) (try (-into-schema ?schema nil nil options) - (catch Exception e + (catch #?(:clj Exception, :cljs js/Error) e (if (= ::child-error (:type (ex-data e))) (-fail! ::error-creating-schema-from-into-schema-without-children {:schema ?schema From ed4b16dfeb2eda0a340005bf0cc7c867dc5e4a20 Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Wed, 27 Mar 2024 12:57:15 -0500 Subject: [PATCH 3/4] doc --- src/malli/core.cljc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/malli/core.cljc b/src/malli/core.cljc index 9a60728a7..d4408bbcd 100644 --- a/src/malli/core.cljc +++ b/src/malli/core.cljc @@ -2047,7 +2047,7 @@ "Creates a Schema object from any of the following: - Schema instance (just returns it) - - IntoSchema instance (calls -into-schema with no children or properties) + - IntoSchema instance (attempts to construct a Schema with no children) - Schema vector syntax, e.g. [:string {:min 1}] - Qualified Keyword or String, using a registry lookup" ([?schema] From 05f04794c780a3f9dcf50cc92547c12a494d06da Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Tue, 9 Apr 2024 11:43:20 -0400 Subject: [PATCH 4/4] remove :message --- src/malli/core.cljc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/malli/core.cljc b/src/malli/core.cljc index d4408bbcd..48f2efde7 100644 --- a/src/malli/core.cljc +++ b/src/malli/core.cljc @@ -260,8 +260,9 @@ (catch #?(:clj Exception, :cljs js/Error) e (if (= ::error-creating-schema-from-into-schema-without-children (:type (ex-data e))) (-fail! ::schema-definition-must-be-in-options-not-properties - {:bad-property-registry-entry k - :bad-registry m}) + (into (:data (ex-data e) {}) + {:bad-property-registry-entry k + :bad-registry m})) (throw e))))] (assoc acc k (f s)))) {} m))) @@ -2059,10 +2060,7 @@ (catch #?(:clj Exception, :cljs js/Error) e (if (= ::child-error (:type (ex-data e))) (-fail! ::error-creating-schema-from-into-schema-without-children - {:schema ?schema - :message (str "When passing an IntoSchema to m/schema, a schema is constructed" - " without children. " (-type ?schema) - " does not seem to support zero children.")}) + {:schema-not-supporting-zero-children ?schema}) (throw e)))) (vector? ?schema) (let [v #?(:clj ^IPersistentVector ?schema, :cljs ?schema) t (-lookup! #?(:clj (.nth v 0), :cljs (nth v 0)) v into-schema? true options)