Fork me on GitHub
#malli
<
2021-07-05
>
oly07:07:29

is there a way to have different map layouts, I wanted to do something like this

[:map [:coord [:or 
               [:map {:closed true}]
               [:map {:closed true} [:x int? :y int?]]]]]
{:coord {}}
{:coord {:x 1 :y 1}}
I know I can make the whole map optional but what about allowing an empty map or a map with fixed keys, I tried a few things but could not find a way.

alpox07:07:40

@oliver.marks I think this works if you use for the second :map:

[:map {:closed true} [:x int?] [:y int?]]

oly07:07:17

yeah that works for the second map, just not sure how to make it work also for an empty map I tried putting optional in various places and tried :or and a few other things oh I might have missed some brackets above but its more an an illustration of one of the methods I tried

oly07:07:16

[:map {:closed true :optional true} [:x int?] [:y int?]]
also tried this but that does not work, I know I could make the :x and :y keys optional but really I want an all or nothing method

alpox07:07:26

I tried it with your :or above and that worked (after adding the missing brackets)

oly07:07:00

this passes ? {:coord {}} when I run it through m/valid i get missing keys or I was yesterday when I was testing πŸ˜›

oly07:07:52

nice, I will re check what I have maybe there was a different error which I did not spot

oly07:07:19

@alpox

[:map [:coord [:or 
               [:map {:closed true}]
               [:map {:closed true}
               [:x {:optional false} int?]
               [:y {:optional false} int?]]]]]

oly07:07:50

what about that, I think because the key do not have the optional map the :or is irrelevant anyway

oly07:07:27

although perhaps thats it, perhaps its optional thats messing me up looking at your screenshot again I can see it fails when one of the keys are missing

oly08:07:05

@alpox thanks for your rubber ducking, not an issue with malli I had 2 schmeas with similar name and was updating the wrong one πŸ˜•

alpox08:07:28

Oups πŸ™‚ np

Ben Sless08:07:47

I constantly get the feeling that I'm missing a schema to talk about map schemas in terms of key-value pairs

Ben Sless08:07:37

an annoying example to implement is mutual exclusion, such as a map which can have x, y and (z or u)

Ben Sless08:07:48

where z and u have different value types, too

ikitommi12:07:03

Hear hear. I think something like malli-keys-relations should be there. With qualified keywords, it’s less boilerplate (but, still has):

[:or 
 [:map ::x ::y ::z]
 [:map ::x ::y ::u]]

ikitommi12:07:51

spec-style would be:

[:map ::x ::y [:keys/or ::y ::u]]

ikitommi12:07:06

maybe:

[:and
 [:map ::x ::y ::z ::u]
 [:keys/or ::z ::u]]

Ben Sless12:07:33

JSON Schema's BNF was one of the motivations for it

Ben Sless12:07:15

> map which contains at least one ::x where ::x is [:or [:x int?] [:y float?]]

Ben Sless12:07:18

If we had it we could define JSON Schema with malli (translating the ebnf is straightforward), then define encoders and decoders to transform between the two

ikitommi16:07:41

added a video of the new function instrumention. three apis: advanced, normal users and for the lazy: https://github.com/metosin/malli/pull/471

πŸŽ‰ 8
ikitommi16:07:52

comments welcome