Skip to content

Commit

Permalink
Merge pull request #1158 from metosin/ref-var-path-1106
Browse files Browse the repository at this point in the history
fix: explain :path of ref schemas
  • Loading branch information
ikitommi authored Feb 16, 2025
2 parents 5e18221 + 52367ff commit 28d72f4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Malli is in well matured [alpha](README.md#alpha).
* Docs: elaborate optional-keys and required-keys [#1117](https://github.com/metosin/malli/pull/1117)
* **BREAKING** Output of `parse` now uses new `malli.core.Tag` and `malli.core.Tags` records for `:orn`, `:multi`, `:altn`, `:catn` etc. [#1123](https://github.com/metosin/malli/issues/1123) [#1153](https://github.com/metosin/malli/issues/1153)
* See [Parsing](#parsing-values) and [Unparsing](#unparsing-values) for docs.
* FIX: `:path` when explaining `:ref` errors [#1106](https://github.com/metosin/malli/issues/1106)

## 0.17.0 (2024-12-08)

Expand Down
2 changes: 1 addition & 1 deletion src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1732,7 +1732,7 @@
(let [validator (-memoize (fn [] (-validator (rf))))]
(fn [x] ((validator) x))))
(-explainer [_ path]
(let [explainer (-memoize (fn [] (-explainer (rf) (conj path 0))))]
(let [explainer (-memoize (fn [] (-explainer (rf) (into path [0 0]))))]
(fn [x in acc] ((explainer) x in acc))))
(-parser [_] (->parser -parser))
(-unparser [_] (->parser -unparser))
Expand Down
38 changes: 36 additions & 2 deletions test/malli/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,19 @@

(testing "ref schemas"

(let [schema [:ref {:registry {::referred [:map [:foo :int]]}} ::referred]]
(is (nil? (m/explain schema {:foo 2})))
(testing "explain path"
(let [exp (m/explain schema {:foo "2"})]
(is (results= {:value {:foo "2"}
:schema schema
:errors [{:in [:foo]
:path [0 0 :foo]
:schema :int
:value "2"}]}
exp))
(is (form= :int (mu/get-in schema (-> exp :errors first :path)))))))

(testing "invalid refs fail"
(is (thrown?
#?(:clj Exception, :cljs js/Error)
Expand All @@ -641,7 +654,7 @@
(is (results= {:schema ConsCell
:value [1 [2]]
:errors [{:in [1]
:path [0 0 0 1 0 0]
:path [0 0 0 1 0 0 0]
:schema (mu/get-in ConsCell [0 0 0])
:type :malli.core/tuple-size
:value [2]}]}
Expand Down Expand Up @@ -3359,7 +3372,28 @@

(testing "it works"
(is (= User (m/form schema)))
(is (every? (m/validator schema) (mg/sample schema {:seed 100}))))))
(is (every? (m/validator schema) (mg/sample schema {:seed 100}))))

(testing "explain path"
(let [exp (m/explain schema {:id 1})]
(is (results= {:value {:id 1}
:schema User
:errors [{:in [:id]
:path [:id 0]
:schema :string
:value 1}]}
exp))
(is (form= :string (mu/get-in schema (-> exp :errors first :path)))))
(let [explicit-ref [:ref #'UserId]
exp (m/explain explicit-ref 1)]
(is (results= {:value 1
:schema explicit-ref
:errors [{:in []
:path [0 0]
:schema :string
:value 1}]}
exp))
(is (form= :string (mu/get-in explicit-ref (-> exp :errors first :path))))))))

#?(:clj
(deftest roundrobin-var-references
Expand Down
14 changes: 13 additions & 1 deletion test/malli/error_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,8 @@
(m/explain [1 2 :foo])
(me/humanize)))))

(def VarSchema [:map [:foo :int]])

(deftest error-definion-lookup-test
(is (= {:foo ["should be an integer"]}
(-> [:map
Expand Down Expand Up @@ -629,7 +631,17 @@
(= password password2))]]
(m/explain {:password "secret"
:password2 "faarao"})
(me/humanize {:resolve me/-resolve-root-error}))))))
(me/humanize {:resolve me/-resolve-root-error})))))

(testing "refs #1106"
(is (= {:foo ["should be an integer"]}
(me/humanize
(m/explain [:ref #'VarSchema] {:foo "2"})
{:resolve me/-resolve-direct-error})))
(is (= {:foo ["should be an integer"]}
(me/humanize
(m/explain [:ref #'VarSchema] {:foo "2"})
{:resolve me/-resolve-root-error})))))

(deftest limits
(is (= {:a [["should be an int"]]
Expand Down
11 changes: 10 additions & 1 deletion test/malli/util_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@
;; LensSchemas
;;

(def Var :string)

(deftest basic-lens-schema-test
(let [re #"kikka"
int? (m/schema int?)]
Expand Down Expand Up @@ -333,6 +335,8 @@

[:ref {:registry {::a int?, ::b string?}} ::a] 0 ::a
[:ref {:registry {::a int?, ::b string?}} ::a] 1 nil
[:ref #'Var] 0 #'Var
[:ref #'Var] 1 nil

[:schema int?] 0 int?
[:schema int?] 1 nil)
Expand Down Expand Up @@ -439,7 +443,12 @@
(is (form= (mu/get-in (m/schema [:ref {:registry {::a int?, ::b string?}} ::a]) [0]) ::a))
(is (mu/equals (mu/get-in (m/schema [:ref {:registry {::a int?, ::b string?}} ::a]) [0 0]) int?))
(is (form= (mu/get-in (m/schema [:schema {:registry {::a int?, ::b string?}} ::a]) [0]) ::a))
(is (mu/equals (mu/get-in (m/schema [:schema {:registry {::a int?, ::b string?}} ::a]) [0 0]) int?)))
(is (mu/equals (mu/get-in (m/schema [:schema {:registry {::a int?, ::b string?}} ::a]) [0 0]) int?))

(is (form= (mu/get-in (m/schema [:ref #'Var]) [0]) #'Var))
(is (form= (mu/get-in (m/schema [:ref #'Var]) [0 0]) :string))
(is (form= (mu/get-in (m/schema [:schema #'Var]) [0]) #'Var))
(is (form= (mu/get-in (m/schema [:schema #'Var]) [0 0]) :string)))

(deftest dissoc-test
(let [schema [:map {:title "map"}
Expand Down

0 comments on commit 28d72f4

Please sign in to comment.