This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-03-14
Channels
- # announcements (3)
- # babashka-sci-dev (22)
- # beginners (6)
- # calva (36)
- # cljsrn (1)
- # clojure (59)
- # clojure-europe (31)
- # clojure-france (3)
- # clojure-gamedev (1)
- # clojure-nl (1)
- # clojure-norway (1)
- # clojure-uk (4)
- # clojurescript (6)
- # conjure (1)
- # cursive (11)
- # data-oriented-programming (1)
- # datahike (2)
- # docker (8)
- # duct (4)
- # emacs (1)
- # figwheel-main (5)
- # kaocha (1)
- # leiningen (8)
- # lsp (64)
- # malli (10)
- # membrane (5)
- # nrepl (11)
- # off-topic (5)
- # portal (6)
- # quil (9)
- # reagent (62)
- # reitit (15)
- # releases (3)
- # ring-swagger (2)
- # shadow-cljs (36)
- # specter (2)
- # tools-deps (21)
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: https://github.com/metosin/malli#value-transformation
Thanks. That works.
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.
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.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
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 🙃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]]]