Fork me on GitHub

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.


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.


Would :multi here?

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


Oh yes, :multi seems to work wonderfully! 👍