This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-08-30
Channels
- # announcements (3)
- # asami (20)
- # babashka (15)
- # beginners (47)
- # biff (25)
- # calva (11)
- # catalyst (2)
- # cider (11)
- # clojure (24)
- # clojure-brasil (3)
- # clojure-europe (21)
- # clojure-norway (34)
- # clojure-uk (2)
- # clojurescript (9)
- # clr (2)
- # datomic (10)
- # fulcro (14)
- # hyperfiddle (58)
- # introduce-yourself (1)
- # jobs (3)
- # malli (5)
- # meander (6)
- # missionary (4)
- # nbb (30)
- # off-topic (6)
- # podcasts-discuss (1)
- # shadow-cljs (13)
- # slack-help (5)
- # tools-build (4)
- # vim (20)
- # xtdb (20)
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?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
2
also given both entries are declared :optional true
, the invalid-entity
might not be invalid
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
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)
?
2