Fork me on GitHub
#malli
<
2021-02-04
>
dakra14:02:44

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

đź‘€ 6
ikitommi16:02:29

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

dakra16:02:56

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}

ikitommi16:02:24

try removing the last m/children

ikitommi16:02:00

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

ikitommi16:02:01

also, this should hold:

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

ikitommi16:02:19

coding blind from a phone, might contain errors :)

ikitommi16:02:13

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

dakra16:02:35

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

dakra16:02:54

So this is the equivalent that works:

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

ikitommi18:02:29

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)

ikitommi18:02:05

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"}'

ikitommi19:02:09

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}

dakra19:02:55

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 🙂