This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-09-06
Channels
- # announcements (14)
- # babashka (12)
- # beginners (61)
- # biff (2)
- # calva (16)
- # clj-kondo (22)
- # cljdoc (7)
- # clojure (131)
- # clojure-europe (52)
- # clojure-losangeles (9)
- # clojure-norway (54)
- # clojure-spec (5)
- # clojure-uk (4)
- # clojurescript (18)
- # cursive (14)
- # datomic (19)
- # deps-new (14)
- # emacs (8)
- # events (7)
- # fulcro (6)
- # graphql (3)
- # hyperfiddle (42)
- # instaparse (5)
- # lsp (10)
- # malli (21)
- # nbb (1)
- # off-topic (3)
- # pathom (3)
- # polylith (7)
- # reagent (14)
- # releases (2)
Hi,
Is there a built-in schema for Java list/collection validation?
Something like [:vector :map]
, but for java.util.Collection
?
I know I can create my own type with:
(m/-collection-schema {:type :java-collection, :pred #(instance? java.util.Collection %)})
But looking for alternativesno, there is nothing built-in. Same applies for concrete classes like Long
, Integer
. I you end up with a set of new schemas for java types, please share here.
I have a draft for effective types of schemas, so you derive generators, providers, transformers from effective type, :java-collection is kinda :vector
. without this, you need to mount each new type separately.
noob "what am I doing wrong?" style question regarding :fn
constraints and generation (contents in thread to not pollute channel):
I'm playing around, and trying to create a spec of what an entry in some kind of version log might look like, with a :fields-changed
(set), :before
(map), and :after
(map) key. I want to enforce a constraint that the keywords in the :fields-changed
set are a subset of the keys in the :before
and :after
maps.
My spec is as follows:
(mg/generate
[:and
{:gen/fmap (fn [{:keys [fields-changed]}]
{:fields-changed fields-changed
:after (zipmap fields-changed (repeatedly #(rand-int 100)))
:before (zipmap fields-changed (repeatedly #(rand-int 100)))})}
[:map
[:fields-changed [:set keyword?]]
[:before map?]
[:after map?]]
[:fn (fn [{:keys [fields-changed before after] :as m}]
(tap> m)
(and (set/subset? fields-changed (-> before keys set))
(set/subset? fields-changed (-> after keys set))))]])
I suspect my :gen/fmap
function is incorrect, and to tell the truth I'm not completely comfortable with generator stuff in general. I'm currently getting couldn't satisfy such-that
errors every other generate
call or so (it works when there's no keys generated)TIL about :gen/fmap
. We use that quite a lot in Spec at work, and me thinking it was missing from malli was one reason why I didn't think it would work for us -- so thank you 🙂
I can't tell what's wrong though...
yea, im not sure, zipmap
should be kosher to use with sets, so I don't think its that
my suspicion is that it might be checking the :fn
relation before the generation maybe? but that would be counter to my purpose of providing a custom generator (to side-step hard-to-generate things)
that tap>
reveals maps that are clearly all randomly generated, and not a result of the fmap
call; for example
{:fields-changed
#{:A./*?3+bY-L
:mn/+--+!**
:!*m/!e95:T
:sL:_/r
...
:_r3?/MKsW
:_8/S?-0T6},
:before
{-34238756986650630276294N 4,
true \;,
#uuid "485d16b8-7d0c-4f91-9d47-285b2f7921a2" :JQoO-:W,
...
"Vb*$&Qk7T" "I;qPI)"},
:after {true :Hg, 6 2}}
does the generator only 'attach' to the schema if its a :gen/gen
"property" (think I'm using the correct term there)?
@U0482NW9KL1 you should move the :gen/fmap
to the :map
schema instead, this way, it's done before the :fn
ahhh, so for reference, like so:
(mg/generate
[:and
[:map
{:gen/fmap
(fn [{:keys [fields-changed]}]
{:fields-changed fields-changed
:after (zipmap fields-changed (repeatedly #(rand-int 100)))
:before (zipmap fields-changed (repeatedly #(rand-int 100)))})}
[:fields-changed [:set keyword?]]
[:before map?]
[:after map?]]
[:fn (fn [{:keys [fields-changed before after] :as m}]
(tap> m)
(and (set/subset? fields-changed (-> before keys set))
(set/subset? fields-changed (-> after keys set))))]])