This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-03-24
Channels
- # announcements (31)
- # babashka (21)
- # babashka-sci-dev (4)
- # beginners (8)
- # cherry (4)
- # cider (32)
- # clj-kondo (15)
- # cljdoc (4)
- # cljsrn (4)
- # clojure (69)
- # clojure-dev (1)
- # clojure-europe (12)
- # clojure-nl (1)
- # clojure-norway (8)
- # clojure-uk (4)
- # clojurescript (16)
- # clr (6)
- # conjure (4)
- # fulcro (4)
- # hispano (1)
- # honeysql (1)
- # humbleui (5)
- # hyperfiddle (8)
- # lambdaisland (4)
- # lsp (8)
- # malli (24)
- # off-topic (3)
- # polylith (5)
- # reagent (10)
- # remote-jobs (3)
- # rewrite-clj (7)
- # scittle (12)
- # spacemacs (4)
- # sql (2)
- # tools-deps (29)
- # xtdb (7)
Is it possible to use malli to validate a JS object against a schema? Or do I always need to run js->clj
beforehand?
There is no way to validate JS objects, but you could use decoding to coerce JS object to clj maps:
(defn to-map [^js o]
(into {} (map (fn [[k v]]
[(keyword k) v])
(js/Object.entries o))))
(js/console.log
(pr-str
(m/decode [:map
{:decode/json to-map}
[:a :string]
[:b :string]
[:c [:map {:decode/json to-map}
[:foo :string]]]]
#js {:a "a"
:b "b"
:c #js {:foo "bar"}}
(mt/json-transformer))))
I'm seeing some really weird behaviour when trying to keywordize the result of my db-call (which is done with firestore-clj) The below structure is what I want, but it only keywordizes the top-level keys.
(let [board-link (get-board-link
{:platform "jira"
:board-id 10001})]
(m/decode board-link-schema board-link (mt/key-transformer {:decode keyword})))
Now the weird thing. The below example does behave as expected and keywordizes all sub-maps. This is 100% a copy of the first example with only the board-link binding being substituted with the result of the function-call in the first example (copied from the repl). This doesn't make any sense to me. Is it possible that the db-call adds something to the result that confuses malli?
(let [board-link {"item-links" [{"jira-item-id" "EX-6", "monday-item-id" 4176528394}],
"jira" {"board-id" 10001},
"monday" {"board-id" 3990111892}}]
(m/decode board-link-schema board-link (mt/key-transformer {:decode keyword})))
Yes, it is possible that the db is returning some objects that look like maps, but arent.
https://github.com/lurodrigo/firestore-clj/blob/master/src/clj/firestore_clj/core.clj
The code seems to refer to some DocumentSnapshot
class for example
and there is ds->plain
function
You could call class
for one of the map-like items from the db call
Mja, the ->plain is for the real-time-data but. I'm doing a normal pull, about which the docs say it returns a normal map: You can use pull to fetch the results as a map.
It was working with the top-level keys? What about objects inside item-links?
Or the values of jira and monday properties
But what is the class for those values
Not sure what the best solution is, but should be able to figure it out from here. Thanks 🙏
You can add :decode/json (fn [v] (to-clj-map v))
to the map schemas where the value need to be convereted from HashMap to clj
(into {} v)
or something might be enough
Once it is clj map, Malli will handle key and value transformations inside the map like regular
Just confirming this is now solved. Still had to tinker a bit, but that was more because of me being relatively new with malli. Thanks again for the quick help 😉
Hello. On this code:
(m/explain [:map {:closed true}]
{:a 43})
=>
{:schema [:map {:closed true}],
:value {:a 43},
:errors ({:path [:a], :in [:a], :schema [:map {:closed true}], :value nil, :type :malli.core/extra-key})}
It reports :value nil
. it is expected? I was expecting it to report :value 43