This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-06-01
Channels
- # announcements (20)
- # babashka (3)
- # beginners (30)
- # calva (28)
- # cider (3)
- # circleci (4)
- # clerk (27)
- # clj-kondo (72)
- # cljdoc (15)
- # cljs-dev (1)
- # clojure (85)
- # clojure-europe (37)
- # clojure-nl (1)
- # clojure-norway (13)
- # clojure-spec (7)
- # clojurescript (19)
- # clr (1)
- # conjure (11)
- # datahike (2)
- # datomic (11)
- # emacs (26)
- # events (4)
- # hoplon (35)
- # hyperfiddle (41)
- # jobs (7)
- # lsp (10)
- # nrepl (3)
- # off-topic (57)
- # portal (47)
- # practicalli (1)
- # rdf (3)
- # reitit (21)
- # releases (1)
- # testing (6)
- # tools-build (16)
- # wasm (1)
- # xtdb (16)
Hi, I noticed this behaviour that even if the keyword is not explicitly included in s/key, it is implicitly included:
(s/def :person/name string?)
(s/def :person/surname string?)
(s/def :person/email string?)
(s/def ::profile
(s/keys :req [:person/name
:person/surname]))
(comment
(s/explain ::profile {:person/name "Tom"
:person/surname "Wilkins"
:person/age 29})
; => Success!
(s/explain ::profile {:person/name "Tom"
:person/surname "Wilkins"
:person/email nil})
; => nil - failed: string? in: [:person/email] at: [:person/email] spec: :person/email
In the above example :person/email is not included in ::profile but the spec is complaining about it.
Can it be disabled?This is by design (see the docstring of s/keys)
Thank you @U064X3EF3, I read it. Can I compose a spec to validate a map in some other way so these namespaced spaces are not implicitly included?
The problem I'm facing is that I can't control the data I receive, and in the codebase I work with, there is lots of specs namespaced by category, here and there, often duplicated (another issue with overriding entries in registory). I'm trying to tackle these issues one by one, instead of making big changes in one go. One of these namespace specs conflicts with keys used in maps I try to validate.
In short, no, with s/keys. You can instead select-keys the data to just the keys you want to check before validating
Ok, I managed to find a workaround to this problem. What I did was writing a wrapper to wrap Malli schema under Spec protocol. That map validation is performed by Malli despite the instrumentation is executed via Spec api.
This way I managed to avoid s/keys behaviour.
The other issue I had in the same repo was that these subspecs (`:person/name`, :person/surname
, etc) were defined multiple times in different parts of the project resulting in overriding global registry. Thanks to that wrapper I also managed to avoid that problem.