metosin / malli

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

Fix :map-of :min and unreachable generator, explain such-that failures #1029

Closed frenchy64 closed 2 months ago

frenchy64 commented 3 months ago
  1. The :map-of generator should use (gen/vector-distinct-by first ..) in order to satisfy :min, otherwise it can generate less than the minimum number of entries by generating the same key multiple times with different vals.

  2. such-that and functions built on it accept a custom error message on generation failures. It's very helpful to have the precise schema that failed in the error message.

  3. The unreachable elements case for :map-of disallows generating the empty map if :max was non-zero. It's perfectly fine to generate empty regardless of :max. Other collection generators almost made this mistake by checking (<= (or min 0) 0 (or max 0)), which is technically correct but simplifiable to (= 0 (or min 0)).

  4. coll-distinct-gen nested generators incorrectly in the unreachable elements case. We want (-never-gen options), not (gen/fmap f (-never-gen options)) which defeats our simplification logic of checking for -never-gen's metadata.

ikitommi commented 2 months ago

Thanks! Calling the m/-exception doesn't currently enable the pretty printing, but will figure how to do that separate of this.