Fork me on GitHub
#malli
<
2020-07-29
>
ikitommi12:07:28

@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

ikitommi12:07:09

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

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

ikitommi12:07:30

=>

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

ikitommi13:07:44

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

ikitommi13:07:34

JSON Schema also has only minLength & maxLength.

ikitommi13:07:48

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

ikitommi15:07:44

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

ikitommi15:07:23

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

ikitommi15:07:14

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

ikitommi15:07:44

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

6
ikitommi15:07:16

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

danielglauser23:07:21

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

danielglauser23:07:10

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