Fork me on GitHub
#malli
<
2022-02-24
>
henryw37406:02:13

@lambder above issue fixed on the latest release - see https://github.com/henryw374/time-literals readme for details. tl;dr it was basically a workaround for a problem with Clojurescript that has taken some time to get fixed - see https://clojure.atlassian.net/browse/CLJS-3294

lambder09:02:29

this worked like a charm 😄

rovanion11:02:12

@ikitommi I wrote a version of -min-max-pred that can deal with nil being passed to it as I could not figure out an f that could transform nil into a number that would make sense min and max to apply to. If you think you have use for it anywhere in Malli, here it is:

(defn -nilable-min-max-pred [f nilable]
  (fn [{:keys [min max]}]
    (cond
      (not (or min max))    nil
      (and  min max f nilable) (fn [x] (if (nil? x) true (let [size (f x)] (<= min size max))))
      (and  min max f)         (fn [x] (let [size (f x)] (<= min size max)))
      (and min max nilable)    (fn [x] (if (nil? x) true (<= min x max)))
      (and min max)            (fn [x] (<= min x max))
      (and min f nilable)      (fn [x] (if (nil? x) true (<= min (f x))))
      (and min f)              (fn [x] (<= min (f x)))
      (and min nilable)        (fn [x] (if (nil? x) true (<= min x)))
      min                      (fn [x] (<= min x))
      (and max f nilable)      (fn [x] (if (nil? x) true (<= (f x) max)))
      (and max f)              (fn [x] (<= (f x) max))
      (and max nilable)        (fn [x] (if (nil? x) true (<= x max)))
      max                      (fn [x] (<= x max)))))
And its use in my code is just:
(register! :html/nilable-int
  (malli/-simple-schema
   (fn [opts _]
     {:type            :html/nilable-int
      :pred            (some-fn int? nil?)
      :property-pred   (su/-nilable-min-max-pred nil true)
      :description     "An int or nil. Empty string is transformed to nil."
      :type-properties {:decode/string (fn [x] (when-not (string/blank? x)
                                                 (mtransform/-string->long x)))
                        :gen/schema [:maybe [:int opts]]}})))