Fork me on GitHub
#malli
<
2023-03-23
>
niwinz14:03:52

After working a bit with creating custom schemas, I think that would be useful if :gen/gen value can be a delay or function, for initialize the generator lazily, only when it is used. What do you think?

2
ikitommi16:03:19

that’s a great idea! with function doing requiring-resolve it would really lean.

ikitommi16:03:43

(just importing test.check takes some time)

niwinz19:03:47

that is not that I have in mind but it also applies

niwinz19:03:14

with lazy I mean having the ability to defer the creting the generator instance when it is requested

niwinz19:03:23

Something like this that I have in mind, provide a function that returns the generator, when it is requested, also there is the option to use clojure.core/delay, it caches the value on second use

niwinz19:03:24

but having the ability to pass a symbol and resolve it once is needed it also a nice to have

niwinz19:03:58

what do you think about the "delay" or function ? if it is ok, I can try to make draft PR for it

ikitommi06:03:40

I think function would be a good start.

ikitommi06:03:38

there is m/eval to for optionally plugging into sci, no-op for normal functions

ikitommi06:03:55

this also works btw:

(defrecord My [])

(mr/set-default-registry!
 (merge
  (m/default-schemas)
  {::my (m/-simple-schema
         {:pred any?
          :type ::my
          :type-properties {:gen/schema ::my-map
                            :gen/fmap map->My}})
   ::my-map [:map [:x :int] [:y :int]]}))

(mg/sample ::my)
;(#{:x 0, :y 0}
; #{:x -1, :y 0}
; #{:x 0, :y 0}
; #{:x 0, :y 0}
; #{:x 0, :y 1}
; #{:x 7, :y 0}
; #{:x -6, :y -1}
; #{:x 2, :y -7}
; #{:x -2, :y -4}
; #{:x 36, :y -1})

cap10morgan16:03:29

What's the correct way to use mu/update-properties on a schema in a registry? When I try just merging the return value back into the registry under the same key, I get invalid-schema exceptions.

cap10morgan16:03:58

for example:

(-> registry
    (update ::fqh/commit mu/update-properties assoc :encode/edn
            (partial encode-commit-edn parsed-context))
    (update ::expanded-iri mu/update-properties assoc :encode/edn
            #(json-ld/expand-iri % parsed-context))))

ikitommi19:03:10

if you are constructing the registry and try to reference schemas that are not yet registered, it’s an error.

ikitommi19:03:28

you can only reference things already in the registry.

ikitommi19:03:05

I would use https://github.com/metosin/malli#mutable-registry and add the schemas imperative one-by-one. Just like with spec.

cap10morgan13:04:35

The schemas are already in the registry. I'm just trying to update their properties. But the returned value from mu/update-properties is invalid.

cap10morgan13:04:57

but perhaps a mutable registry is still the right answer there