Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time humanize error brings "unknown error" #1114

Open
awb99 opened this issue Oct 14, 2024 · 2 comments
Open

time humanize error brings "unknown error" #1114

awb99 opened this issue Oct 14, 2024 · 2 comments
Labels
help wanted Help most welcome

Comments

@awb99
Copy link

awb99 commented Oct 14, 2024

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"]}
@ikitommi
Copy link
Member

Hi. This is unfortunate. Humanized errors do not have a multimethod to support easy extension of schema-based errors.

With current design, you need to add a property to the schema instance for this:

(defn -local-date-schema [] (-temporal-schema {:type :time/local-date :class LocalDate :type-properties {:min (. LocalDate -MIN) :max (. LocalDate -MAX), :error/message "not a valid 
LocalTime instance"}}))

(->> (m/explain (-local-date-schema) 123) (me/humanize))
; => ["not a valid LocalTime instance"]

See #1154

@ikitommi ikitommi added the help wanted Help most welcome label Jan 1, 2025
@ikitommi
Copy link
Member

ikitommi commented Jan 1, 2025

Would you like to add to add the [:type-properties :error/message] data to the time schemas? PR most welcome on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Help most welcome
Projects
Status: No status
Development

No branches or pull requests

2 participants