This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-06-25
Channels
- # announcements (2)
- # asami (16)
- # babashka (55)
- # beginners (27)
- # calva (14)
- # cider (5)
- # clj-kondo (16)
- # cljs-dev (22)
- # clojure (72)
- # clojure-europe (89)
- # clojure-nl (10)
- # clojure-uk (7)
- # clojured (1)
- # clojurescript (14)
- # community-development (4)
- # core-async (15)
- # emacs (10)
- # events (2)
- # fulcro (3)
- # graalvm (1)
- # graalvm-mobile (71)
- # helix (7)
- # honeysql (2)
- # introduce-yourself (1)
- # jobs-discuss (17)
- # juxt (3)
- # lsp (62)
- # malli (13)
- # meander (7)
- # off-topic (14)
- # pathom (54)
- # polylith (6)
- # re-frame (11)
- # releases (1)
- # sci (22)
- # sql (9)
- # tools-deps (84)
- # vim (37)
- # xtdb (18)
Hi there! I’m trying to use malli as a parsing+coercion library for external data. I’m a bit lost on what the schemas should look like and what function should I be using.
An example is I want to transform a map with shortened key names like this {"a" "11", "b" "10.01"}
into this {:ask 11.0 :bid 10.01}
. Another case is sequential data, which may look like this ["11" "10.01"]
, and I would want to get the same data back.
I know it’s possible to do some of these steps without malli, i.e. rename-keys or spec/explain with :sequential
, but it felt like there should be builtin idioms from the library/schema definitions that helps with the extra transformations that I am performing. Am I just missing something?
I’m no expert, but I think you want encode/decode
. See https://github.com/metosin/malli#value-transformation. You could write your own custom key-transformer
that maintains a mapping of the abbreviations you want.
Schema for map would be
[:map-of :keyword :number]
Or whatever more specific number type you want.Something like this in the end (non-tested code)
(m/encode [:map-of :keyword :number]
{:ask 11.0 :bid 10.01}
(mt/transformer
(mt/string-transformer)
(mt/key-transformer {:encode your-ns/kw->abbr
:decode your-ns/abbr->kw})))
;; =>> {"a" "11", "b" "10.01"}
Hello when using map
syntax is there a way of describing a oneOf relationship such that:
[:map
[:or
[:opt1 integer?]
[:opt2 integer?]]]
will allow either :opt1 or :opt2?Just for clarity and not sure there is an easier way but exclusive or looks like
(m/validate
[:and
[:or
[:map [:p integer?]]
[:map [:q integer?]]]
[:not
[:and
[:map [:p integer?]]
[:map [:q integer?]]]]]
{:p 123 :q 123}) => false
i'm curious if there is a better answer that generates gooder, but this would do the right validation
exclusive thanks @emccue
@garyberger Usually, :and
and :or
are the way to go, see example in http://malli.io but there is also a declarative map-keys-relations poc: https://github.com/bsless/malli-keys-relations