Fork me on GitHub
#malli
<
2024-03-16
>
jamesmintram10:03:26

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.

šŸ‘ 1
Safe Hammad13:03:47

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))

šŸ‘ 2
Daniel Ben Itzhak12:04:47

Had a similar issue but with nested maps, this solved the issue:

(m/decode
  schema
  value
  (mt/strip-extra-keys-transformer))
Adding for visibility

šŸ‘ 1
Lari Saukkonen14:03:58

Trying out malli-based custom autocomplete with emacs and cider:

šŸ¤Æ 1
Lari Saukkonen14:03:03

Schema:

(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.

vemv17:03:14

Nice! Looking forward :)

escherize20:03:16

Amazing! How does this work?

Lari Saukkonen05:03:14

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

šŸ‘ 2
vemv09:03:43

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.

šŸ™Œ 1
vemv09:03:06

I'm also interested in showing/walking Malli schemas interactively (in CIDER we have a "spec browser" with no Malli counterpart)

šŸ‘ 1
ikitommi06:03:06

looks awesome!

šŸ™Œ 1
Lari Saukkonen12:03:30

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.