Fork me on GitHub
#malli
<
2023-01-28
>
pithyless12:01:14

Can I define a generator for [:map [:a :string] [:b string]] such that I can :gen/fmap the :b based on the generated value of :a ?

pithyless12:01:48

eg. if using faker I can generate:

{:company-name "Acme Inc"}
but I'd like to slugify the name via some function:
{:company-name "Acme Inc.", :slug "acme"}

ikitommi12:01:21

so, :gen/fmap is not good enough here?

pithyless12:01:52

Should I :gen/fmap on the :map itself?

ikitommi12:01:03

yes, I would do that

pithyless12:01:36

That makes sense. Thanks for the sanity check :)

👍 2
nonrecursive17:01:39

hey y’all, is there a way to create a map spec where all the keys are optional, but values must have one of the keys to be valid?

nonrecursive17:01:24

eg given [:map [:a {:optional true}] [:b {:optional true}]], then {:a :foo} would be valid but {:c :foo} would not

nonrecursive17:01:12

i can think of [:or [:map [:a]] [:map [:b]]] but that seems clunky?

pithyless18:01:30

well, it sounds like you don’t want optional but you’re actually modeling a union type - I’d go with :multi or :or - but if that feels clunky you probably need an :and where you first spec the map and then check with custom fn that at least one of the expected keys exists.

👍 4
pithyless18:01:48

(You’ve still got things like :merge to help you pull out the repetitive parts of the individual unions)