Fork me on GitHub
#malli
<
2022-01-11
>
Jungwoo Kim02:01:38

Hi! I’m really into malli very recently. thanks! I have a question though. Here’s my example pants-schema.

(def pants-schema
  [:and
   [:map
    [:id int?]
    [:size {:optional true} [:maybe :int]]
    [:size-alphabet {:optional true} [:maybe [:enum "S" "M" "L"]]]]
   [:fn {:error/message "size and size alphabet should be nil-matched"}
    '(fn [{:keys [size size-alphabet]}]
       (or (and (nil? size) (nil? size-alphabet))
           (and (some? size) (some? size-alphabet))))]])
You can see my nil-matched fn schema. I’m trying to use the schema to validate for functions like below,
(defn do-something-with-pants
  {:malli/schema [:=> [:cat pants-schema] :nil]}
  [pants]
  (prn pants)) ; do something
and then, if i pass the value {:id 1 :size 90} into the do-something-with-pants , I want to extract the error like humanize results but I got the full error messages.
; Execution error (ExceptionInfo) at malli.core/-fail! (core.cljc:137).
; :malli.core/invalid-input {:input [:cat [:and [:map [:id int?] [:size [:maybe :int]] [:size-alphabet [:maybe [:enum "S" "M" "L"]]]] [:fn #:error{:message "size and size alphabet should be nil-matched"} (fn [{:keys [size size-alphabet]}] (or (and (nil? size) (nil? size-alphabet)) (and (some? size) (some? size-alphabet))))]]], :args [{:id 1, :size 90}], :schema [:=> [:cat [:and [:map [:id int?] [:size [:maybe :int]] [:size-alphabet [:maybe [:enum "S" "M" "L"]]]] [:fn #:error{:message "size and size alphabet should be nil-matched"} (fn [{:keys [size size-alphabet]}] (or (and (nil? size) (nil? size-alphabet)) (and (some? size) (some? size-alphabet))))]]] :nil]}
I know explain + humanize is very useful to see the readable results but as I feel like that’s good for development stage. In the web application, My needs are very important to me. How do I handle errors properly?