blosavio / speculoos

An experimental Clojure data validation library
MIT License
0 stars 0 forks source link

`valid-collections?` only considers boolean `true`, not truthy values #9

Closed blosavio closed 2 hours ago

blosavio commented 3 hours ago

Regarding: version 2SNAPSHOT-4

Background: The API documentation for valid-collections? states

Note: valid-collections? returns true if validation returns zero {:valid? falsey} results.

Observation: Collection predicates that return a non-boolean are not regarded as valid, even through they are truthy.

(get [42] 0)
;; => 42

(valid-collections? [42] [#(get % 0)])
;; => false

Expected output:

(valid-collections? [42] [#(get % 0)])
;; => true

Comment: validate-collections, the underlying function, handles this case as expected.

(validate-collections [42] [#(get % 0)])
;; => ({:datum [42], :valid? 42, :path-predicate [0], :predicate #fn--9731, :ordinal-path-datum [], :path-datum []})

Also, coercing that same predicate's output to a boolean produces the expected result.

(valid-collections? [42] [#(boolean (get % 0))])
;; => true

Approach: Remove the current implementation's (true? ...).

blosavio commented 2 hours ago

Bug fixed in version 2-SNAPSHOT5.

(valid-collections? [42] [#(get % 0)])
;; => true

(valid-collections? {:x 42} {:foo #(get % :x)})
;; => true

Replaced (every? #(true? ...) ...) with (empty? (only-invalid ...)) idiom.