Fork me on GitHub
#malli
<
2022-03-02
>
DenisMc13:03:03

Hi, I would like to disallow null bytes in :string fields in my malli schema because Postgres can’t handle them. What is the cleanest way to add this sort of validation? Thanks in advance.

DenisMc13:03:30

Just looking at this further: I’m running into this null byte generator issue because I need some of my strings to be non-empty, so I’m setting :min (and :max) parameters in Malli, like so:

[:my-param [:string {:min 1 :max 5}]]
I see that the malli.generator/-string-gen code looks like this:
(defn- -string-gen [schema options]
  (let [{:keys [min max]} (-min-max schema options)]
    (cond
      (and min (= min max)) (gen/fmap str/join (gen/vector gen/char min))
      (and min max) (gen/fmap str/join (gen/vector gen/char min max))
      min (gen/fmap str/join (gen/vector gen/char min (* 2 min)))
      max (gen/fmap str/join (gen/vector gen/char 0 max))
      :else gen/string-alphanumeric)))
..so the generator generates alphanumeric strings if the min or max length are not set, but generates random characters (including non-alphanumeric characters) if the length is set. I’m wondering what the logic is for this? Why does the string length determine what sort of characters are included in the randomly generated strings?

2