Fork me on GitHub
#malli
<
2021-12-12
>
ikitommi08:12:21

related to is-a relations, malli should have more concrete type definitions in the core, e.g. pos-int? is not a type, it’s a :int with constraint of being positive. Less types to map in transformers and just better. Related: • https://github.com/metosin/malli/issues/264https://github.com/clj-kondo/clj-kondo/blob/d9fca2705863e3e604e004ccb942e0b3d2e268ec/src/clj_kondo/impl/types.clj#L18-L51

ikitommi16:12:27

on master - adding type-hints for providers, starting with :map-of:

(require '[malli.provider :as mp])

(mp/provide
  [^{::mp/hint :map-of}
   {:a {:b 1, :c 2}
    :b {:b 2, :c 1}
    :c {:b 3}
    :d nil}])
;[:map-of
; keyword?
; [:maybe [:map
;          [:b int?]
;          [:c {:optional true} int?]]]]

ikitommi16:12:57

the current threshold with unified key & values schemas is 3, so:

(mp/provide
  [{:a [1]
    :b [1 2]
    :c [1 2 3]}])
; [:map-of keyword? [:vector int?]]

ikitommi16:12:04

… can be configured via options.

ikitommi16:12:23

providers start to be useful 🙂

ikitommi20:12:56

adding :tuple inferreing, provider already 89 loc 🙀

ikitommi20:12:49

with :malli.provider/tuple-threshold defaulting to 3:

;; tuple-like with too few elements
   [[:vector some?]
    [[1 "2" true]
     [2 "2" true]]]

   ;; tuple-like with enough samples
   [[:tuple int? string? boolean?]
    [[1 "2" true]
     [2 "2" true]]
    {::mp/tuple-threshold 2}]

   ;; tuple-like with enough samples
   [[:tuple int? string? boolean?]
    [[1 "2" true]
     [2 "2" true]
     [3 "3" true]]]

   ;; tuple-like with non-coherent data
   [[:vector some?]
    [[1 "2" true]
     [2 "2" true]
     [3 "3" "true"]]]

   ;; a homogenous hinted tuple
   [[:tuple int? string? boolean?]
    [^{::mp/hint :tuple} [1 "2" true]
     [2 "2" true]]]

   ;; a hererogenous hinted tuple
   [[:tuple int? string? some?]
    [^{::mp/hint :tuple} [1 "2" true]
     [2 "2" "true"]]]

   ;; invalid hinted tuple
   [[:vector some?]
    [^{::mp/hint :tuple} [1 "2" true]
     [2 "2" true "invalid tuple"]]]