malli

https://github.com/metosin/malli :malli:
ikitommi 2020-10-13T07:26:43.139100Z

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 "-"}}

ikitommi 2020-10-13T07:27:47.140200Z

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.

dangercoder 2020-10-13T08:36:16.142500Z

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

kwrooijen 2020-10-13T09:06:52.142700Z

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

raymcdermott 2020-10-13T09:15:42.143500Z

I am hitting a dumb problem with references

raymcdermott 2020-10-13T09:15:47.143800Z

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

raymcdermott 2020-10-13T09:16:12.144200Z

asset works OK but merge fails

raymcdermott 2020-10-13T09:16:18.144500Z

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"]}

raymcdermott 2020-10-13T09:17:11.145500Z

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

kwrooijen 2020-10-13T09:27:07.145800Z

I think this line is wrong in the asset def?

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

kwrooijen 2020-10-13T09:27:27.146Z

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

kwrooijen 2020-10-13T09:28:21.146200Z

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

1
dangercoder 2020-10-13T09:32:45.146400Z

I would like it to be general

dangercoder 2020-10-13T09:32:46.146600Z

Like, for all regexp errors I apply a custom message

kwrooijen 2020-10-13T09:33:11.146800Z

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

kwrooijen 2020-10-13T09:36:46.147200Z

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

ikitommi 2020-10-13T10:58:38.147400Z

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"]

katox 2020-10-13T11:52:57.150800Z

@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

katox 2020-10-14T07:50:01.157700Z

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

raymcdermott 2020-10-13T12:54:41.150900Z

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

miikka 2020-10-13T13:24:56.151200Z

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

dangercoder 2020-10-13T13:26:58.151400Z

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

ikitommi 2020-10-13T13:34:48.151600Z

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+"]

dangercoder 2020-10-13T13:40:50.151900Z

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

shem 2020-10-13T15:34:27.156200Z

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

shem 2020-10-13T15:34:58.156300Z

sparkofreason 2020-10-13T18:19:32.157500Z

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?

ikitommi 2020-10-15T16:23:19.166100Z

havenโ€™t seen anything for this, looking forward to your example ๐Ÿ™‚

sparkofreason 2020-10-17T14:24:36.228900Z

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.