Skip to content

Commit 3ca186e

Browse files
committed
fix double negation
1 parent e9454fd commit 3ca186e

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

src/malli/error.cljc

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@
1818
negated message))))
1919

2020
(let [prefix (str "-en-humanize-negation-" (random-uuid))]
21-
(defn- -en-humanize-negation [{:keys [schema] :as error} options]
22-
(let [remove-prefix #(str/replace-first % prefix "")
23-
negated? #(str/starts-with? % prefix)]
24-
(loop [schema schema]
25-
(or (when-some [s (error-message (assoc error :negated #(some->> % (str prefix))) options)]
26-
(if (negated? s)
27-
(remove-prefix s)
28-
(or (when (and (string? s)
29-
(str/starts-with? s "should not "))
30-
(str/replace-first s "should not" "should"))
31-
(when (and (string? s)
32-
(str/starts-with? s "should "))
33-
(str/replace-first s "should" "should not")))))
34-
(let [dschema (m/deref schema)]
35-
(when-not (identical? schema dschema)
36-
(recur dschema))))))))
21+
(defn- -en-humanize-negation [{:keys [schema negated] :as error} options]
22+
(if negated
23+
(negated (error-message (dissoc error :negated) options))
24+
(let [remove-prefix #(str/replace-first % prefix "")
25+
negated? #(str/starts-with? % prefix)]
26+
(loop [schema schema]
27+
(or (when-some [s (error-message (assoc error :negated #(some->> % (str prefix))) options)]
28+
(if (negated? s)
29+
(remove-prefix s)
30+
(or (when (and (string? s)
31+
(str/starts-with? s "should not "))
32+
(str/replace-first s "should not" "should"))
33+
(when (and (string? s)
34+
(str/starts-with? s "should "))
35+
(str/replace-first s "should" "should not")))))
36+
(let [dschema (m/deref schema)]
37+
(when-not (identical? schema dschema)
38+
(recur dschema)))))))))
3739

3840
(defn- -forward-negation [?schema {:keys [negated] :as error} options]
3941
(let [schema (m/schema ?schema options)]

test/malli/error_test.cljc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -886,12 +886,9 @@
886886

887887
(is (= ["should be larger than 1"] (me/humanize (m/explain [:> 1] 0))))
888888
(is (= ["should be at most 1"] (me/humanize (m/explain [:not [:> 1]] 2))))
889-
;;FIXME
890889
(is (= ["should be larger than 1"] (me/humanize (m/explain [:not [:not [:> 1]]] 0))))
891890
(is (= ["should be at most 1"] (me/humanize (m/explain [:not [:not [:not [:> 1]]]] 2))))
892-
;;FIXME
893-
(is (= ["should be larger than 1"] (me/humanize (m/explain [:not [:not [:not [:not [:> 1]]]]] 0))))
894-
)
891+
(is (= ["should be larger than 1"] (me/humanize (m/explain [:not [:not [:not [:not [:> 1]]]]] 0)))))
895892

896893
(deftest custom-negating-test
897894
(is (= ["should be a multiple of 3"]

0 commit comments

Comments
 (0)