malli

https://github.com/metosin/malli :malli:
ikitommi 2020-07-29T12:48:28.260600Z

@dharrigan not fond of that. You can already say [:string {:min 4, :max 4}] , which is not that much longer than [:string {:length 4}]. And you can always uses aliases to save on typing:

[:schema 
 {:registry {::str4 [:string {:min 4, :max 4}]}} 
 ::str4]
or:
(def string4 [:string {:min 4, :max 4}])

(m/validate string4 "kikka")
; => false

ikitommi 2020-07-29T12:49:09.261200Z

there could be a pretty error formatter for that, e.g.

(and min (= min max)) (str "should be " min " characters")

ikitommi 2020-07-29T12:49:30.261400Z

=>

(-> [:schema {:registry {::str4 [:string {:min 4, :max 4}]}} ::str4]
    (m/explain "kikka")
    (me/humanize))
; => ["should be 4 characters"]

ikitommi 2020-07-29T13:00:44.262Z

fixed that in master, thanks for giving it a though.

ikitommi 2020-07-29T13:02:34.262900Z

JSON Schema also has only minLength & maxLength.

ikitommi 2020-07-29T13:09:48.264100Z

(def registry
  (reduce
    (fn [acc i]
      (assoc acc (str "string" i) [:string {:min i, :max i}]))
    {}
    (range 5)))

registry
;{"string0" [:string {:min 0, :max 0}],
; "string1" [:string {:min 1, :max 1}],
; "string2" [:string {:min 2, :max 2}],
; "string3" [:string {:min 3, :max 3}],
; "string4" [:string {:min 4, :max 4}]}

(-> [:map {:registry registry}
     [:s1 "string1"]
     [:s2 "string2"]
     [:s3 "string3"]]
    (m/explain {:s1 "hi", :s2 "hi", :s3 "hi"})
    (me/humanize))
;{:s1 ["should be 1 characters"]
; :s3 ["should be 3 characters"]}

ikitommi 2020-07-29T15:41:44.265300Z

Is anyone leaning on the current format of :errors of m/explain ?

ikitommi 2020-07-29T15:43:23.267Z

About to break the :path and :in so that :path is a valid pointer to mu/get-in.

ikitommi 2020-07-29T15:44:14.267800Z

this gives symmetry, that should have been there since beginning.

ikitommi 2020-07-29T15:57:44.268100Z

the paths are so much better now 🙂

(m/explain
  [:multi {:dispatch :type}
   [:sized [:map
            [:type [:= :sized]]
            [:size [:maybe int?]]]]
   [:human [:map
            [:type [:= :human]]
            [:name string?]
            [:age int?]
            [:address [:maybe [:map [:country keyword?]]]]]]]
  {:type :human
   :name "inkeri"
   :age "100"
   :address {:country "sweden"}})
;{:schema [:multi
;          {:dispatch :type}
;          [:sized [:map [:type [:= :sized]] [:size [:maybe int?]]]]
;          [:human [:map [:type [:= :human]] [:name string?] [:age int?] [:address [:maybe [:map [:country keyword?]]]]]]],
; :value {:type :human, :name "inkeri", :age "100", :address {:country "sweden"}},
; :errors (#Error{:path [:human :age]
;                 :in [:age]
;                 :schema int?
;                 :value "100"}
;           #Error{:path [:human :address 0 :country]
;                  :in [:address :country]
;                  :schema keyword?
;                  :value "sweden"})}

2
ikitommi 2020-07-29T15:58:16.269Z

(Schemas with named branches, e.g. :map and :multi use the name instead of vector index.

danielglauser 2020-07-29T23:12:21.269800Z

How can you validate a "naked" series of integers? Like 1,4,8,10

danielglauser 2020-07-29T23:13:10.270200Z

Not in a vector, they are coming in as a query parameter.