What's the rationale behind the fact that a set is considered as :sequential
?
For instance
(validate [:sequential string?] #{"aa"}) ;; false
it’s following the clojure way:
{:sequential (m/-collection-schema {:type :sequential, :pred sequential?})}
(sequential? #{}) ; => false
you should be able create custom collections types easily that access both sequentials and sets.
@ikitommi Could you share a pointer to the documentation about custom collection types?
sure: https://github.com/metosin/malli/blob/master/src/malli/core.cljc#L813. Documentation PRs most welcome 🙂
in use: https://github.com/metosin/malli/blob/master/src/malli/core.cljc#L1925-L1927
How it would look like to create a custom :sequential-or-set
predicate?
And where would i put its definition?
@viebel sequential means: it has a defined order. sets are not ordered, similar to maps, although you can create sequences out of them
Yeah. It makes sense. But still surprising.
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
My use case is a function that receives a bunch of ids and return the entities whose id is contained in the bunch
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
(def SequentialOrSet (m/-col...))
(m/validate [SequentialOrSet :int] [1])
you could always say [:or [:sequential :int] [:set :int]]
.
or, one could always transform the values always to sets/vectors.
coll?
works as well, if you don't mind using predicates directly
(coll? {}) ;=> true
coercion is always an option:
(m/decode
[:set :int]
[1 2 3]
(mt/collection-transformer))
; => #{1 2 3}
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