Fork me on GitHub
#malli
<
2023-06-22
>
didibus21:06:07

In a map spec, can you refer to other specs unqualified? The doc only mentions qualified keys: https://github.com/metosin/malli#qualified-keys-in-a-map And just in general, how would I refer to another spec in Malli? Say I want to create an age spec which is a range from 1 to 120? For example? Or a email type for email and I want to then use that in lists and maps and all that?

didibus22:06:38

Like say given this example:

(def Address
  [:map
   [:id string?]
   [:tags [:set keyword?]]
   [:address
    [:map
     [:street string?]
     [:city string?]
     [:zip int?]
     [:lonlat [:tuple double? double?]]]]])
I want to make city an enum of say 500 cities, and I want to define that separately and refer it here? And I might refer it in other places, but I don't want the key to become ::city The wiki seems to define everything inline.

Michael Gardner00:06:31

put it in a var or binding, just like any other data

(def City
  [:enum "Ahmedabad" "Alexandria" ...])

(def Address
  [:map
   [:address
    [:city City]]])

Michael Gardner00:06:09

we do this even for widely-used schemas. We just put the var in a common schemas namespace

didibus00:06:38

I guess that's a simple way. I also found you can use mutable registry and set default registry. I won't what the pros and cons are as opposed to what you suggested

Ben Sless03:06:16

From experience using def forms, use a registry and set it as default

didibus03:06:14

@UK0810AQ2 Just making sure I'm not reading your sentence wrong, you're saying, from your experience, it's better to use a mutable registry and set it as default, than using def forms?

Ben Sless04:06:32

It doesn't have to be mutable (you also don't have to set it as default, you can use a schema schema), but yes, it's preferable to using def

👍 2
ikitommi06:06:46

> .. And I might refer it in other places, but I don't want the key to become ::city > You can do: 1. [:map ::city] - qualified key + registry 2. [:map [:city ::city]] - any key + registry 3. [:map [:city City]] - any key + Var At library code, you can't rely on the existence of the global registry, so 3 (or Schemas with explicit immutable registry) should be used. Used them all, leaning bit towards the global registry.

👍 4
ikitommi06:06:06

and, documentation improvement most welcome!

Michael Gardner21:06:15

@UK0810AQ2 why do you find it preferable?