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

Better error handling when passing IntoSchema to m/schema #1026

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 18 additions & 3 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,17 @@

(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 #?(: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
(into (:data (ex-data e) {})
{: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))
Expand Down Expand Up @@ -2154,15 +2164,20 @@
"Creates a Schema object from any of the following:

- Schema instance (just returns it)
- IntoSchema instance
- 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]
(schema ?schema nil))
([?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 #?(:clj Exception, :cljs js/Error) e
(if (= ::child-error (:type (ex-data e)))
(-fail! ::error-creating-schema-from-into-schema-without-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)
n #?(:bb (count v) :clj (.count v), :cljs (count v))
Expand Down
10 changes: 10 additions & 0 deletions test/malli/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3358,6 +3358,16 @@
::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"]))))

(deftest proxy-schema-explain-path
(let [y-schema [:int {:doc "int"}]
schema (m/schema [(mu/-select-keys)
Expand Down