Questions tagged [clojure.spec]
clojure.spec is a Clojure language feature introduced in Clojure 1.9 for defining predicative and structural data and function definitions.
181
questions
0
votes
0
answers
11
views
Clojure spec surprising behaviour
I am trying to learn spec, and have come across the following behaviour.
My code:
(s/def ::foo (s/cat :first symbol?
:rest (s/alt :simple vector?
:...
0
votes
1
answer
27
views
Nested seqs in clojure.spec
When I use clojure.spec, I am trying to figure out how to spec nested seq/vector. Here is my failing example:
(s/def ::test (s/cat :s any? :r (s/cat :int int?)))
(s/explain-str ::test ["test"...
1
vote
1
answer
75
views
Clojure.Spec: error when using spec/cat to specificate maps
I have this spec for a customer (using spec/cat instead of spec/keys for readability reasons):
(ns thrift-store.entities.customer
(:require [clojure.spec.alpha :as s]
[thrift-store....
1
vote
1
answer
102
views
Error -> should contain keys in clojure.spec
I'm new to Clojure and I have a problem that I really can't find a solution to.
I have a structure already defined with Clojure Spec, its keys and data types match with a JSON file.
Doing an ...
0
votes
1
answer
71
views
How To Use Spec with Namespace Keywords That Aren't Valid Namespaces Coming From Datomic?
I'm not sure how to word my question, so apologies if it's confusing. I'm currently trying to write specs for some data that's coming out of datomic. A map I'm getting back might look like:
{:id "...
1
vote
0
answers
81
views
clojure/spec: Built in way to create a generator from an existing function?
I had a situation where I had an existing function that generated examples of a particular type x:
(defn generate-sample-of-x [] ... )
and I wanted to use that inside of spec as a generator. I could ...
1
vote
1
answer
104
views
Clojure Spec to parse Reducible
The doc of clojure.spec.alpha/+ says:
Returns a regex op that matches one or more values matching
pred. Produces a vector of matches
And I can use it like this:
erdos=> (s/conform (s/+ (s/cat :e #...
1
vote
1
answer
501
views
Test for a Valid Instance of java.time.LocalDate using Clojure Spec
I am trying to use Clojure Spec to define a data structure containing a java.time.LocalDate element:
(s/def :ex/first-name string?)
(s/def :ex/last-name string?)
(s/def :ex/birth-date (s/valid? inst? (...
0
votes
2
answers
97
views
Deleting any node inside BST - Clojure
I'm studying algorithms and at the class we were asked to create a BST with structures, I'm trying really hard to create a delete function but the one I created isn't efficient and doesn't work. I ...
0
votes
1
answer
153
views
Clojure spec for vector of map with indeterminate number of keys
I am trying to write a clojure spec for a function that takes the following two maps as parameters.
{1 BL0-MD0-SU, 0 BL0-SM0-SU}
{0 [https://gateway.pinata.cloud/ipfs/...
1
vote
1
answer
154
views
passing optional :query-param in clojure request
I have started learning clojure and have one use case where I would want to call a downstream service with query-params. Now these query-params can vary and can be absent. I am stuck at how to ...
0
votes
1
answer
156
views
clojure project build using gradle clojurephant plugin "namespace not found after loading "
I'm trying to build clojure project with gradle build tool using clojurephant plugin.
The project is on github here
My build file is -
plugins {
id 'dev.clojurephant.clojure' version '0.6....
0
votes
1
answer
160
views
Clojure spec.alpha - How to (reference another argument) / (describe that argument collection should include values from another argument collection)
What I need:
a spec for a function, that has two arguments:
a hash-map of keywords and strings.
a vector that may have strings or keywords but if it is a keyword it must exist inside hash-map (first ...
2
votes
3
answers
213
views
Spec: partially overriding generators in a map spec
Assuming I have already defined a spec from which I'd like to generate test data:
(s/def :customer/id uuid?)
(s/def :customer/given-name string?)
(s/def :customer/surname string?)
(s/def :customer/age ...
1
vote
2
answers
308
views
How to define specs dynamically?
I'm looking to generate a set of specs based off some data I'm pulling from a request. I'd like to dynamically define some specs based off the data I receive.
(def my-key :frame-data/pretty_name) ;;...
1
vote
1
answer
106
views
Problem with Clojure Spec about simple parameter matching
I'm struggling with Clojure(script) spec.
I slightly found out what part causes problem but I can't solve it.
(defn filter-ids
[[items fields] _]
(let [ids
(for [item items
...
0
votes
1
answer
99
views
Is clojure.spec check generating bad input?
Using clojure.spec (org.clojure/clojurescript {:mvn/version "1.10.520"}), I have a function spec that specifies a map for its input.
gen/generate and gen/sample work fine. But calling cljs....
0
votes
1
answer
41
views
Howto include cljs.spec'd functions in a test suite (Redux)
I'm trying to wire into the cljs.test reporting system with a custom macro. I'm following the pattern in cljs.test/deftest:
https://cljs.github.io/api/cljs.test/deftest
https://github.com/clojure/...
1
vote
1
answer
198
views
Is it common for people to test their clojure.spec specs?
I'm learning Clojure, all by myself and I've been working on a simple toy project to create a Kakebo (japanese budgeting tool) for me to learn. First I will work on a CLI, then an API.
Since I'm just ...
0
votes
2
answers
205
views
Clojure.Spec derive or alias another spec
I'd like to use clojure spec to build up a set of type constraints that can be aliased or further constrained by other specs.
For example, I might have many fields that all need to be valid sanitized ...
-1
votes
1
answer
189
views
Clojure spec for a single key in a map
I'm speccing http responses from Google Calendar API
and I want different specs for each response type.
I've defined a spec for HTTP response as
(s/def ::http-resp
(s/keys :req-un [:status] :opt-un [...
0
votes
3
answers
535
views
Can I validate functions with Clojure spec?
Can I use the Clojure spec system to define function signatures and verify if functions satisfy them?
Here are some examples I've tried without success
(s/valid? (s/fspec :args string? :ret string?) ...
2
votes
1
answer
97
views
Can Clojure Spec be used to constrain protocol argument and return types?
I'd like to know if spec can be used to constrain argument and return types on a protocol similar to how fdef works for functions.
The best lead I've found so far is a section called protocols in the ...
3
votes
1
answer
275
views
Spec for map with interdependent values between nested levels?
I'm trying to define a spec for a portion of the GraphQL schema syntax. Here's what a field type looks like as returned from an API (note that :ofType can be infinitely nested):
{:kind "NON_NULL&...
0
votes
1
answer
122
views
Clojure spec for dependencies between nodes in a recursively defined data structure
Is there a canonical or idiomatic way of writing a spec for dependencies between the values in nodes in a recursively defined data structure?
As a minimal example, let's say I want to store a sorted ...
2
votes
1
answer
265
views
Clojure Spec: Adding generators to an existing spec
Is there a clean way of adding spec generators in test code that apply to a spec defined in another source file? For example, say I have the following under my main src/ directory:
(s/fdef my-func
...
2
votes
1
answer
152
views
Use macros with doseq to generate spec
I find myself writing a lot of specs like this:
(s/def ::name string?)
(s/def ::logUri string?)
(s/def ::subnet (s/and string? #(> (count %) 5)))
(s/def ::instanceType string?)
...
(s/def ::key (s/...
2
votes
2
answers
351
views
Clojurescript first steps doubts and problems
I just started with clojurescript and I have many doubts, what I want to do is use clojurescript only to solve basic problems, for now nothing to do with web.
If I eliminate the web development part, ...
0
votes
1
answer
327
views
Clojure spec maps
Having two following specs:
(s/def ::x keyword?)
(s/def ::y keyword?)
(s/def ::z keyword?)
(s/def ::a
(s/keys :req-un [::x
::y]
:opt-un [::z]))
(s/def ::b
(s/map-of ...
1
vote
0
answers
155
views
Is there any way to convert clojure spec to a readble json response?
I have been using clojure.spec.alpha library for request body validation. Unfortunately, I after convert spec from s/explain-data to json, the given error is not quite clear:
{
"clojure.spec....
1
vote
1
answer
454
views
Clojure Spec: How can I spec a tuple as a function argument?
I have a function in Clojure that accepts a 2-element vector as an argument:
(defn influence [[school value]])
I would like to write a spec for the arguments to this function using existing specs I ...
1
vote
0
answers
214
views
Can I merge two keys specs requiring the same unqualified key?
In the same namespace I define the following specs:
(s/def :foo/x string?)
(s/def :foo/o (s/keys :req-un [:foo/x]))
(s/valid? :foo/o {:x "true"})
;=> true
(s/def :bar/x boolean?)
(s/def :bar/o (...
0
votes
1
answer
234
views
Clojure Spec - Register a spec pointing at another spec
Could someone shed a light on the following behavior please?
Let's assume I have this namespace with a spec:
(ns user.specs
(:require [clojure.alpha.spec :as s]
[clojure.alpha.spec.gen ...
6
votes
1
answer
282
views
Clojure.spec: field existence based on other field in generator
Let's say we have API to save different type of files' attributes into DB*: text, images, audio and video files. It should be able to get the following fields based on their type:
base properties for ...
0
votes
2
answers
59
views
Is there a significant performance difference between running `explain-data` and `valid?`
Is there a significant performance difference between running explain-data and valid?.
For example, I have this code
(let [is-valid? (spec/valid? ::SpecitySpec the-map)
_ (when-not is-valid?
...
3
votes
2
answers
410
views
Did :pre & :post defn conditions become obsolete after core.spec was introduced?
I'm new to Clojure and at first sight from what I see core.spec does everything that :pre & :post defn conditions is used for PLUS much more.
I understand that core.spec was introduced much later(...
0
votes
1
answer
318
views
Is it ok to use spec/valid? for function input validation at runtime?
I am writing a clojure library, and I am thinking on using clojure.spec. Is it good practice to use spec/valid? on functions input at runtime? or should I use destracturing along with type hints? I am ...
2
votes
1
answer
429
views
Validate map of known and unknown key-values with Spec
I want to create a clojure spec to validate :multipart parameter of an http request, created by reitit.ring.middleware.multipart middleware.
The multipart form data must contain particular parameters,...
1
vote
1
answer
418
views
Combining s/and with s/or in Clojure spec
I want to write a spec for a map that either has the key :rule/children or has two keys - :condition/field and :condition/predicate. This is what I have tried:
(s/keys :req [(s/or :children :rule/...
0
votes
1
answer
266
views
I can't understand the following clojure.spec error
I can't understand why the following spec error occurred.
Could anyone tell me why?
(ns spec-test
(:require [clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as stest]))
;;; ...
3
votes
3
answers
544
views
Clojure: how to enable spec asserts globally?
I would like to use clojure specs to check the input of my functions (at least during development). So far, I have done the following: I have defined specs and at the beginnings of the functions I ...
0
votes
1
answer
311
views
Overriding Clojure spec in a different namespace
I am wondering if there is a way from one namespace to change the spec in another namespace and still retain the uses of that modified spec in subsequent specs. The use case is that the namespace ...
1
vote
1
answer
81
views
Can one make a Clojure spec private?
I have a spec for a "vertex" (a 2D point in the plane, represented with a map holding two double values) and a "rectangle" (an axis-aligned rectangular area in the plane, represented by a map of two "...
2
votes
2
answers
653
views
How to fix "clojure.test.check.generators never required" when exercising a function in clojurescript?
In a Clojurescript environment when I run the following code I get the error Var clojure.test.check.generators/simple-type-printable does not exist, clojure.test.check.generators never required.
cljs....
3
votes
1
answer
653
views
Clojure spec "conformer" function, what is it?
The clojure.spec.alpha API has a macro called conformer with this description:
Usage: (conformer f)
(conformer f unf)
takes a predicate function with the semantics of conform i.e. it should
...
0
votes
1
answer
181
views
Why does `clojure.spec.alpha/every-kv` accept `[]` as a valid "associative collection"?
We run this:
Definition of lovely specs:
(require '[clojure.spec.alpha :as s :refer [valid? explain conform]])
(s/def ::fun-key integer?)
(s/def ::fun-value keyword?)
(s/def ::fun
(s/every-kv ::...
1
vote
1
answer
404
views
how to check the mocked function is called atleast once with given arguments in clojure with_redefs?
The test I have written below won't fail if test-service/process is not called. And it also don't check if the URL and params are right.
(deftest main-func-test
(let [event {:a 123}]
(with-...
3
votes
2
answers
182
views
Clojure: speccing a couple of constraints; forced to "break out" into a predicate. Any better way?
I have this structure to tile the 2D plane with squares:
A "strip" is an ensemble of "squares" with the same x coordinate.
A struct-map representing the strip holds said x coordinate as ::sq-x and a ...
3
votes
2
answers
1k
views
Clojure spec: checking map values directly?
I'm writing a spec for a square, which is very simply a pair of integer coordinates (keys ::sq-x ::sq-y) combined with a sequential collection of vertices (key ::vtxs).
Speccing this constraint:
(s/...
0
votes
2
answers
296
views
Why there is error in importing clojure.spec.alpha?
I'm having a boot-clj project with the below dependencies,
:dependencies '[[org.clojure/clojure "RELEASE"]
[adzerk/boot-test "RELEASE" :scope "test"]]
Under that ...