malli icon indicating copy to clipboard operation
malli copied to clipboard

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

Open sirmspencer opened this issue 2 years ago • 3 comments

(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.

sirmspencer avatar Jun 01 '23 19:06 sirmspencer

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?

ikitommi avatar Aug 14 '23 06:08 ikitommi

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

sirmspencer avatar Aug 29 '23 00:08 sirmspencer

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

ikitommi avatar Sep 03 '23 08:09 ikitommi