metosin/malli

time humanize error brings "unknown error"

Open

#1,114 opened on Oct 14, 2024

View on GitHub
 (3 comments) (1 reaction) (0 assignees)Clojure (1,724 stars) (237 forks)batch import
help wanted

Description

I made a schema that is using malli.experimental.time. I check if one key is of either local-datetime zoned-datetime or instant. malli/validate is working as it should , explain does not give back errors, the explained error I get is this: {:exit-date ["unknown error" "unknown error" "unknown error" "unknown error"]}

This is the sourcecode to reproduce the error

(ns quanta.trade.report.roundtrip.validation
  (:require
   [tick.core :as t]
   [malli.core :as m]
   [malli.registry :as mr]
   [malli.error :as me]
   [malli.experimental.time :as time]
  ;(:import
  ; (java.time Duration Period LocalDate LocalDateTime LocalTime Instant
  ;            ZonedDateTime OffsetDateTime ZoneId OffsetTime))
  )

(def r
  (mr/composite-registry
   m/default-registry
   (mr/registry (time/schemas))))

(def above-zero 0.0000000000000001)

(def Roundtrip
  [:map
   [:asset :string]
   [:side [:enum :long :short]]
   [:qty [:double]]
   [:entry-price [:double {:min quanta.trade.report.roundtrip.validation/above-zero}]]
   [:exit-price [:double {:min quanta.trade.report.roundtrip.validation/above-zero}]]
   [:entry-date [:or :time/local-date :time/local-date-time :time/zoned-date-time :time/instant]]
   [:exit-date  [:or :time/local-date :time/local-date-time :time/zoned-date-time :time/instant]]
   [:entry-idx {:optional true} [:int]]
   [:exit-idx {:optional true} [:int]]])

(defn validate-roundtrip [rt]
  (m/validate Roundtrip rt {:registry r}))

(defn human-error-roundtrip [rt]
  (->> (m/explain Roundtrip rt {:registry r})
       (me/humanize)))

(comment

  ;; test with a roundtrip that is ok

  (def rt1 {:asset "QQQ" 
            :side :long
            :qty 1.0
            :entry-price 105.0
            :exit-price 110.0
            :entry-idx 15
            :exit-date (t/zoned-date-time)
            :entry-date (t/date)})
  
  (validate-roundtrip rt1)
  ;; => true

  (human-error-roundtrip rt1)
  ;; => nil
  

  (human-error-roundtrip
   {:asset "QQQ" :side :long
    :entry-price 105.0
    :exit-price 110.0
    :entry-date (t/instant)})
  ;; => {:qty ["missing required key"], :exit-date ["missing required key"]}

  (human-error-roundtrip
 {:asset "QQQ" :side :long
  :entry-price 105.0
  :exit-price 110.0
  :entry-idx "asdf"})
  ;; => {:qty ["missing required key"],
  ;;     :entry-date ["missing required key"],
  ;;     :exit-date ["missing required key"],
  ;;     :entry-idx ["should be an integer"]}

  (human-error-roundtrip
   {:asset "QQQ" :side :long :qty 1.0
    :entry-price 105.0
    :exit-price 110.0
    :entry-date (t/instant)
    :exit-date 3})
  ;; => {:exit-date ["unknown error" "unknown error" "unknown error" "unknown error"]}

Contributor guide