Fork me on GitHub
#malli
<
2020-10-13
>
ikitommi07:10:43

https://github.com/metosin/malli/pull/278, added :key and :defaults to mt/default-value-transformer to make it easier to create empty/default values with it:

(m/decode
  [:map
   [:user [:map
           [:name :string]
           [:description {:ui/default "-"} :string]]]]
  nil
  (mt/default-value-transformer
    {:key :ui/default
     :defaults {:map (constantly {})
                :string (constantly "")}}))
; => {:user {:name "", :description "-"}}

ikitommi07:10:47

the values in :defaults can access the Schema instance, so can use any information from it to create the default, e.g. schema -> default function.

dangercoder08:10:16

Is it possible to override the default error message for :re?

Kevin09:10:52

[:re {:error/message "Invalid email"}
     #".+@.+\\.+"]
Something like this?

dangercoder09:10:45

I would like it to be general

dangercoder09:10:46

Like, for all regexp errors I apply a custom message

Kevin09:10:46

Ah like that. I'm not familiar with a solution for that

ikitommi10:10:38

not a good way to do that, but one can override the :errors from malli.error like this:

(-> #"\d+"
    (m/explain "kikka")
    (me/humanize {:errors {:re {:error/message "not a number"}}}))
; => ["not a number"]

dangercoder13:10:58

My goal is to have an error message like this: "Value: %s does not comply with regexp: %s" 🙂

ikitommi13:10:48

maybe:

(defn regex-error [{:keys [value schema]} _]
  (str "Value: " value " does not comply with regexp: " (first (m/children schema))))

(-> #"\d+"
    (m/explain "kikka")
    (me/humanize {:errors {:re {:error/fn regex-error}}}))
; => ["Value: kikka does not comply with regexp: \\d+"]

dangercoder13:10:50

I'll check it out. Thanks for the inspiration @ikitommi

genRaiy09:10:42

I am hitting a dumb problem with references

genRaiy09:10:47

(ns data.recursive-case
  (:require [malli.util :as mu]))

(def asset
  [:map
   [:asset/id string?]
   [:asset/status [:enum :initialized :accepted :active :revoked :failed]]
   [:asset/asset {:optional true} asset]])

(def org-asset
  (mu/merge
    asset
    [:map
     [:asset/org :org]]))

(def user-asset
  (mu/merge
    asset
    [:map
     [:asset/user :user]]))

genRaiy09:10:12

asset works OK but merge fails

genRaiy09:10:18

Execution error (ExceptionInfo) at malli.core/-fail! (core.cljc:79).
:malli.core/invalid-schema {:schema #object[clojure.lang.Var$Unbound 0x4bcbe824 "Unbound: #'data.recursive-case/asset"]}

genRaiy09:10:11

pretty sure I've misunderstood something to do with registries so would appreciate if anyone can suggest what's up this

Kevin09:10:07

I think this line is wrong in the asset def?

[:asset/asset {:optional true} asset]]

Kevin09:10:27

You're referring to asset, but it's in the definition itself

Kevin09:10:21

Judging from the naming, you actually want it to be recursive?

3
Kevin09:10:11

You might need to add asset into a registry which you actually want to use it in. e.g. something like this:

{::asset
 [:map
  [:asset/id string?]
  [:asset/status [:enum :initialized :accepted :active :revoked :failed]]
  [:asset/asset {:optional true} [:ref ::asset]]]}
https://github.com/metosin/malli#recursive-schemas

genRaiy12:10:41

yes ... I figured it was a registry thing. Thanks for the advice

katox11:10:57

@ikitommi I have been bitten by round-trip java.time.Instant -> string -> malli -> #inst. The problem manifests in java9+ because of increased precision from miliseconds to microseconds. I noticed there is a bigger plan for time handling code but in the meantime I patched the string->date transformer. Should I do a PR for that? See https://github.com/metosin/malli/compare/master...katox:timestamp-frac-precision

miikka13:10:56

Seems like a good idea (as long as the code still works on Java8 to the extent that it can work)

katox07:10:01

I tested the change in both j8 and j11. It works the same. Issuing PR.

shem15:10:27

i have JSON data that has entries like "Start":new Date(1413147600000) . clojure JSON parsers (at least Cheshire and Jsonista) choke on this. i wonder if it's possible to use malli.core/decode with the :enter interceptor to parse this and transform it to "Start": 1413147600000 ? my first attempt produces an exception

sparkofreason18:10:32

Schema inference is awesome going to save me a tone of time. Before I start reinventing a wheel, is anybody working on transforming malli schema to Datomic schema?

ikitommi16:10:19

haven’t seen anything for this, looking forward to your example 🙂

sparkofreason14:10:36

Looks to be pretty straightforward. I've done this with spec before. There were two headaches there, one being the lack of a public data representation for specs, second being opaque predicates. In the latter case we used test.check to generate examples and tried to infer from that. I think you do the same thing here, but leveraging malli's inference.