malli

Leena Hyvönen 2024-11-12T09:38:13.073179Z

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"] 🤔

opqdonut 2024-11-12T09:59:33.625669Z

should probably be (<= min % max)

Leena Hyvönen 2024-11-12T11:27:52.187059Z

Yes! Thank you 🙂

opqdonut 2024-11-12T11:49:18.283219Z

I'll fix the readme 🙂

Leena Hyvönen 2024-11-12T13:02:04.899839Z

Another question about content based schema rules: Has anyone used them in a project?

ikitommi 2024-11-12T18:28:18.589359Z

would that help here?

chouser 2024-11-12T19:56:36.395509Z

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.

ikitommi 2024-11-13T09:54:17.072479Z

good to hear if that works for you. All real life pain scenarios (and workarounds to solve them) most welcome.

2024-11-12T21:40:13.676679Z

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)

opqdonut 2024-11-13T06:02:08.951499Z

you could (ab)use a custom decoder for MyThing for that

2024-11-13T15:02:39.170849Z

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?

opqdonut 2024-11-14T05:50:39.422079Z

yeah, I guess people would have a separate, perhaps implicit, normalisation phase

opqdonut 2024-11-14T05:51:49.723049Z

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.

👍 1