malli

ppandis 2024-12-30T19:55:24.165399Z

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

valtteri 2024-12-30T20:04:38.862459Z

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

ppandis 2024-12-30T20:06:17.661329Z

Oooops that actually worked!

πŸ‘ 1
ppandis 2024-12-30T20:07:18.670489Z

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

valtteri 2024-12-30T20:08:20.999799Z

Why would it be semantically incorrect?

ppandis 2024-12-30T20:12:23.318039Z

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.

valtteri 2024-12-30T20:13:57.289689Z

That’s fine, no worries πŸ™‚

valtteri 2024-12-30T20:20:00.710339Z

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.

valtteri 2024-12-30T20:20:38.251169Z

But anyway, enums are very useful with schemas

ppandis 2024-12-30T20:22:24.209259Z

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

πŸ‘ 1
ikitommi 2024-12-31T15:36:58.393379Z

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

πŸ‘ 1