Fork me on GitHub
#malli
<
2022-11-02
>
dumrat08:11:49

Is there a way to use a schema which has few maybe schemas but consider maybe not be there for validation? Or do I have to strip the maybes from schema and do validation manually? On mobile, I can give code sample later if needed.

ikitommi09:11:37

please share an example

dumrat03:11:29

ok case is like this. 1. I've got a few endpoints (around 20) returning large sets of xml. These are quite old services that we don't have control over. 2. I parse xml, then do some transformations on them 3. I have malli schemas for the transformed data. I coerce the data using schemas. Then I validate using same schemas. 4. Data from the services are often incomplete (nil values where they shouldn't be mostly). So I add [:maybe ...] for some fields to sort of make the validations work. 5. Now, I'd like to still know the cases where validations fail without the [:maybe ...] wrappings so I can have a report of missing stuff. Is it possible to do this or do I have to transform schema myself and remove the [:maybe ...] wrappings? sample:

(def counterparties-schema
  [:vector
   [:map
    [:address [:maybe :string]]
    [:baseNumber :int]
    [:contactName [:maybe :string]]
    [:contactPhone [:maybe :string]]
    [:cptyType [:maybe :string]]
    [:enabledForSettlement :boolean]
    [:id :int]
    [:legalVehicleId :int]
    [:mnemonic :string]
    [:name [:maybe :string]]]])

;; Validation with this succeeds
(m/validate counterparties-schema data)
=> true

;; But I'd still like to know fields where validation would have failed if [:maybe ...] wrappings weren't there.
;; i.e [:address :string] etc.

;; Manually something like this:
(defn strict [schema]
  (clojure.walk/postwalk 
   (fn [v] 
     (if (and (vector? v) (= (first v) :maybe))
       (second v) v)) 
   schema))

;; So I can still know the missing values
(m/validate (strict counterparties-schema) data)
What I was asking first is that is this kind of thing supported by malli itself? Perhaps that's a dumb question. Is this a common use case though? Or am I thinking about this wrong?

mauricio.szabo17:11:15

A question about decompleted map keys and values: if I register, for example, that :user/id is always an [:string {:min 1}], is there a way to query Malli for :user/id and know the result is [:string {:min 1}]? Or even if it's just a :string?

ikitommi18:11:59

(m/schema :user/id) returns the ref, you can deref that with m/deref (or m/deref-all)

mauricio.szabo21:11:57

Thanks, I had to do one step further to be useful:

(m/ast (m/deref-all (m/schema :user/id)))
That helped 🙂