Skip to content

Commit

Permalink
Add support for named transformations with :schema & ::m/schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ikitommi committed Jul 13, 2020
1 parent 9f105a5 commit 4cf197b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,17 @@
(-type [_] type)
(-validator [_] (-validator child))
(-explainer [_ path] (-explainer child path))
(-transformer [_ transformer method options] (-transformer child transformer method options))
(-transformer [this transformer method options]
(let [this-transformer (-value-transformer transformer this method options)
child-transformer (-transformer child transformer method options)
build (fn [phase]
(let [->this (phase this-transformer)
->child (phase child-transformer)]
(if (and ->this ->child)
(comp ->child ->this)
(or ->this ->child))))]
{:enter (build :enter)
:leave (build :leave)}))
(-walk [this walker in options]
(if (-accept walker this in options)
(if id
Expand Down
12 changes: 11 additions & 1 deletion test/malli/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,12 @@
(is (= [:schema {:registry {::cons [:maybe [:tuple 'int? [:ref ::cons]]]}}
::cons]
(m/form [:schema {:registry {::cons [:maybe [:tuple int? [:ref ::cons]]]}}
[::m/schema ::cons]]))))
[::m/schema ::cons]])))

(is (= "$kikka"
(m/decode
[:schema {:decode/custom (partial str "$")} string?] "kikka"
(mt/transformer {:name :custom})))))

(testing "schema schemas"
(let [schema [:and
Expand All @@ -414,6 +419,11 @@
(is (= 1 (m/decode schema "1" mt/string-transformer)))
(is (= "1" (m/decode schema "1" mt/json-transformer)))

(is (= "$kikka"
(m/decode
[:schema {:decode/custom (partial str "$")} string?] "kikka"
(mt/transformer {:name :custom}))))

(testing "deref"
(is (mu/equals (m/schema int?) (m/-deref (m/schema [:schema int?])))))

Expand Down

0 comments on commit 4cf197b

Please sign in to comment.