malli 2024-12-30

Hello, how can one define a specific keyword for a key's value in Malli? For example, given this data structure where :type is always :url

{:type :url
 :content-version "v1.2.3"
 :value "xyz"}
What the schema def should look like? I have tried something like this
(def License 
  [:map 
   [:type {:default :url} :keyword]
   [:content-version :string]
   [:value :string]])
but malli.generator/generate will create a random value for :type and I'm not sure how and if I should be using the _malli.transform_/default-value-transformer

How about a single element :enum ? [:type [:enum :url]]

Oooops that actually worked!

πŸ‘ 1

I do wonder if this is the "correct" way to represent this (semantically) but happy to have a working solution!

Why would it be semantically incorrect?

I'd expect to have the ability to define literal values without presenting them as enums I guess, though it would probably look the same as the enum but with a different keyword? I'm not sure how to support my point in a pinch atm to be honest. Only now starting to use Malli and I'm still trying to figure it all out.

That’s fine, no worries πŸ™‚

In general enum is a type for a set of constants. There maybe could be a :const that could work as a special case of enum with exactly one possible value.

But anyway, enums are very useful with schemas

Yes, the :const example is probably a good analogy. Nonetheless, thank you very much πŸ˜‰

πŸ‘ 1

there is also :=, e.g. [:type [:= :url]]

πŸ‘ 1