metosin / malli

High-performance data-driven data specification library for Clojure/Script.
Eclipse Public License 2.0
1.44k stars 205 forks source link

Explain stops at first error in vector. Add option to show all errors. #908

Open sirmspencer opened 1 year ago

sirmspencer commented 1 year ago

(malli.core/explain [:* :int] [1 2 "a" "b"])

{:schema #object[malli.core.t_malli$core91001], :value [1 2 "a" "b"], :errors ({:path [0], :in [2], :schema #object[malli.core.t_malli$core90615], :value "a"} {:path [], :in [2], :schema #object[malli.core.t_malli$core91001], :value "a", :type :malli.core/input-remaining})}

The second error here is a generic one that appear to show up as the error for the vector, after the error for the invalid item.

ikitommi commented 11 months ago

You can use :vector to get all errors:

(malli.core/explain [:vector :int] [1 2 "a" "b"])
;{:schema [:vector :int],
; :value [1 2 "a" "b"],
; :errors ({:path [0], :in [2], :schema :int, :value "a"} 
;          {:path [0], :in [3], :schema :int, :value "b"})}

What is your use case this these with sequence schemas?

sirmspencer commented 10 months ago

Does :vector allow an empty vector? This sounds like it should work.

ikitommi commented 10 months ago

Yes, it does. You can control the size requirements with :min and :max property.

(m/validate [:vector :any] []) ; => true
(m/validate [:vector {:min 1} :any] []) ; => false
(m/validate [:vector {:min 1} :any] [1]) ; => true