malli

https://github.com/metosin/malli :malli:
Hugh Powell 2021-04-19T06:57:49.462100Z

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?

ikitommi 2021-04-19T07:49:53.462900Z

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

Hugh Powell 2021-04-19T09:02:12.464Z

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

ikitommi 2021-04-19T09:22:01.464200Z

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

ikitommi 2021-04-19T09:22:39.464400Z

https://github.com/metosin/malli#value-generation

👍 1
bartuka 2021-04-19T14:31:22.465900Z

@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

bartuka 2021-04-19T14:31:30.466Z

is this still desired?

bartuka 2021-04-19T14:32:04.466200Z

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

ikitommi 2021-04-19T14:40:18.466500Z

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

ikitommi 2021-04-19T14:40:59.466700Z

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

ikitommi 2021-04-19T14:42:12.466900Z

or:

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

bartuka 2021-04-19T15:02:11.467100Z

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

bartuka 2021-04-19T15:03:10.467300Z

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