Fork me on GitHub
#malli
<
2022-09-12
>
Carlo10:09:39

Can I generate a value from a registry? Something like:

(def reg
  {::id int?
   ::name string?
   ::user [:tuple ::id ::name]})

(mg/generate {:registry reg} ::user)

Ferdinand Beyer13:09:03

Yes, by wrapping it in [:schema]:

(mg/generate [:schema {:registry reg} ::user])

🙌 1
Carlo13:09:24

Thank you, could you expand a bit more on why this works/where to find docs?

Ferdinand Beyer17:09:17

It works since generate requires a schema to generate examples for. A registry is not a schema, just a map. So you need to create a schema that uses your registry. As https://github.com/metosin/malli#local-registry, any schema can have a local registry property, e.g. [:map {:registry ,,,}]. Since you don't have such a schema in your case, you can use the generic [:schema] one, and just refer to a named one from the local registry. An alternative would be the m/schema function to instantiate the schema explicitly. Remember that forms such as [:map] are not schemas yet, but will be instantiated on demand. You can explicitly turn them into schemas using m/schema. Hope that helps!

Carlo14:09:27

That is much more clear, thanks for the thoughtful answer @U031CHTGX1T