This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-02-07
Channels
- # aleph (3)
- # aws (7)
- # beginners (117)
- # boot (119)
- # cider (2)
- # cljs-dev (3)
- # clojure (193)
- # clojure-austin (1)
- # clojure-dusseldorf (4)
- # clojure-finland (5)
- # clojure-france (5)
- # clojure-italy (7)
- # clojure-portugal (1)
- # clojure-russia (204)
- # clojure-serbia (5)
- # clojure-spec (31)
- # clojure-uk (64)
- # clojurescript (288)
- # community-development (9)
- # core-async (54)
- # cursive (8)
- # datascript (18)
- # datomic (26)
- # dirac (8)
- # emacs (26)
- # figwheel (1)
- # hoplon (16)
- # jobs (2)
- # jobs-discuss (4)
- # juxt (1)
- # lein-figwheel (4)
- # leiningen (14)
- # london-clojurians (2)
- # lumo (17)
- # off-topic (44)
- # om (63)
- # om-next (2)
- # onyx (26)
- # perun (14)
- # planck (5)
- # portland-or (34)
- # proton (2)
- # protorepl (8)
- # quil (1)
- # re-frame (6)
- # reagent (16)
- # remote-jobs (4)
- # ring (7)
- # ring-swagger (10)
- # rum (1)
- # untangled (2)
Is it possible, given some data, a keyword for its spec and a keyword for a simple spec that is a part of the data spec, extract all values conforming to the simple spec?
E.g. I have a map id->val and three specs - for id, for val and for the map itself. And I'd like to extract all vals from the map.
so not all of the map entries in this map conform to the map spec, but you want to find the ones that do?
how to specify a map that may be recursive?
actually, any map spec is recursive, since a map spec allows keys not specified in the spec
but for clarity's sake, you may want to put the spec name in the :opt
portion of the map spec
it’s recursive perhaps, but not useful if it doesn’t validate anything about the map, is it?
user=> (s/def ::foo (s/or :a number? :b ::bar))
:user/foo
user=> (s/def ::bar :req [::foo])
ArityException Wrong number of args (3) passed to: spec/def clojure.lang.Compiler.macroexpand1 (Compiler.java:6832)
user=> (s/def ::bar (s/keys :req [::foo]))
:user/bar
user=> (s/valid? ::bar {::foo 1})
true
user=> (s/valid? ::bar {::foo {::foo 1}})
true
user=> (s/valid? ::bar {::foo {::foo :a}})
false
user=>
as it is, I have
(s/or (s/map-of string? keyword?) (s/map-of string? map?))
but that is only recursing one level@hiredman thanks, that looks like it
oh hrm maybe not with the map I have here
user=> (require '[clojure.spec :as s])
nil
user=> (s/def ::foo (s/map-of string? (s/or :value keyword? :rec ::foo)))
:user/foo
user=> (s/valid? ::foo {"a" :a})
true
user=> (s/valid? ::foo {"a" 1})
false
user=> (s/valid? ::foo {"a" {"b" :b}})
true
user=>
haha, on cue
(s/def ::a string?)
(s/def ::mymap (s/keys :req [::a]))
(s/valid? ::mymap {::a "abc" ::mymap {::a "def" ::mymap {::a "xyz"}}})
that works if your keys are namespaced keywords
but I have string keys here
but, I’m filing all of this away for the time I do need such a construction…thanks @joshjones and @hiredman , very helpful
How do I generate map with keys and values that correspond to a given (s/keys)
spec?
let's say I have (s/keys :req-un [::id ::name] :opt-un [::money])
based on that I should generate vector like:
[{:id 1 :name "Greg" :money 0.02} {:id 2 :name “Alice”}]
upd: I actually want the keys to be randomly selected out of that spec, not precisely the same keys@ag https://clojure.github.io/clojure/branch-master/clojure.spec-api.html#clojure.spec/exercise
it depends sort of on what purpose you are generating them for, but if you generally just want to poke at them, exercise is a way to generate data for a give spec, s/keys created or otherwise