Skip to content

Commit

Permalink
Merge pull request #1031 from frenchy64/eq-humanize
Browse files Browse the repository at this point in the history
Distinguish between symbols and strings in humanize
  • Loading branch information
ikitommi authored Apr 20, 2024
2 parents b80c4bc + 7da05f5 commit 32cc8c6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
20 changes: 13 additions & 7 deletions src/malli/error.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
[malli.core :as m]
[malli.util :as mu]))

(defn- -pr-str [v]
#?(:clj (pr-str v)
:cljs (str v)))

(defn -pred-min-max-error-fn [{:keys [pred message]}]
(fn [{:keys [schema value]} _]
(let [{:keys [min max]} (m/properties schema)]
Expand All @@ -28,9 +32,11 @@
::m/extra-key {:error/message {:en "disallowed key"}}
:malli.core/invalid-dispatch-value {:error/message {:en "invalid dispatch value"}}
::misspelled-key {:error/fn {:en (fn [{::keys [likely-misspelling-of]} _]
(str "should be spelled " (str/join " or " (map last likely-misspelling-of))))}}
(str "should be spelled "
(str/join " or " (map (comp -pr-str last) likely-misspelling-of))))}}
::misspelled-value {:error/fn {:en (fn [{::keys [likely-misspelling-of]} _]
(str "did you mean " (str/join " or " (map last likely-misspelling-of))))}}
(str "did you mean "
(str/join " or " (map (comp -pr-str last) likely-misspelling-of))))}}
::m/input-remaining {:error/message {:en "input remaining"}}
::m/end-of-input {:error/message {:en "end of input"}}
'any? {:error/message {:en "should be any"}}
Expand Down Expand Up @@ -86,9 +92,9 @@
:enum {:error/fn {:en (fn [{:keys [schema]} _]
(str "should be "
(if (= 1 (count (m/children schema)))
(first (m/children schema))
(str "either " (->> (m/children schema) butlast (str/join ", "))
" or " (last (m/children schema))))))}}
(-pr-str (first (m/children schema)))
(str "either " (->> (m/children schema) butlast (map -pr-str) (str/join ", "))
" or " (-pr-str (last (m/children schema)))))))}}
:any {:error/message {:en "should be any"}}
:nil {:error/message {:en "should be nil"}}
:string {:error/fn {:en (fn [{:keys [schema value]} _]
Expand Down Expand Up @@ -124,9 +130,9 @@
(str "should be at most " (first (m/children schema)))
"should be a number"))}}
:= {:error/fn {:en (fn [{:keys [schema]} _]
(str "should be " (first (m/children schema))))}}
(str "should be " (-pr-str (first (m/children schema)))))}}
:not= {:error/fn {:en (fn [{:keys [schema]} _]
(str "should not be " (first (m/children schema))))}}})
(str "should not be " (-pr-str (first (m/children schema)))))}}})

(defn- -maybe-localized [x locale]
(if (map? x) (get x locale) x))
Expand Down
49 changes: 40 additions & 9 deletions test/malli/error_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@
(m/explain {:orders true, :deliverz true})
(me/with-spell-checking)
(me/with-error-messages)
(get-errors))))
(is (= [{:path ["deliverz"]
:type ::me/misspelled-key
::me/likely-misspelling-of [["deliver"]]
:message #?(:clj "should be spelled \"deliver\""
:cljs "should be spelled deliver")}]
(-> [:map
["orders" boolean?]
["deliver" boolean?]]
(mu/closed-schema)
(m/explain {"orders" true, "deliverz" true})
(me/with-spell-checking)
(me/with-error-messages)
(get-errors)))))

(testing "nested"
Expand Down Expand Up @@ -420,21 +433,25 @@
(m/explain "foo")
(me/humanize)))))
(testing "error with 1 value"
(is (= ["should be foo"]
(is (= [#?(:clj "should be \"foo\""
:cljs "should be foo")]
(-> [:enum "foo"]
(m/explain "baz")
(me/humanize)))))
(testing "error with 2 values"
(is (= ["should be either foo or bar"]
(is (= [#?(:clj "should be either \"foo\" or \"bar\""
:cljs "should be either foo or bar")]
(-> [:enum "foo" "bar"]
(m/explain "baz")
(me/humanize)))))
(testing "more than 2 values"
(is (= ["should be either foo, bar or buzz"]
(-> [:enum "foo" "bar" "buzz"]
(is (= [#?(:clj "should be either \"foo\", \"bar\", bar or \"buzz\""
:cljs "should be either foo, bar, bar or buzz")]
(-> [:enum "foo" "bar" 'bar "buzz"]
(m/explain "baz")
(me/humanize))))
(is (= ["should be either foo, bar, buzz or biff"]
(is (= [#?(:clj "should be either \"foo\", \"bar\", \"buzz\" or \"biff\""
:cljs "should be either foo, bar, buzz or biff")]
(-> [:enum "foo" "bar" "buzz" "biff"]
(m/explain "baz")
(me/humanize))))))
Expand All @@ -458,14 +475,16 @@
(deftest multi-error-test
(let [schema [:multi {:dispatch :type}
["plus" [:map [:value int?]]]
["minus" [:map [:value int?]]]]]
["minus" [:map [:value int?]]]
['minus [:map [:value int?]]]]]

(is (= {:type ["invalid dispatch value"]}
(-> schema
(m/explain {:type "minuz"})
(me/humanize))))

(is (= {:type ["did you mean minus"]}
(is (= {:type [#?(:clj "did you mean \"minus\" or minus"
:cljs "did you mean minus or minus")]}
(-> schema
(m/explain {:type "minuz"})
(me/with-spell-checking)
Expand Down Expand Up @@ -552,9 +571,10 @@
(me/humanize {:resolve me/-resolve-root-error})))))

(testing "enum #553"
(is (= {:a ["should be either a or b"]}
(is (= {:a [#?(:clj "should be either \"a\", \"b\", a or b"
:cljs "should be either a, b, a or b")]}
(-> [:map
[:a [:enum "a" "b"]]]
[:a [:enum "a" "b" 'a 'b]]]
(m/explain {:a nil})
(me/humanize {:resolve me/-resolve-root-error})))))

Expand Down Expand Up @@ -757,3 +777,14 @@
(-> (m/explain Address address)
(me/error-value {::me/wrap-error #(select-keys % [:value :type])
::me/keep-valid-values true}))))))))

#?(:clj
(deftest pr-str-humanize-test
(is (= ["should be \"a\""] (me/humanize (m/explain [:enum "a"] 1))))
(is (= ["should be a"] (me/humanize (m/explain [:enum 'a] 1))))
(is (= ["should be either \"a\" or \"b\""] (me/humanize (m/explain [:enum "a" "b"] 1))))
(is (= ["should be either a or b"] (me/humanize (m/explain [:enum 'a 'b] 1))))
(is (= ["should be \"a\""] (me/humanize (m/explain [:= "a"] 1))))
(is (= ["should be a"] (me/humanize (m/explain [:= 'a] 1))))
(is (= ["should not be \"a\""] (me/humanize (m/explain [:not= "a"] "a"))))
(is (= ["should not be a"] (me/humanize (m/explain [:not= 'a] 'a))))))

0 comments on commit 32cc8c6

Please sign in to comment.