Fork me on GitHub
#malli
<
2022-05-13
>
eskos12:05:17

I’d need a bit of help/ideas to implement conditional validation for a case when • vector with a single Thing validates as true as it normally would • vector with multiple Things validates true only if each Thing has an additional field • Thing is a record represented as map Practically this is “If only one record is present in result set, id field isn’t required, otherwise id field is mandatory”. There’s probably multiple ways to tackle this, I’m just not sure what would be a good approach.

eskos13:05:49

Basically what I have is

(m/schema [:or
           [:vector {:min 1} (mu/merge IdField Thing)]
           [:vector {:max 1} Thing]])
and this seems to work as intended but I’m not sure if this is the best way to do this. EDIT: Ordering also matters, the more complex schema must be first for sensible error output.

ikitommi13:05:32

Would :multi here?

[:multi {:dispatch count}
 [1 [:vector {:max 1} Thing]]
 [:malli.core/default [:vector {:min 2} (mu/merge IdField Thing)]]]

eskos14:05:37

Oh yes, :multi seems to work wonderfully! 👍