Fork me on GitHub
#malli
<
2023-09-07
>
robert-stuttaford07:09:23

what is the correct spec to use for a seq that could be a vector or a list or a set? the docs say "You can use :sequential for any homogeneous Clojure sequence", but that's not true:

(for [v [[1]
           (list 1)
           #{1}]]
    [v (m/validate [:sequential any?] v)])

  ;; ([[1] true]
  ;;  [(1) true]
  ;;  [#{1} false])
use case: a public API that can accept both EDN and JSON data. EDN uses sets, JSON uses arrays, which marshall to vectors

radhika08:09:39

a set isn't sequential? in clojure although it is seqable (order is not guaranteed). you could make an :or schema with :sequential :set to capture both value forms. but I'd also consider canonicalizing the API input value into a set before processing further.

ikitommi10:09:26

Do you want to retain the original collection type or convert internally to.. sets?

ikitommi10:09:29

I think json transformers decodes vectors to sets automatically

ikitommi10:09:07

also, there is mt/collection-transformer to change any (supported) collection type to another

ikitommi10:09:01

Not sure why :sequential doesn't allow lists :thinking_face:

ikitommi10:09:46

What does: (m/coerce [:set :int] [1 2] mt/json-transformer) do? (not at computer)

robert-stuttaford07:09:01

@U055NJ5CC :sequential does allow lists 🙂

Bingen Galartza Iparragirre08:09:55

Hello! Is this behavior expected?

dev> (m/encode [:and [:keyword] [:enum :one :two]] :one mt/string-transformer)
"one"
dev> (m/encode [:enum :one :two] :one mt/string-transformer)
:one

ikitommi12:09:02

It's already fixed in master 😎

Bingen Galartza Iparragirre12:09:51

Wow, that was fast! thank you

👍 2