diff --git a/CHANGELOG.md b/CHANGELOG.md index e98e7f895..1105214b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Malli is in well matured [alpha](README.md#alpha). ## 0.17.0 (UNRELEASED) +* `unparse` accepts vectors in addition to MapEntry values for `:orn` and `:multi` [#1123](https://github.com/metosin/malli/issues/1123) * Don't output `:definitions nil` in swagger. [#1134](https://github.com/metosin/malli/issues/1134) * **BREAKING**: `:gen/fmap` property requires its schema to create a generator. * previous behavior defaulted to a `nil`-returning generator, even if the schema doesn't accept `nil` diff --git a/src/malli/core.cljc b/src/malli/core.cljc index 5ccb8a397..bfe45f407 100644 --- a/src/malli/core.cljc +++ b/src/malli/core.cljc @@ -872,8 +872,8 @@ (let [unparsers (into {} (map (fn [[k _ c]] [k (-unparser c)])) (-children this))] (fn [x] (if (miu/-tagged? x) - (if-some [unparse (get unparsers (key x))] - (unparse (val x)) + (if-some [unparse (get unparsers (nth x 0))] + (unparse (nth x 1)) ::invalid) ::invalid)))) (-transformer [this transformer method options] @@ -1650,7 +1650,7 @@ (fn [x] (if-some [parser (find (dispatch x))] (parser x) ::invalid)))) (-unparser [_] (let [unparsers (reduce-kv (fn [acc k s] (assoc acc k (-unparser s))) {} @dispatch-map)] - (fn [x] (if (miu/-tagged? x) (if-some [f (unparsers (key x))] (f (val x)) ::invalid) ::invalid)))) + (fn [x] (if (miu/-tagged? x) (if-some [f (unparsers (nth x 0))] (f (nth x 1)) ::invalid) ::invalid)))) (-transformer [this transformer method options] ;; FIXME: Probably should not use `dispatch` ;; Can't use `dispatch` as `x` might not be valid before it has been unparsed: diff --git a/src/malli/impl/util.cljc b/src/malli/impl/util.cljc index 84f3b13ab..233391e15 100644 --- a/src/malli/impl/util.cljc +++ b/src/malli/impl/util.cljc @@ -6,7 +6,7 @@ (def ^:const +max-size+ #?(:clj Long/MAX_VALUE, :cljs (.-MAX_VALUE js/Number))) (defn -tagged [k v] #?(:clj (MapEntry. k v), :cljs (MapEntry. k v nil))) -(defn -tagged? [v] (instance? MapEntry v)) +(defn -tagged? [v] (or (instance? MapEntry v) (and (vector? v) (= 2 (count v))))) (defn -invalid? [x] #?(:clj (identical? x :malli.core/invalid), :cljs (keyword-identical? x :malli.core/invalid))) (defn -map-valid [f v] (if (-invalid? v) v (f v))) diff --git a/test/malli/core_test.cljc b/test/malli/core_test.cljc index 8c4641381..68b261ca7 100644 --- a/test/malli/core_test.cljc +++ b/test/malli/core_test.cljc @@ -257,7 +257,9 @@ (is (= (miu/-tagged :pos 1) (m/parse schema* 1))) (is (= ::m/invalid (m/parse schema* 0))) (is (= 1 (m/unparse schema* (miu/-tagged :pos 1)))) + (is (= 1 (m/unparse schema* [:pos 1]))) (is (= ::m/invalid (m/unparse schema* (miu/-tagged :pos 0)))) + (is (= ::m/invalid (m/unparse schema* [:pos 0]))) (doseq [schema [schema schema*]] (testing (m/form schema) @@ -1151,8 +1153,11 @@ (is (= ::m/invalid (m/parse schema invalid5))) (is (= ::m/invalid (m/parse schema invalid6))) (is (= valid1 (m/unparse schema (m/parse schema valid1)))) + (is (= valid1 (m/unparse schema [:sized valid1]))) (is (= valid2 (m/unparse schema (m/parse schema valid2)))) + (is (= valid2 (m/unparse schema [:human valid2]))) (is (= valid3 (m/unparse schema (m/parse schema valid3)))) + (is (= valid3 (m/unparse schema [:sized valid3]))) (is (= ::m/invalid (m/unparse schema invalid1))) (is (= ::m/invalid (m/unparse schema invalid2))) (is (= ::m/invalid (m/unparse schema invalid3)))