Fork me on GitHub
Martynas M13:03:04

Hey. What function should I use to parse a string into a number? Let's say I have this schema: (m/parse :int 15) My initial assumption was that the parse would do the string parsing. But it... doesn't work at all. How do I make this return 15 : (m/parse :int "15") Is this the right function to use?


hi, it’s value transformation in malli, here are the docs:

Martynas M15:03:05

Thanks. That works.

Martynas M15:03:12

Why does parse exist then?


it's structural parsing, please read the part from the README. Malli itself uses it for parsing Clojure destructuring syntax to infer schemas from source code.

Ryan Tate17:03:46

Is it possible to combine :and with :cat? In spec this would be "`&`" (ampersand). I need to check each key with :cat and then the two keys together via a special fn. This works without the :and but once I add the :and and the :fn it fails:

(m/validate [:* [:and [:cat :symbol :string] [:fn (fn [[key value]] true)]]]  '(regex "foo"))
Ultimate form is going to be like
'(regex "foo" xpath "bar" ...) 
and I want :fn to check each pair. Thanks for any tips.

Ryan Tate17:03:52

Ack I don't think it will work anyway since the :cat does not produce the values for the next predicate as in spec. So even if I could make :and work it's not gonna give key/value to :fn. Hmm. Not sure how I can accomplish this. Maybe I can parse inside the validation

Ryan Tate17:03:01

OK I solved it like this, the special function is the inner one:

(m/validate [:fn (fn [seqn] (every? (fn [[key value]]  true) (m/parse [:* [:cat :symbol :string]] seqn)))] '(regex "foo" xpath "bar"))
👍 If anyone has a better idea just post here 🙃

Ryan Tate17:03:17

In order to make it work and composable with m/parse I think the schema will need to repeat the seqex. So parse will run 2x. Probably will still be faster than spec:

[:and [:fn (fn [seqn] (every? (fn [[key value]] true) (m/parse [:* [:cat :symbol :string]] seqn)))] [:* [:cat :symbol :string]]]

Ryan Tate17:03:46

Would still be nice if :and worked with cat though because then I could match multiple schemas across the same sequence. hmm