Fork me on GitHub
#malli
<
2024-03-18
>
ikitommi06:03:36

there are many new PRs and Issues I haven’t had time to check. Have had busy few weeks, taking friday off to go through ’em and work on my wip-malli things

👍 10
Marius11:03:30

Hi all! I’m pondering about how/whether to pass external parameters to a custom Fn schemas. Something like “current date” (which is not necessarily the real date). It seems I would have to add it to the data to be validated, although from a logical point it does not belong there. Any ideas?

Ed13:03:27

Not sure if malli supports this directly, but you could use dynamic scope to work around that? See https://clojure.org/reference/vars

Marius13:03:50

That would work, but unfortunately the environment is not the same for all validations

Ed15:03:05

not sure what you mean by "environment", but dynamic scope should let you control it at the call-site like an extra parameter to your validation function.

(require '[malli.core :as m])

  (def ^:dynamic *size-limit-factory* (fn [] 10))
  (def too-big? #(< (*size-limit-factory*) %))
  (def my-schema [:fn (fn [x] (not (too-big? x)))])

  (m/validate my-schema 8) ;; => true
  (m/validate my-schema 12) ;; => false

  (binding [*size-limit-factory* (fn [] 100)]
    [(m/validate my-schema 80)
     (m/validate my-schema 120)]) ;; => [true false]
otherwise, you'll need to detect the env inside the validation function?

Marius15:03:02

Ah ok, I didn’t know that it works like that. My assumption was that the var will be a global var. Thanks for the explanation!

Marius15:03:31

If there was a button to upvote or give kudos, you’d earned it 🙂

Ed15:03:40

😁 ... just be careful, dynamic scope is generally considered bad api design because it often breaks the principal of least surprise. It's a hidden argument to every function that uses it.

Marius15:03:06

will do. thanks for the heads up

👍 1