Fork me on GitHub
#malli
<
2023-03-30
>
Marius12:03:29

Is there a conventient way to recursively make all keys optional using mu/optional-keys ?

opqdonut12:03:25

I think you'll just have to use m/walk to do it

opqdonut12:03:46

There could be a function for it in malli.util . It comes up often enough.

opqdonut12:03:58

I'm pretty sure I've written it at least once, but can't find it.

opqdonut12:03:16

(defn make-all-keys-optional
  "Recursively apply mu/optional-keys to map schemas"
  [schema]
  (m/walk
   schema
   (m/schema-walker
    (fn [schema]
      (if (= :map (m/type schema))
        (mu/optional-keys schema)
        schema)))))

opqdonut12:03:17

found it 🙂

Marius12:03:23

Awesome! Thank you very much!

Marius15:03:41

Hi all, I have a question regarding Reitit Malli request coercion: When receiving a Transit request, the string decoders defined in my schema are executed, if it is a JSON request, it is only transformed by Muuntaja, but the Malli string decoders seems to be skipped… does that make sense? Background: I’m using the string decoders to convert strings into dates or big decimals.

Marius19:03:52

Found the solution: I had to add an additional :decode/json property on my LocalDate / BigDecimal type, which a copy of the :decode/string property.

Marius19:03:44

Looks like this for LocalDate:

(def LocalDate
  (mc/-simple-schema
   {:type 'LocalDate
    :pred #(instance? java.time.LocalDate %)
    :type-properties {:error/message "invalid date"
                      :decode/string #(t/date %)
                      :decode/json #(t/date %)
                      :json-schema {:type "string"
                                    :format "date"}
                      :gen/gen  (gen/fmap #(t/>> (t/today) (t/new-period % :days)) (gen/choose -365 365))}}))

Marius19:03:22

t is tick.core

simonacca22:03:08

Hi all, one more question: is it possible to walk a spec + value in Malli? Say for example from the following spec

[:schema {:registry {::id :int
                      ::address [:map
                                 [::street :string]
                                 [::city ::id]]
                      ::user [:map
                              [:name ::id]
                              ::address]}}
  ::user]
and example data
{:name -10257,
  :my-ns/address
  #:my-ns{:street "bhvZWkA2KL37u7y25O3R", :city 122054499}}
one wants to extract all values corresponding to an ::id key, that is: [122054499, -10257] Can this be done? I look at value transformations but they seem to be hardcoded for a prewalk, while here we need a postwalk. Am I missing something? :thinking_face:

simonacca22:03:25

Duh, a transformer's encode operation can take an interceptor as an operator for a certain schema. Or a :compile key if you also need to access the schema, like in this case 🎉

ikitommi16:04:16

Do you have a working code example how to do this?