Fork me on GitHub
#malli
<
2021-06-16
>
Yehonathan Sharvit08:06:56

What's the rationale behind the fact that a set is considered as :sequential ? For instance

(validate [:sequential string?] #{"aa"}) ;; false

ikitommi08:06:38

it’s following the clojure way:

{:sequential (m/-collection-schema {:type :sequential, :pred sequential?})}

(sequential? #{}) ; => false

ikitommi08:06:39

you should be able create custom collections types easily that access both sequentials and sets.

Yehonathan Sharvit08:06:03

@ikitommi Could you share a pointer to the documentation about custom collection types?

Yehonathan Sharvit10:06:33

How it would look like to create a custom :sequential-or-set predicate? And where would i put its definition?

ikitommi15:06:07

1. def a Var and use it instead of the type (keword), like with Reagent 2. add it to a registry, see README for alternatives

ikitommi15:06:52

(def SequentialOrSet (m/-col...))

(m/validate [SequentialOrSet :int] [1])

borkdude11:06:20

@viebel sequential means: it has a defined order. sets are not ordered, similar to maps, although you can create sequences out of them

Yehonathan Sharvit14:06:05

Yeah. It makes sense. But still surprising.

Yehonathan Sharvit14:06:41

I think we need a name and a predicate for a bunch of things that could be either in a set or in a sequence

Yehonathan Sharvit14:06:30

My use case is a function that receives a bunch of ids and return the entities whose id is contained in the bunch

ikitommi17:06:03

you could always say [:or [:sequential :int] [:set :int]].

ikitommi17:06:44

or, one could always transform the values always to sets/vectors.

eskos18:06:42

coll? works as well, if you don't mind using predicates directly

ikitommi18:06:10

(coll? {}) ;=> true

ikitommi18:06:51

coercion is always an option:

(m/decode
 [:set :int]
 [1 2 3]
 (mt/collection-transformer))
; => #{1 2 3}

deadghost21:06:44

Any recommendations on how to approach extracting out a registry from a schema? For example:

[:map
  [::id int]
  [:name string?]
  [::country {:optional true} string?]]

to

{::id int?
 ::country string?}

and as a nice to have, the schema simplified to use the new registry:

[:map
  ::id
  [:name string?]
  [::country {:optional true}]]
Use cases: • Simplify large schemas • Finding differences in semantics • Refactoring multiple schemas to use a shared registry