This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-06-09
Channels
- # beginners (121)
- # boot (28)
- # cider (106)
- # clara (57)
- # cljs-dev (70)
- # cljsrn (6)
- # clojure (145)
- # clojure-dev (7)
- # clojure-italy (36)
- # clojure-russia (42)
- # clojure-spec (26)
- # clojure-uk (127)
- # clojurescript (103)
- # core-async (10)
- # cursive (56)
- # datascript (66)
- # datomic (16)
- # defnpodcast (1)
- # emacs (18)
- # events (6)
- # figwheel (1)
- # jobs (1)
- # luminus (1)
- # lumo (44)
- # off-topic (58)
- # om (17)
- # onyx (2)
- # parinfer (75)
- # pedestal (4)
- # re-frame (18)
- # ring (4)
- # ring-swagger (8)
- # rum (7)
- # spacemacs (7)
- # specter (2)
- # sql (4)
- # unrepl (39)
- # untangled (17)
- # vim (3)
- # yada (21)
currently no, although I think that would be a good enhancement
thanks @alexmiller
@linuss line 7 - should be s/and ?
and not a function?
Hey all, I haven been having problems trying to write a spec as follows. I have a map that looks like this
{
:key1 "val1"
:key2 ""
:key3 34
:x-somestring "some string"
:x-someotherstring "string as well"
}
In essence the map is a mix of well know keys(:key1, :key2, :key3) and and optional
set of patterned keys (:x-…). I can easily model either part: (s/keys for the first part and s/map-of for the second part) but I am having a hard time expressing both at the same time. Any ideas on how I can do it?this is sometimes called a “hybrid map”
you can’t use s/map-of
but you can use an s/merge of s/keys and an s/every-kv that specs the map entry tuples
there is a slightly more complicated example outlined in this blog: http://blog.cognitect.com/blog/2017/1/3/spec-destructuring
search for “hybrid” in there to find that part
@pithyless hi, and thanks for the report - there was a bug in explain, should be fixed in [metosin/spec-tools "0.2.1-SNAPSHOT"]
(which now uses the latest clj, cljs & spec):
(require '[spec-tools.core :as st])
(require '[spec-tools.data-spec :as ds])
(st/explain-data (ds/spec ::foo {:foo string?}) {:foo 42})
; ::s{:problems ({:path [:foo]
; :pred clojure.core/string?
; :val 42
; :via [:user$foo/foo]
; :in [:foo]})
; :spec #Spec{:form (clojure.spec.alpha/keys :req-un [:user$foo/foo])
; :type :map
; :keys #{:foo}}
; :value {:foo 42}}
@ikitommi - Perfect! I can confirm it fixes my issue. Thanks for the lightning-fast response and apologies for not going through GH issues. I figured I was just doing something wrong 🙂
I'd like to pass parameters into a predicate function along with the value being checked ... a simple example might be a predicate to validate a integer with a min and max value... i.e. validate x is between 1-10. What are common design patterns for this? First thought is to write a function that returns a closure ... any real-world, slick examples of such a thing?
@don.dwoske there's already some builtins like int-in-range?
@ghadi - thanks for the example, that's what I was thinking - write macros or functions which return specs or predicates. https://github.com/clojure/clojure/blob/d920ada9fab7e9b8342d28d8295a600a814c1d8a/src/clj/clojure/spec.clj#L1623
@don.dwoske yup. fyi all of clojure.spec is being tracked in a different repository now.
Ah - of course. In my defense, whenever I search for docs, I end up here : https://clojure.github.io/clojure/branch-master/clojure.spec-api.html and when clicking on "source" for a function.. I end up where I linked to.