metosin / malli

High-performance data-driven data specification library for Clojure/Script.
Eclipse Public License 2.0
1.44k stars 204 forks source link

Is it possible to represent an increasing sequence? #1014

Closed AWinterman closed 3 months ago

AWinterman commented 4 months ago

I'm wondering if I can represent a sequence of maps with a non-decreasing value.

For example:

[{:gen 0} {:gen 0} {:gen 1} {:gen 2}] ; valid
[{:gen 0} {:gen 1} {:gen 0}]              ; invalid

I can't quite see how i'd do this except through a :fn field in the parent schema. Is that the way?

ikitommi commented 3 months ago

you can compose the sequence with :and + :fn like:

(mg/sample [:and [:vector {:gen/min 4, :gen/max 4} [:int {:min 0}]] [:fn (partial apply <)]])
;([1 1919 1267759024631 2123746214464]
; [1647168068 21396962994 1791026298764950 12656959539819441]
; [6 29 47 56]
; [705 1072 11424 3783976]
; [348 7180 48608 625266]
; [1 3 49 710]
; [0 1 2 27]
; [2 32 37 457]
; [3053 119063 324145657 529195926540]
; [1 2308 2561 3373])