Skip to content

Commit

Permalink
Merge pull request #969 from metosin/orn-catn-altn-path-in
Browse files Browse the repository at this point in the history
fix path->in with :orn, :catn and :altn
  • Loading branch information
ikitommi authored Oct 26, 2023
2 parents 142fefd + da98b16 commit 5211aca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ We use [Break Versioning][breakver]. The version numbers follow a `<major>.<mino

Malli is in well matured [alpha](README.md#alpha).

## UNRELEASED

* `mu/path->in` works with `:orn`, `:catn` and `:altn`.

## 0.13.0 (2023-09-24)

* **BREAKING** Fallback to use result of first branch when decoding `:or` and `:orn`, [#946](https://github.com/metosin/malli/pull/946)
Expand Down
22 changes: 11 additions & 11 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@
Cached
(-cache [_] cache)
LensSchema
(-keep [_] true)
(-keep [_])
(-get [this key default] (-get-entries this key default))
(-set [this key value] (-set-entries this key value)))))))

Expand Down Expand Up @@ -1907,7 +1907,7 @@
(-regex-min-max [_ _] (re-min-max properties children)))))))

(defn -sequence-entry-schema
[{:keys [type re-validator re-explainer re-parser re-unparser re-transformer re-min-max] {:keys [min max]} :child-bounds :as opts}]
[{:keys [type re-validator re-explainer re-parser re-unparser re-transformer re-min-max] {:keys [min max keep]} :child-bounds :as opts}]
^{:type ::into-schema}
(reify
AST
Expand Down Expand Up @@ -1941,7 +1941,7 @@
Cached
(-cache [_] cache)
LensSchema
(-keep [_] true)
(-keep [_] keep)
(-get [this key default] (-get-entries this key default))
(-set [this key value] (-set-entries this key value))
EntrySchema
Expand Down Expand Up @@ -2393,56 +2393,56 @@
:uuid (-uuid-schema)})

(defn sequence-schemas []
{:+ (-sequence-schema {:type :+, :child-bounds {:min 1, :max 1}
{:+ (-sequence-schema {:type :+, :child-bounds {:min 1, :max 1}, :keep true
:re-validator (fn [_ [child]] (re/+-validator child))
:re-explainer (fn [_ [child]] (re/+-explainer child))
:re-parser (fn [_ [child]] (re/+-parser child))
:re-unparser (fn [_ [child]] (re/+-unparser child))
:re-transformer (fn [_ [child]] (re/+-transformer child))
:re-min-max (fn [_ [child]] {:min (:min (-regex-min-max child true))})})
:* (-sequence-schema {:type :*, :child-bounds {:min 1, :max 1}
:* (-sequence-schema {:type :*, :child-bounds {:min 1, :max 1}, :keep true
:re-validator (fn [_ [child]] (re/*-validator child))
:re-explainer (fn [_ [child]] (re/*-explainer child))
:re-parser (fn [_ [child]] (re/*-parser child))
:re-unparser (fn [_ [child]] (re/*-unparser child))
:re-transformer (fn [_ [child]] (re/*-transformer child))
:re-min-max (fn [_ _] {:min 0})})
:? (-sequence-schema {:type :?, :child-bounds {:min 1, :max 1}
:? (-sequence-schema {:type :?, :child-bounds {:min 1, :max 1}, :keep true
:re-validator (fn [_ [child]] (re/?-validator child))
:re-explainer (fn [_ [child]] (re/?-explainer child))
:re-parser (fn [_ [child]] (re/?-parser child))
:re-unparser (fn [_ [child]] (re/?-unparser child))
:re-transformer (fn [_ [child]] (re/?-transformer child))
:re-min-max (fn [_ [child]] {:min 0, :max (:max (-regex-min-max child true))})})
:repeat (-sequence-schema {:type :repeat, :child-bounds {:min 1, :max 1}
:repeat (-sequence-schema {:type :repeat, :child-bounds {:min 1, :max 1}, :keep true
:re-validator (fn [{:keys [min max] :or {min 0, max ##Inf}} [child]] (re/repeat-validator min max child))
:re-explainer (fn [{:keys [min max] :or {min 0, max ##Inf}} [child]] (re/repeat-explainer min max child))
:re-parser (fn [{:keys [min max] :or {min 0, max ##Inf}} [child]] (re/repeat-parser min max child))
:re-unparser (fn [{:keys [min max] :or {min 0, max ##Inf}} [child]] (re/repeat-unparser min max child))
:re-transformer (fn [{:keys [min max] :or {min 0, max ##Inf}} [child]] (re/repeat-transformer min max child))
:re-min-max (fn [props [child]] (-re-min-max * props child))})
:cat (-sequence-schema {:type :cat, :child-bounds {}
:cat (-sequence-schema {:type :cat, :child-bounds {}, :keep true
:re-validator (fn [_ children] (apply re/cat-validator children))
:re-explainer (fn [_ children] (apply re/cat-explainer children))
:re-parser (fn [_ children] (apply re/cat-parser children))
:re-unparser (fn [_ children] (apply re/cat-unparser children))
:re-transformer (fn [_ children] (apply re/cat-transformer children))
:re-min-max (fn [_ children] (reduce (partial -re-min-max +) {:min 0, :max 0} children))})
:alt (-sequence-schema {:type :alt, :child-bounds {:min 1}
:alt (-sequence-schema {:type :alt, :child-bounds {:min 1}, :keep true
:re-validator (fn [_ children] (apply re/alt-validator children))
:re-explainer (fn [_ children] (apply re/alt-explainer children))
:re-parser (fn [_ children] (apply re/alt-parser children))
:re-unparser (fn [_ children] (apply re/alt-unparser children))
:re-transformer (fn [_ children] (apply re/alt-transformer children))
:re-min-max (fn [_ children] (reduce -re-alt-min-max {:max 0} children))})
:catn (-sequence-entry-schema {:type :catn, :child-bounds {}
:catn (-sequence-entry-schema {:type :catn, :child-bounds {}, :keep false
:re-validator (fn [_ children] (apply re/cat-validator children))
:re-explainer (fn [_ children] (apply re/cat-explainer children))
:re-parser (fn [_ children] (apply re/catn-parser children))
:re-unparser (fn [_ children] (apply re/catn-unparser children))
:re-transformer (fn [_ children] (apply re/cat-transformer children))
:re-min-max (fn [_ children] (reduce (partial -re-min-max +) {:min 0, :max 0} (-vmap last children)))})
:altn (-sequence-entry-schema {:type :altn, :child-bounds {:min 1}
:altn (-sequence-entry-schema {:type :altn, :child-bounds {:min 1}, :keep false
:re-validator (fn [_ children] (apply re/alt-validator children))
:re-explainer (fn [_ children] (apply re/alt-explainer children))
:re-parser (fn [_ children] (apply re/altn-parser children))
Expand Down
15 changes: 14 additions & 1 deletion test/malli/util_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,20 @@
[:a [:maybe [:sequential [:maybe [:map [:b [:and [:or int?]]]]]]]]]]
[:fn '(constantly true)]]
(m/schema)
(mu/in->paths [:a 0 :b]))))))
(mu/in->paths [:a 0 :b])))))
(testing "orn, catn and altn don't contribute to :in"
(are [type value]
(= [:a]
(let [schema (m/schema [type [:a-branch [:map [:a :int]]]])]
(->> (m/explain schema value)
:errors
first
:path
(mu/path->in schema))))

:orn {:a "2"}
:catn [{:a "2"}]
:altn [{:a "2"}])))

(deftest declarative-schemas
(let [->> #(m/schema % {:registry (merge (mu/schemas) (m/default-schemas))})]
Expand Down

0 comments on commit 5211aca

Please sign in to comment.