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 "-"}}
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.
Is it possible to override the default error message for :re
?
[:re {:error/message "Invalid email"}
#".+@.+\\.+"]
Something like this?I am hitting a dumb problem with references
(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]]))
asset
works OK but merge fails
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"]}
pretty sure I've misunderstood something to do with registries so would appreciate if anyone can suggest what's up this
I think this line is wrong in the asset
def?
[:asset/asset {:optional true} asset]]
You're referring to asset
, but it's in the definition itself
Judging from the naming, you actually want it to be recursive?
I would like it to be general
Like, for all regexp errors I apply a custom message
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-schemasAh like that. I'm not familiar with a solution for that
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"]
@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
I tested the change in both j8 and j11. It works the same. Issuing PR.
yes ... I figured it was a registry thing. Thanks for the advice
Seems like a good idea (as long as the code still works on Java8 to the extent that it can work)
My goal is to have an error message like this: "Value: %s does not comply with regexp: %s" ๐
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+"]
I'll check it out. Thanks for the inspiration @ikitommi
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
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?
havenโt seen anything for this, looking forward to your example ๐
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.