Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First-class map syntax (with compact-maps) #406

Closed
ikitommi opened this issue Mar 29, 2021 · 1 comment · Fixed by #544
Closed

First-class map syntax (with compact-maps) #406

ikitommi opened this issue Mar 29, 2021 · 1 comment · Fixed by #544
Labels
enhancement New feature or request

Comments

@ikitommi
Copy link
Member

ikitommi commented Mar 29, 2021

Currently, the primary syntax for defining Malli schemas is the hiccup syntax. Malli also supports map syntax and a symmetric round-trip between the two. This is ok, but the we should investigate if a more compact map presentation would make sense, as a first-class concept:

Current

(def Fruit
  [:enum {:title "fruit"
          :description "a possible fruits"
          :swagger/default "apple"}
   "pear" "lemon"])

(m/form Fruit)
;[:enum {:title "fruit"
;        :description "a possible fruits"
;        :swagger/default "apple"}
; "pear" "lemon"]

(mu/to-map-syntax Fruit)
;{:type :enum,
; :properties {:title "fruit"
;              :description "a possible fruits"
;              :swagger/default "apple"},
; :children ["pear" "lemon"]}

(-> Fruit mu/to-map-syntax mu/from-map-syntax m/form (= Fruit))
; => true

Idea

(m/form Fruit {:syntax :hiccup})
;[:enum {:title "fruit"
;        :description "a possible fruits"
;        :swagger/default "apple"}
; "pear" "lemon"]

(m/form Fruit {:syntax :map})
;{:type :enum,
; :properties {:title "fruit"
;              :description "a possible fruits"
;              :swagger/default "apple"},
; :children ["pear" "lemon"]}

(m/form Fruit {:syntax :map-compact})
;{:malli/type :enum
; :title "fruit"
; :description "a possible fruits"
; :swagger/default "apple"
; :malli/children ["pear" "lemon"]}

Parsed childs

the last one would be great for cases where the children have a special meaning (like :=>), could be derived from m/parse results.

so, instead of:

(m/form [:=> {:doc "fn"} :cat :nil] {:syntax :map})
; {:type :=>
;  :properties {:doc "fn"}
;  :children [{:type :cat}
;             {:type :nil}]}

it would be:

(m/form [:=> {:doc "fn"} :cat :nil] {:syntax :map-compact})
;{:malli/type :=>
; :doc "fn"
; :malli/input {:malli/type :cat}
; :malli/output {:malli/type :nil}}

like clj-fx.

@ikitommi ikitommi changed the title First-class map syntax First-class map syntax (with compact-maps) Mar 29, 2021
@ikitommi
Copy link
Member Author

.. eventually, we should only support one map-syntax. With that, m/schema could support both, e.g. the following would mean the same:

(m/schema
  {:malli/type :=>
   :doc "fn"
   :malli/input {:malli/type :cat}
   :malli/output {:malli/type :nil}})

(m/schema [:=> {:doc "fn"} :cat :nil])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant