Skip to content

Commit

Permalink
map entrys have custom forms
Browse files Browse the repository at this point in the history
  • Loading branch information
ikitommi committed Aug 10, 2020
1 parent f953cb7 commit 12fd067
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
5 changes: 3 additions & 2 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@
(reify IntoSchema
(-into-schema [_ properties children options]
(-check-children! ::entry properties children {:min 1, :max 1})
(let [[schema :as children] (map #(schema % options) children)]
(let [[schema :as children] (map #(schema % options) children)
form (-create-form ::entry properties (map -form children))]
^{:type ::schema}
(reify Schema
(-type [_] ::entry)
Expand All @@ -305,7 +306,7 @@
(-properties [_] properties)
(-options [_] (-options schema))
(-children [_] children)
(-form [_] (-form schema))
(-form [_] form)
LensSchema
(-keep [_])
(-get [_ key default] (if (= 0 key) schema default))
Expand Down
2 changes: 1 addition & 1 deletion src/malli/dot.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
sorted #(sort-by (comp str first) %)
wrap #(str "\"" % "\"")
label (fn [k v] (str "\"{" k "|"
(or (some->> (m/entries v) (map (fn [[k s]] (str k " " (esc (m/form s))))) (str/join "\\l"))
(or (some->> (m/entries v) (map (fn [[k s]] (str k " " (esc (m/form (m/-deref s)))))) (str/join "\\l"))
(esc (m/form v)))
"\\l}\""))
> #(apply println %&)
Expand Down
40 changes: 22 additions & 18 deletions test/malli/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[malli.edn :as me]
[malli.transform :as mt]
[malli.util :as mu]
[malli.registry :as mr]))
[malli.registry :as mr]
[clojure.walk :as walk]))

(defn with-schema-forms [result]
(some-> result
Expand All @@ -19,8 +20,8 @@
(defn results= [& results]
(apply = (map with-schema-forms results)))

(defn entries= [& entries]
(apply = (map (partial map #(update % (dec (count %)) m/form)) entries)))
(defn schema= [& entries]
(apply = (map (partial walk/postwalk (fn [x] (if (m/schema? x) (m/form x) x))) entries)))

(defn form= [& entries]
(apply = (map m/form entries)))
Expand All @@ -47,11 +48,11 @@
[:y {:optional true, :title "boolean"} 'boolean?]]
forms)))
(testing "entries"
(is (= [[:x 'int?]
[::x ::x]
[::y ::y]
[:y 'boolean?]]
(map #(update % 1 m/form) entries))))
(is (schema= [[:x [::m/entry 'int?]]
[::x [::m/entry ::x]]
[::y [::m/entry {:optional true} ::y]]
[:y [::m/entry {:optional true :title "boolean"} 'boolean?]]]
entries)))
(testing "children"
(is (= [[:x nil 'int?]
[::x nil ::x]
Expand All @@ -78,8 +79,12 @@
(is (= {:district 9} (m/eval "(m/properties [int? {:district 9}])")))
(is (= :maybe (m/eval "(m/type [:maybe int?])")))
(is (= ['int? 'string?] (map m/form (m/eval "(m/children [:or {:some \"props\"} int? string?])"))))
(is (entries= [[:x 'int?] [:y 'string?]] (m/eval "(m/entries [:map [:x int?] [:y string?]])")))
(is (entries= [[:x nil 'int?] [:y nil 'string?]] (m/eval "(m/children [:map [:x int?] [:y string?]])")))) ;
(is (schema= [[:x [::m/entry 'int?]] [:y [::m/entry 'string?]]] (m/eval "(m/entries [:map [:x int?] [:y string?]])")))
(is (schema= [[:x nil 'int?] [:y nil 'string?]] (m/eval "(m/children [:map [:x int?] [:y string?]])")))) ;

(m/eval "(m/entries [:map [:x int?] [:y string?]])")

(m/eval "(m/entries [:map [:x int?] [:y string?]])")

(deftest into-schema-test
(is (form= [:map {:closed true} [:x int?]]
Expand Down Expand Up @@ -565,11 +570,10 @@
(is (nil? (m/explain schema valid)))
(is (nil? (m/explain schema valid2)))

(is (entries=
[[:x nil boolean?]
[:y {:optional true} int?]
[:z {:optional false} string?]]
(m/children schema)))
(is (schema= [[:x nil 'boolean?]
[:y {:optional true} 'int?]
[:z {:optional false} 'string?]]
(m/children schema)))

(is (results= {:schema schema
:value {:y "invalid" :z "kikka"}
Expand Down Expand Up @@ -727,9 +731,9 @@
:children [[:country nil {:type 'keyword?}]]}]]}]]}
(mu/to-map-syntax schema)))

(is (entries= [[:sized nil [:map [:type keyword?] [:size int?]]]
[:human nil [:map [:type keyword?] [:name string?] [:address [:map [:country keyword?]]]]]]
(m/children schema)))
(is (schema= [[:sized nil [:map [:type 'keyword?] [:size 'int?]]]
[:human nil [:map [:type 'keyword?] [:name 'string?] [:address [:map [:country 'keyword?]]]]]]
(m/children schema)))

(is (= [:multi
{:dispatch :type, :decode/string '(fn [x] (update x :type keyword))}
Expand Down
16 changes: 8 additions & 8 deletions test/malli/transform_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -454,25 +454,25 @@
(reset! state nil)
(m/decode schema {:x 1, :y "2"} transformer)
(is (= [[:decode :enter :map {:x 1, :y "2"}]
[:decode :enter :malli.core/entry 1]
[:decode :enter :malli.core/entry "2"]
[:decode :enter ::m/entry 1]
[:decode :enter ::m/entry "2"]
[:decode :enter 'string? "2"]
[:decode :leave :malli.core/entry 1]
[:decode :leave ::m/entry 1]
[:decode :leave 'string? "2"]
[:decode :leave :malli.core/entry "2"]
[:decode :leave ::m/entry "2"]
[:decode :leave :map {:x 1, :y "2"}]]
@state)))

(testing "encode"
(reset! state nil)
(m/encode schema {:x 1, :y "2"} transformer)
(is (= [[:encode :enter :map {:x 1, :y "2"}]
[:encode :enter :malli.core/entry 1]
[:encode :enter :malli.core/entry "2"]
[:encode :enter ::m/entry 1]
[:encode :enter ::m/entry "2"]
[:encode :enter 'string? "2"]
[:encode :leave :malli.core/entry 1]
[:encode :leave ::m/entry 1]
[:encode :leave 'string? "2"]
[:encode :leave :malli.core/entry "2"]
[:encode :leave ::m/entry "2"]
[:encode :leave :map {:x 1, :y "2"}]]
@state)))))

Expand Down

0 comments on commit 12fd067

Please sign in to comment.