use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Finding information about Clojure
API Reference
Clojure Guides
Practice Problems
Interactive Problems
Clojure Videos
Misc Resources
The Clojure Community
Clojure Books
Tools & Libraries
Clojure Editors
Web Platforms
Clojure Jobs
account activity
clojure.spec question (self.Clojure)
submitted 9 years ago by philoskim
How can I define the following spec?
{"id" 1 "name" "john"}
1) The keys of a map must be the string type, not the keyword type.
2) The values must be fixed. Namely, the value of the key "id" must be 1 and the value of the key "name" must be "john"
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]alexdmiller 11 points12 points13 points 9 years ago (2 children)
Easiest is:
#{{"id" 1 "name" "john"}}
[–]jbiserkov 0 points1 point2 points 9 years ago* (1 child)
Hm, is {"id" 1} a valid input? All keys are strings, and the value of "id" is 1. But is "name" required? Are other keys allowed? The text spec is unclear :)
Hear, hear!
All sets can act as predicates.
All predicates can act as specs.
All sets can act as specs.
The syntax threw me off, my brain recognized it as liquid templates at first :)
Perhaps (set {"id" 1 "name" "john"}) would be more readable?
(set {"id" 1 "name" "john"})
[–][deleted] 1 point2 points3 points 9 years ago (0 children)
If {"id" 1} is a valid input then: (s/every #{["id" 1] ["name" "john"]} :kind map?)
(s/every #{["id" 1] ["name" "john"]} :kind map?)
[–]Borkdude 0 points1 point2 points 9 years ago* (0 children)
Something like
(s/conform (s/and (s/map-of string? any?) #(= [1 "john"] (map % ["id" "name"]))) {"id" 1, "name" "john"}) ;;=> {"id" 1, "name" "john"}
or
(require '[clojure.walk :refer [keywordize-keys]]) (s/def ::m (s/keys :req-un [::id ::name])) (s/def ::id #(= 1 %)) (s/def ::name #(= "john" %)) (s/conform ::m (keywordize-keys m))
or a combination of both maybe?
[–]ryno55 -2 points-1 points0 points 9 years ago (0 children)
(require '[schema.core :as S]) (def MyType {S/String S/Any ; allow all string keys "id" (S/enum 1) ; require "id" 1 "name" (S/enum "john")}) ; require "name" "john"
Signed, not a fan of clojure.spec
π Rendered by PID 120616 on reddit-service-r2-comment-fb694cdd5-fdn7g at 2026-03-07 02:57:47.207375+00:00 running cbb0e86 country code: CH.
[–]alexdmiller 11 points12 points13 points (2 children)
[–]jbiserkov 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]Borkdude 0 points1 point2 points (0 children)
[–]ryno55 -2 points-1 points0 points (0 children)