malli

https://github.com/metosin/malli :malli:
dakra 2021-02-04T14:27:44.050900Z

How can I read the properties of attributes in a map. E.g. the description of one-attr in this example

(def test-schema
    (malli/schema [:map {:description "test schema desc."}
                   [:one-attr {:description "attr description"} string?]]))

  (malli/properties test-schema)  ;; => {:description "test schema desc."}
  
  (malli/properties (mu/get test-schema :one-attr))  ;; => nil

๐Ÿ‘€ 2
ikitommi 2021-02-04T16:25:29.053200Z

@daniel415 try (mu/find schema key) and you can iterate the child entries with m/children

dakra 2021-02-04T16:30:56.055100Z

The mu/get does work and gives me the correct schema back but just m/properties is not returning anything. (only for maps) And m/children is not working in my example?

(-> (malli/schema [:map {:description "test schema desc."}
                     [:one-attr {:description "attr description"} string?]])
      (mu/find :one-attr)
      malli/children)
return this error
Execution error (ExceptionInfo) at malli.impl.util/-fail! (util.cljc:12).
:malli.core/invalid-schema {:schema :one-attr}

ikitommi 2021-02-04T16:32:24.055200Z

try removing the last m/children

ikitommi 2021-02-04T16:33:00.055400Z

mu/find should return the entry tuple, props being the second value

ikitommi 2021-02-04T16:35:01.055600Z

also, this should hold:

(-> [:map [:x [:int {:default 1}]]]
    (m/get :x)
    (m/properties))
;; -> {:default 1}

ikitommi 2021-02-04T16:35:19.055800Z

coding blind from a phone, might contain errors :)

ikitommi 2021-02-04T16:36:13.056Z

but: maps, map entries and map entry values can all have properties

dakra 2021-02-04T16:37:35.056200Z

Hmm, your example works. But when I simply have string? or so it doesn't work.

(-> [:map [:x {:default 1} string?]]
      (mu/get :x)
      (m/properties))
  ;; => nil

dakra 2021-02-04T16:38:54.056400Z

So this is the equivalent that works:

(-> [:map [:x [:string {:default 1}]]]
      (mu/get :x)
      (m/properties))
  ;; => {:default 1}

ikitommi 2021-02-04T18:52:29.057Z

string? doesnโ€™t have properties. is is short format for [string?]. You can add properties to it too, e.g. [string? {:default 1}]. Full example:

(-> [:map [:x [string? {:default 1}]]]
      (mu/get :x)
      (m/properties))
  ;; => {:default 1}
(still not at malli repl, just guessing what it returns)

ikitommi 2021-02-04T18:58:05.057200Z

now at the repl:

(def Schema
  [:map {:in "map"}
   [:x {:in "entry"} [:string {:in "value"}]]])

(m/properties Schema)
;; => {:in "map"}

(-> Schema
    (mu/find :x)
    (second))
;; => {:in "entry"}

(-> Schema
    (mu/get :x)
    (m/properties))
;; => {:in "value"}'

ikitommi 2021-02-04T19:00:09.057400Z

some helpers:

(defn entry-properties [schema key]
  (-> schema (mu/find key) (second)))

(defn value-properties [schema key]
  (-> schema (mu/get key) (m/properties)))

(m/properties Schema) ;; => {:in "map"}
(entry-properties Schema :x) ;; => {:in "entry"}
(value-properties Schema :x) ;; => {:in "value}

dakra 2021-02-04T19:44:55.057600Z

OK, thank you very much. Now it's clear. I wasn't aware that string? is shorthand for [string?] and also didn't really think about the difference between entry and value properties. Makes sense now ๐Ÿ™‚