This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-03-16
Channels
- # announcements (1)
- # babashka (26)
- # beginners (17)
- # clojure (18)
- # clojure-europe (4)
- # clojure-losangeles (1)
- # clojure-norway (42)
- # clojure-uk (3)
- # datalevin (3)
- # datomic (5)
- # fulcro (7)
- # funcool (2)
- # gratitude (1)
- # hoplon (15)
- # hyperfiddle (7)
- # lsp (5)
- # malli (12)
- # off-topic (3)
- # reitit (7)
- # releases (5)
- # remote-jobs (8)
- # shadow-cljs (21)
- # sql (9)
Simple question, and I am sure it has been asked before - but I am not really sure what I would be looking for! I have a schema defined, I would like to 'decode' this data to get a map of coerced data back. At the moment mali returns everything I give it, coercing the stuff that is described in the schema and leaving everything else. What I would like to do, is drop everything that is not defined in the schema - a little bit like a select-keys operation on a map.
You can use malli.core/explicit-keys
to pull the keys from the schema. So the following will "clean up" your data before you decode it:
(require '[malli.core :as m])
(select-keys value (m/explicit-keys schema))
Had a similar issue but with nested maps, this solved the issue:
(m/decode
schema
value
(mt/strip-extra-keys-transformer))
Adding for visibilitySchema:
(def Schema
[:map
[:field
[:map
[:id :int]
[:another-field
[:vector
[:map
[:id :int]
[:status [:enum :WORKING :NOT_WORKING :OLD]]
[:id :int]]]]
[:imaginary-subfield
[:map
[:field-first :string]
[:field-second :string]]]]]])
It has custom compliment source that reads prefix and form to match it with the schema and offers it through cider-nrepl middleware that i changed a bit.With malli I get subschemas with unique value paths, extract the right side name, in this case imaginary-subfield
from the call, then find value paths containing it and return next values as a completion candidates.
It is a compliment custom source that I use with cider-nrepl-middleware to return completion candidates to cider and emacs. It should work also for anything using cider-nrepl-middleware if they use it for completions but im not sure if showing just these if they are found is as easy.
Programming with schemas has the example for value-paths and subschemas:
https://github.com/metosin/malli?tab=readme-ov-file#programming-with-schemas
Compliment has docs about custom-source:
https://github.com/alexander-yakushev/compliment/wiki/Custom-sources
Here cider-nrepl-middleware registers compliment sources, I had to change this a bit to add my custom source:
https://github.com/clojure-emacs/cider-nrepl/blob/master/src/cider/nrepl/middleware/complete.clj
From the other thread I was aware that you were hacking something with Compliment, but it makes me happy that it's for Malli specifically - I'm a fan!
Bridging Compliment contexts and Malli subschema walking would seem a great innovation.
It could mean that, for instance, besides from completion, I could "jump to definition" for :id
, where :id
is dependent on the Malli schema present in the current Compliment context.
I'm also interested in showing/walking Malli schemas interactively (in CIDER we have a "spec browser" with no Malli counterpart)
Thanks, Iām glad that it was something that might add value in the future for someone else also. š I plan to start using this daily and expanding the variations to destructuring etc. and see what kind of corner cases and things I will bump into.