Fork me on GitHub
#malli
<
2023-08-30
>
Chip20:08:57

Just picked up malli yesterday (and Clojure a few weeks ago) so I’m a superdork. I’m doing something stupid here. I want to make sure I don’t have rogue cognito-ids hanging off non-people.

(ns life.db.schema.validators
  (:require
   [malli.core :as m]
   ))

(def life-with-user-schema
  [:map
   [:life/uuid {:type :uuid, :optional true}]
   [:system/cognito-id {:type :string, :optional true}]])

(def valid-entity
  {:life/uuid (java.util.UUID/randomUUID)
   :system/cognito-id "some-cognito-id"})

(def invalid-entity
  {:system/cognito-id "some-cognito-id"})

(println (m/validate life-with-user-schema valid-entity))  ; Should return true
(println (m/validate life-with-user-schema invalid-entity)) ; Should return false
I get this error for both
; Execution error (ExceptionInfo) at malli.core/-exception (core.cljc:138).
; :malli.core/invalid-schema
I obviously don’t know what I’m doing. Suggestions?

radhika20:08:08

you might want to rewrite the map entry definitions like [:life/uuid {:optional true} :uuid] so that the last item describes the entry's value

gratitude-thank-you 2
radhika20:08:42

also given both entries are declared :optional true, the invalid-entity might not be invalid

Chip20:08:18

I needed both of those suggestions. Works now. Here are the corrections FWIW

(ns life.db.schema.validators
  (:require
   [malli.core :as m]
   ))

(def life-with-user-schema
  [:map
   [:life/uuid {:optional false} :uuid]
   [:system/cognito-id {:optional true} :string]])
(def valid-entity
  {:life/uuid (java.util.UUID/randomUUID)
   :system/cognito-id "some-cognito-id"})

(def invalid-entity
  {:system/cognito-id "some-cognito-id"})

(println (m/validate life-with-user-schema valid-entity))  ; Should return true
(println (m/validate life-with-user-schema invalid-entity))
Thanks again.

🙌 4
Stig Brautaset09:08:54

While not related to your question, did you know that in Clojure 1.11 and up you can replace (java.util.UUID/randomUUID) with (random-uuid) ?

gratitude-thank-you 2