Hello everyone! Is it possible in Malli to create function schema with second argument dependent from the first one? Something like this:
(m/=> price->str [:function
[:=> [:cat [:enum :bytes :minutes] :int] :string]
[:=> [:cat [:= :date] [:map
{:closed true}
[:days {:optional true} :int]
[:months {:optional true} :int]
[:years {:optional true} :int]]] :string]])
But this approach returns :malli.core/duplicate-arities error...So is it a function with two positional arguments that can be either 1. an enum of :bytes, :minutes or 2. a date and a map?
Three different variavnts:
• :bytes keyword and integer value;
• :minutes keyword and integer value;
• :date keyword and a map.
It would be nice to use multimethod, but like I know so far Malli doesn't support instrumentation for multimethods... doesn't it?
Yeah I don't remember it supporting multi-methods, perhaps try something with an :or and maps for the argument?
[:or
[:map [:key1 :int]]
[:map [:key2 :int]]]
{:dispatch :bytes, :value 123}
{:dispatch :minutes, :value 123}
{:dispatch :date, :value {}}:or not working. It throws invalid-schema. Thank you for approach with map. It will definitely work, but isn't suitable for my case.
I will validate input inside the function then despite it is not so convient.
you can use function guards to validate arguments instead of inside of the function https://github.com/metosin/malli/blob/master/docs/function-schemas.md#function-guards
Yes. Not a bad idea!