Hi, I tried this in repl: https://github.com/metosin/malli?tab=readme-ov-file#content-dependent-simple-schema
(def Between
(m/-simple-schema
{:type `Between
:compile (fn [_properties [min max] _options]
(when-not (and (int? min) (int? max))
(m/-fail! ::invalid-children {:min min, :max max}))
{:pred #(and (int? %) (>= min % max))
:min 2 ;; at least 1 child
:max 2 ;; at most 1 child
:type-properties {:error/fn (fn [error _] (str "should be between " min " and " max ", was " (:value error)))
:decode/string mt/-string->long
:json-schema {:type "integer"
:format "int64"
:minimum min
:maximum max}
:gen/gen (gen/large-integer* {:min (inc min), :max max})}})}))
Is there an error in the example, I can't make values between 10 and 20 (e.g. 12) go through the validation? The result is always like ["should be between 10 and 20, was 12"] 🤔should probably be (<= min % max)
Yes! Thank you 🙂
I'll fix the readme 🙂
Another question about content based schema rules: Has anyone used them in a project?
https://clojurians.slack.com/archives/C03S1KBA2/p1731428215959609
@ambrosebs is working on Malli Constraints - https://www.clojuriststogether.org/news/sept.-and-oct.-2024-short-term-project-updates/#malli-ambrose-bonnaire-sergeant
would that help here?
Well, I think I have an approach that will work, by copying the implementation of -and-schema and then adding support for an :extended-pred property. Not an succinct or elegant solution, but I think it will suffice.
good to hear if that works for you. All real life pain scenarios (and workarounds to solve them) most welcome.
Hi All, does Malli support normalizing values like this?
(def MyThing
[:orn
[:simple-form :keyword]
[:normal-form [:map [:attr :keyword]]]])
(normalize MyThing {:attr :foo}) ;; => {:attr :foo} (no change, already in normal form)
(normalize MyThing :foo) ;; => {:attr :foo} (convert to normal form)you could (ab)use a custom decoder for MyThing for that
Thanks for the tip. The docs say decoding is "a process of transforming (invalid) values into potentially valid ones" - I guess that's why this would be abusing decoders, since the input value is valid, just not in the preferred shape. I'm a bit surprised this isn't a more common need. Are people transforming to the preferred shape outside of Malli, after validation?
yeah, I guess people would have a separate, perhaps implicit, normalisation phase
one way to think of it is to have two schemas: the more permissive one for your API, and then the normalized one for your internals. In that case decoding would be exactly the right thing to do.