malli

dominicm 2025-06-26T13:29:33.848369Z

What's the preferred way to do generator overrides? e.g. I have a deeply nested multi in my schema and I'd like to force it to pick a particular branch for generation. To be clear, I mean from tests. Rather than on the schema itself.

2025-06-26T16:59:41.377979Z

There's no way to manipulate the generator without changing the schema. But there are plenty of ways to do the latter from tests if your registries are customizable.

2025-06-26T17:00:41.154379Z

It would be neat to be able to provide a custom generator for a ref or a path.

dominicm 2025-06-27T08:14:44.943129Z

Yes, that would be great. I think spec ends up getting this quite wrong, especially with multi, as the overrides don't end up working correctly.

dominicm 2025-06-27T08:15:00.339559Z

I wonder if it would be hard to do this in user land for malli

2025-06-27T17:36:21.973679Z

Try something like this:

(with-redefs-fn
  {#'malli.generator/-create
   (let [orig @#'malli.generator/-create]
     (fn [s o]
       (if (= ::schema-to-override (m/-ref s))
         custom-generator
         (orig s o)))}
  #(m/generate ...))

2025-06-27T17:49:23.043789Z

if your registry is a map you could also just update the options map: (update-in options [:registry ::schema-to-override] m/-update-properties assoc :gen/gen custom-generator)

2025-06-27T17:50:17.369779Z

Or use the right kind of registry that will locally shadow just one name from the global registry.

2025-06-27T17:52:32.098799Z

(m/generate schema {:registry (mr/composite-registry {::schema-to-override (m/-update-properties (m/deref (m/schema ::schema-to-override)) assoc :gen/gen custom-generator)}) m/default-registry)})})

2025-06-27T17:52:41.146499Z

both untested