Hi, Is there a way to specify custom error message for sequence schemas? If expecting a sequence of one or more ints and sending in something else, I get my custom message:
(-> (m/explain [:+ {:error/message "Custom message"} int?] 1) (me/humanize))
=> ["Custom message"]
But if I send in an empty sequence, I get "end of input" for the missing first element:
(-> (m/explain [:+ {:error/message "Custom message"} int?] []) (me/humanize))
=> [["end of input"]]
How can I override this to return ["Custom message"] ?somewhat surprisingly, this works:
(-> (m/explain [:+ [int? {:error/message "Custom"}]] []) (me/humanize))
[["Custom"]]this is because the actual end-of-input error is associated with the child schema:
(m/explain [:+ int?] [])
{:schema [:+ int?], :value [], :errors ({:path [0], :in [0], :schema int?, :value nil, :type :malli.core/end-of-input})}this allows you to distinguish where the input ended, and react accordingly:
user=> (def int-and-string [:cat [int? {:error/message "didn't get an int"}] [string? {:error/message "didn't get a string"}]])
#'user/int-and-string
user=> (-> (m/explain int-and-string []) (me/humanize))
[["didn't get an int"]]
user=> (-> (m/explain int-and-string [1]) (me/humanize))
[nil ["didn't get a string"]]wow it is difficult to write the right code in the repl 🤦
first, I thought -resolve-root-error was meant for this exact case
but it didn't work for me in the repl, but that was because I was running the wrong code
me/-resolve-root-error does do the trick here:
(-> (m/explain [:+ {:error/message "Custom message"} int?] []) (me/humanize {:resolve me/-resolve-root-error}))
["Custom message"]Thanks, that's what I was trying to accomplish 😊
Now the next problem is that I don't actually call me/humanize myself. The schema is used in a reitit route:
...
:parameters {:body [:map
[:dvp-id int?]
[:tilganggrupper [:+ {:error/message "Custom message"} int?]]]}
I could probably specify a custom malli-coercion and use it by setting :coercion my-custom-coercion on the route? Or change my default coercion which looks like this:
(malli-coercion/create
{:error-keys #{:humanized}
:encode-error #(set/rename-keys % {:humanized :errors})})
I think you need to use :error-keys #{:errors} and do the humanize yourself in your exception handler