malli

https://github.com/metosin/malli :malli:
Yehonathan Sharvit 2021-06-16T08:03:56.148500Z

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

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

ikitommi 2021-06-16T08:19:38.149300Z

it’s following the clojure way:

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

(sequential? #{}) ; => false

ikitommi 2021-06-16T08:20:39.150100Z

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

Yehonathan Sharvit 2021-06-16T08:50:03.151300Z

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

ikitommi 2021-06-16T09:16:00.151400Z

sure: https://github.com/metosin/malli/blob/master/src/malli/core.cljc#L813. Documentation PRs most welcome 🙂

Yehonathan Sharvit 2021-06-16T10:06:33.152Z

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

borkdude 2021-06-16T11:23:20.152600Z

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

Yehonathan Sharvit 2021-06-16T14:08:05.153300Z

Yeah. It makes sense. But still surprising.

Yehonathan Sharvit 2021-06-16T14:08:41.153900Z

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 Sharvit 2021-06-16T14:09:30.155Z

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

ikitommi 2021-06-16T15:45:07.155600Z

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

ikitommi 2021-06-16T15:46:52.155800Z

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

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

ikitommi 2021-06-16T17:53:03.158200Z

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

ikitommi 2021-06-16T17:53:44.159Z

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

eskos 2021-06-16T18:05:42.160800Z

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

ikitommi 2021-06-16T18:30:10.160900Z

(coll? {}) ;=> true

ikitommi 2021-06-16T18:33:51.161100Z

coercion is always an option:

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

deadghost 2021-06-16T21:10:44.165400Z

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