Fork me on GitHub
#malli
<
2021-04-19
>
Hugh Powell06:04:49

I'm trying to create a schema for a CSV data structure with defined, case insensitive headers. It must also be able to generate a legal data structure. I'm using the same pattern as clojure.data.csv, a vector of vectors with the first vector representing the headers and the remaining vectors representing the rows. I can create a schema for the headers that checks validity fairly simply

(def headers
  [:cat
    [:re "(?i)header1"]
    [:re "(?i)header2"]
    ...
    [:re "(?i)headerN"]])
But test.chunk doesn't handle flags (the (?i) bit), so I don't have a generator. My next guess is to create a schema that can match a given string case-insensitively and then a generator that will generate a randomly cased version of that string. I can write these two functions, but can't work out how to create a schema with them. Any thoughts or docs you can point me to?

ikitommi07:04:53

@hugh336 maybe a custom generator? [:re {:gen/gen gen/alphanumric} "(?i)header1"]

Hugh Powell09:04:12

Aha! Awesome, thanks for the quick reply :thumbsup:

ikitommi09:04:01

here a {:gen/elements ["header1"]} would be good, just data and always correct.

bartuka14:04:22

@ikitommi I also have several malli schemas that need to verify for [:fn (complement str/blank)] and I saw an open discussion from some years ago in Malli about adding validation of stripped strings to [:string] base schema

bartuka14:04:30

is this still desired?

bartuka14:04:04

not some years.. last year, sorry. thought was 2019 https://github.com/metosin/malli/pull/205

ikitommi14:04:18

is the [:string {:min 1}] bad?

ikitommi14:04:59

maybe (def NonEmptyString [:string {:min 1}]) liike we would do in Schema?

ikitommi14:04:12

or:

[:map {:registry {::non-blank-string [:string {:min 1}]}}
 [:name ::non-blank-string]
 [:address [:map [:street ::non-blank-string]]]]

bartuka15:04:11

(m/validate [:string {:min 1}] " ") ;; => true but I expect false if no blank is allowed

bartuka15:04:10

I like the idea of using {:trim true :min 1 :max 99} .. looks very clean and we dont need to add custom registries