clojure-spec

About: http://clojure.org/about/spec Guide: http://clojure.org/guides/spec API: https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html
mokr 2021-03-11T14:16:08.009700Z

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?

alexmiller 2021-03-11T14:22:45.010200Z

Yeah, this won’t work in spec

alexmiller 2021-03-11T14:23:18.010500Z

Oh wait, I’m misreading

alexmiller 2021-03-11T14:24:49.011500Z

Might be a bug in spec emitting the wrong namespace on the spec form? We have fixed a few things like that

alexmiller 2021-03-11T14:26:11.012700Z

I’m keying off of “spec” there - not sure if that’s a hard coded word or a ns from data

mokr 2021-03-11T15:15:26.023100Z

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.

mokr 2021-03-11T15:21:30.024900Z

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. 🙂

alexmiller 2021-03-11T15:41:29.025400Z

some of the fiddliness of this is improved in spec 2

mokr 2021-03-11T17:25:42.026500Z

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 2021-03-11T19:43:27.026600Z

Anyone knows if there is a library to convert json schema to spec definitions?

robertfw 2021-03-11T20:02:51.028700Z

@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

🙏 1