@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
there could be a pretty error formatter for that, e.g.
(and min (= min max)) (str "should be " min " characters")
=>
(-> [:schema {:registry {::str4 [:string {:min 4, :max 4}]}} ::str4]
(m/explain "kikka")
(me/humanize))
; => ["should be 4 characters"]
fixed that in master, thanks for giving it a though.
JSON Schema also has only minLength & maxLength.
(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"]}
Is anyone leaning on the current format of :errors
of m/explain
?
About to break the :path
and :in
so that :path
is a valid pointer to mu/get-in
.
this gives symmetry, that should have been there since beginning.
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"})}
(Schemas with named branches, e.g. :map
and :multi
use the name instead of vector index.
How can you validate a "naked" series of integers? Like 1,4,8,10
Not in a vector, they are coming in as a query parameter.