This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-03-11
Channels
- # aleph (50)
- # announcements (20)
- # babashka (68)
- # beginners (70)
- # calva (25)
- # cider (1)
- # clj-kondo (10)
- # cljs-dev (3)
- # clojars (1)
- # clojure (113)
- # clojure-australia (7)
- # clojure-berlin (4)
- # clojure-europe (47)
- # clojure-italy (14)
- # clojure-nl (2)
- # clojure-norway (1)
- # clojure-serbia (5)
- # clojure-spec (11)
- # clojure-uk (8)
- # clojurescript (16)
- # community-development (2)
- # conjure (2)
- # cursive (15)
- # data-oriented-programming (35)
- # datahike (12)
- # datascript (5)
- # datomic (13)
- # duct (7)
- # fulcro (21)
- # graalvm (94)
- # graphql (1)
- # helix (4)
- # honeysql (19)
- # jackdaw (8)
- # jobs (2)
- # jobs-rus (1)
- # leiningen (1)
- # malli (32)
- # missionary (1)
- # mount (1)
- # off-topic (40)
- # perun (2)
- # portal (7)
- # reitit (10)
- # rewrite-clj (26)
- # shadow-cljs (90)
- # spacemacs (29)
- # sql (17)
- # tools-deps (49)
- # wasm (1)
- # xtdb (26)
Quick question before I resort to rewrite my code: Is there something inherently wrong with trying to keep code DRY by extracting values directly from a spec like this?
(ns my.specs ;; cljc-file
(:require [clojure.spec.alpha :as s]))
(s/def :select/legal-values #{1 2 3 4})
(ns my.ui ;; cljs-file
(:require [clojure.spec.alpha :as s]
[my.specs]))
(def dropdown-config [{:id :select-legal-values
:tooltip "Select your value"
:values (s/form :select/legal-values)},,,])
This only illustrates the part where I retrieve values from this spec. Data verification is elsewhere. What makes me ask is that I’ve had this working in my Luminus+Shadow-cljs+re-frame app, but as soon as I upgrade shadow-cljs, my project won’t load in the browser. From e.g. “ReferenceError: spec is not defined” I get the impression that the spec might not be ready yet. I don’t think it is shadow-cljs that is to blame as I encountered the same issue when upgrading yesterday as well, but after cleaning and trying a few times it suddenly started working. Maybe I got lucky with a race condition or something…
Anyone?Yeah, this won’t work in spec
Oh wait, I’m misreading
Might be a bug in spec emitting the wrong namespace on the spec form? We have fixed a few things like that
I’m keying off of “spec” there - not sure if that’s a hard coded word or a ns from data
Based on the error I don’t get the impression that there is a wrong namespace issue, but it’s not that informative really.
It’s often a bit hard to reduce the actual code down to a suitable snippet that captures to issue. In this case I would say the gist is: Can a spec from a required namespace be used in a normal def that calls s/form to extract the “value” from that spec. Is the spec ready for value extraction at the time of def evaluation?
Even though I had this working I’ll just rewrite it as it seems a bit brittle. For instance IIRC I had to put the set directly inside the s/def, as s/form did not work if the spec used a var containing the set like this:
(def my-set #{1 2 3}
(s/def :foo/bar my-set)
(def boom (s/form :foo/bar)
I guess I might be abusing spec a bit, or relying on functionality that is not suitable for that purpose.
A quick rewrite where both the spec and my config refers to the same vars/defs resolved the issue. Still DRY, a few more lines of code, less “clever”, but maybe easier to understand and less brittle. 🙂
some of the fiddliness of this is improved in spec 2
Sounds great. I was holding back on spec for a long time waiting for spec 2 😉 But, I understand and appreciate the attention to detail that goes into the process.
@vlaaad I've looked for this previously (and for the reverse transformation) but came up empty. luposlip/json-schema
is the library I used for validation, and it seems to have sprouted schema inference since I last looked through its docs, that may not be useful in its exact form but could have some useful code within