Skip to content

Commit 0c7aacb

Browse files
committed
feat: accept vectors of size 2 in addition to MapEntry in unparse
fixes #1123
1 parent dcfc4ba commit 0c7aacb

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

src/malli/core.cljc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,8 @@
872872
(let [unparsers (into {} (map (fn [[k _ c]] [k (-unparser c)])) (-children this))]
873873
(fn [x]
874874
(if (miu/-tagged? x)
875-
(if-some [unparse (get unparsers (key x))]
876-
(unparse (val x))
875+
(if-some [unparse (get unparsers (first x))]
876+
(unparse (second x))
877877
::invalid)
878878
::invalid))))
879879
(-transformer [this transformer method options]
@@ -1650,7 +1650,7 @@
16501650
(fn [x] (if-some [parser (find (dispatch x))] (parser x) ::invalid))))
16511651
(-unparser [_]
16521652
(let [unparsers (reduce-kv (fn [acc k s] (assoc acc k (-unparser s))) {} @dispatch-map)]
1653-
(fn [x] (if (miu/-tagged? x) (if-some [f (unparsers (key x))] (f (val x)) ::invalid) ::invalid))))
1653+
(fn [x] (if (miu/-tagged? x) (if-some [f (unparsers (first x))] (f (second x)) ::invalid) ::invalid))))
16541654
(-transformer [this transformer method options]
16551655
;; FIXME: Probably should not use `dispatch`
16561656
;; Can't use `dispatch` as `x` might not be valid before it has been unparsed:

src/malli/impl/util.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
(def ^:const +max-size+ #?(:clj Long/MAX_VALUE, :cljs (.-MAX_VALUE js/Number)))
77

88
(defn -tagged [k v] #?(:clj (MapEntry. k v), :cljs (MapEntry. k v nil)))
9-
(defn -tagged? [v] (instance? MapEntry v))
9+
(defn -tagged? [v] (or (instance? MapEntry v) (and (vector? v) (= 2 (count v)))))
1010

1111
(defn -invalid? [x] #?(:clj (identical? x :malli.core/invalid), :cljs (keyword-identical? x :malli.core/invalid)))
1212
(defn -map-valid [f v] (if (-invalid? v) v (f v)))

test/malli/core_test.cljc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@
257257
(is (= (miu/-tagged :pos 1) (m/parse schema* 1)))
258258
(is (= ::m/invalid (m/parse schema* 0)))
259259
(is (= 1 (m/unparse schema* (miu/-tagged :pos 1))))
260+
(is (= 1 (m/unparse schema* [:pos 1])))
260261
(is (= ::m/invalid (m/unparse schema* (miu/-tagged :pos 0))))
262+
(is (= ::m/invalid (m/unparse schema* [:pos 0])))
261263

262264
(doseq [schema [schema schema*]]
263265
(testing (m/form schema)
@@ -1151,8 +1153,11 @@
11511153
(is (= ::m/invalid (m/parse schema invalid5)))
11521154
(is (= ::m/invalid (m/parse schema invalid6)))
11531155
(is (= valid1 (m/unparse schema (m/parse schema valid1))))
1156+
(is (= valid1 (m/unparse schema [:sized valid1])))
11541157
(is (= valid2 (m/unparse schema (m/parse schema valid2))))
1158+
(is (= valid2 (m/unparse schema [:human valid2])))
11551159
(is (= valid3 (m/unparse schema (m/parse schema valid3))))
1160+
(is (= valid3 (m/unparse schema [:sized valid3])))
11561161
(is (= ::m/invalid (m/unparse schema invalid1)))
11571162
(is (= ::m/invalid (m/unparse schema invalid2)))
11581163
(is (= ::m/invalid (m/unparse schema invalid3)))

0 commit comments

Comments
 (0)