This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-10-16
Channels
- # announcements (16)
- # asami (5)
- # aws (2)
- # babashka (4)
- # beginners (22)
- # calva (28)
- # cider (3)
- # clj-kondo (1)
- # cljdoc (13)
- # cljs-dev (16)
- # clojure (3)
- # clojure-australia (1)
- # clojure-europe (25)
- # clojure-gamedev (1)
- # clojure-germany (2)
- # fulcro (34)
- # helix (4)
- # jobs-discuss (16)
- # leiningen (10)
- # malli (20)
- # meander (7)
- # nrepl (35)
- # off-topic (1)
- # portal (13)
- # ring-swagger (3)
- # shadow-cljs (34)
- # tools-deps (7)
- # vim (1)
Hi, is it possible to restrict the range of values for integer predicates produced by the generator ?
For example (malli.generator/generate [:int {:min 0 :max 6}])
reduces the range to [0,6], but (malli.generator/generate [int? {:min 0 :max 6}])
, where int?
is an integer predicate, does not. My actual use case is with pos-int?
. Thanks
pos-int? is equivalent to [:int {:min 1}] Type schemas are more flexible than predicate schemas
Excellent thanks! This is what I was looking for [:int {:min 0 :gen/max 10}]
; i.e. eq to nat-int? for validation and [0,10] for the generated range
Where types are concerned, I prefer the type schemas (with keywords) and not the predicate schemas
When transforming multi to json schema, how about using implications? https://json-schema.org/understanding-json-schema/reference/conditionals.html#id7
Is there a generator option to produce distinct values for a vector? for example the following schema generated some values more than once
(->> [:vector {:min 400} string?]
malli.generator/generate
frequencies
(filter (fn [[_ v]] (> v 1))))
;; => (["" 20] ["e" 2] ["7" 2])
ThanksGreat thanks! Although this has been a little bit of a mouthful it has done the trick:
[:vector {:min 400
:gen/schema [:set {:min 10} string?]
:gen/fmap vec}
string?]
(perhaps there should be a :gen/distinct
option for collections? clojure.test.check.generators
does seem to provide list-distinct
and`vector-distinct` to this end)
This unfortunately does not scale very well, because I'd need to repeat the vector predicate twice, one for :vector
the other for the generator's schema, which could easily get out of sync for more complicated schemas, e.g.
[:vector {:min 400
:gen/schema [:set {:min 10} [:map [:x int?] [:y string?]]]
:gen/fmap vec
}
[:map [:x int?] [:y string?]]]
At that point you might want to define your own type and use a registry, you could call it distinct vector. Why does it need to be a vector, btw? Why can't it be. a set?
Hi, it has to be a vector because this is how values are coming out from the data source. The distinct values are only needed by the generator to produce some specific values during testing only.
I can't seem to create a custom type with -simple-schema
for distinct-vector ... The syntax of the type looks to be in accordance with the section on the readme file, but it gives me a :malli.core/invalid-schema {:schema (#object[cljs$core$string_QMARK_])}
error when I try to use it ...
(let [vector-distinct
(malli.core/-simple-schema
(fn [_ schema]
(let [schema (first schema)])
{:type :vector-distinct
:pred vector?
:type-properties
{:gen/gen (clojure.test.check.generators/vector-distinct (malli.generator/generator schema))}}))]
(-> [vector-distinct {} string?]
malli.generator/generate))
Thanks!sorry, not sure what you mean by "a variation on vector-of"; but I think I got it this time working, It appears I was missing the number of arguments accepted by the new type, in my case 1 (as in :min
and :max
):
(let [vector-distinct
(malli.core/-simple-schema
(fn [_ schema]
(let [schema (first schema)]
{:type ::vector-distinct
:min 1
:max 1
:pred vector?
:type-properties
{:gen/gen (clojure.test.check.generators/vector-distinct (malli.generator/generator schema))}})))]
(->> [vector-distinct string?]
malli.generator/generate))
Does this look sane or is it just too hacky?Actually that is not complete because pred
is not going to check the vector values for conformity. I will stick with the following hack which almost does what I want (the only glitch is that generated distinct values are likely to be less than :min
):
[:vector {:min 400 :max 400
:gen/fmap #(into [] (set %))} string?]