metosin/malli

time humanize error brings "unknown error"

Open

#1114 aperta il 14 ott 2024

Vedi su GitHub
 (3 commenti) (1 reazione) (0 assegnatari)Clojure (237 fork)batch import
help wanted

Metriche repository

Star
 (1724 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

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

Guida contributor