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.

Writing a spec should enable automatic:

  • Validation
  • Error reporting
  • Destructuring
  • Instrumentation
  • Test-data generation
  • Generative test generation

See why and how.

180 questions
52
votes
3 answers

Where to put specs for Clojure.Spec?

So, I'm diving deeper and deeper into Clojure.Spec. One thing I stumbled upon is, where to put my specs. I see three options: Global Spec File In most examples, I found online, there is one big spec.clj file, that gets required in the main…
DiegoFrings
  • 3,043
  • 3
  • 26
  • 30
28
votes
1 answer

How can I use my specs for their intended purposes if they are in a separate namespace?

One of the examples in the clojure.spec Guide is a simple option-parsing spec: (require '[clojure.spec :as s]) (s/def ::config (s/* (s/cat :prop string? :val (s/alt :s string? :b boolean?)))) (s/conform ::config ["-server" "foo"…
Sam Estep
  • 12,974
  • 2
  • 37
  • 75
23
votes
4 answers

Meaningful error message for Clojure.Spec validation in :pre

I used the last days to dig deeper into clojure.spec in Clojure and ClojureScript. Until now I find it most useful, to use specs as guards in :pre and :post in public functions that rely on data in a certain format. (defn person-name [person] …
DiegoFrings
  • 3,043
  • 3
  • 26
  • 30
16
votes
2 answers

Forbidden keys in clojure.spec

I am following the clojure.spec guide. I understand it is possible to declare required and optional attributes when using clojure.spec/keys. I don't understand what is meant by optional. To me :opt doesn't do anything. (s/valid? (s/keys :req…
Johan Jonasson
  • 500
  • 2
  • 18
12
votes
3 answers

Is use of clojure.spec for coercion idiomatic?

I've seen use of clojure conformers to coerce data in various gists, but have also picked up an impression (I can't recall where though) that coercion (e.g. as follows) is not idiomatic use of conformers. (s/def :conformers/int (s/conformer (fn…
Daniel Neal
  • 4,165
  • 2
  • 20
  • 34
11
votes
2 answers

What does retag parameter in s/multi-spec mean?

Can you explain with examples how does retag parameter impacts multi-spec creation? I find multi-spec documentation hard to digest.
OlegTheCat
  • 4,443
  • 16
  • 24
11
votes
1 answer

Are Clojure's specs equivalent to Wadler's propositions?

Wadler wrote an amazing paper: Propositions as Types - where he talks about the Howard-Curry correspondence, that you can check program behaviour in terms of the types of the program. (For a given subset of languages). Recently Rich Hickey…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
9
votes
1 answer

Clojure Spec for a map with non-keyword keys

Lets look at the real-world example of Leiningen project maps :global-vars: ;; Sets the values of global vars within Clojure. This example ;; disables all pre- and post-conditions and emits warnings on ;; reflective calls. See the Clojure…
Rovanion
  • 4,382
  • 3
  • 29
  • 49
9
votes
1 answer

Using Clojure Spec with Datomic entities

Say I have the following specs: (s/def :person/age number?) (s/def :person/name string?) (s/def ::person (s/keys :req [:person/name :person/age])) Then I fetch an entity from Datomic: (def person-entity (d/entity (d/db conn) [:person/name "Mr…
Odinodin
  • 2,147
  • 3
  • 25
  • 43
8
votes
2 answers

How can I spec a hybrid map?

After writing this answer, I was inspired to try to specify Clojure's destructuring language using spec: (require '[clojure.spec :as s]) (s/def ::binding (s/or :sym ::sym :assoc ::assoc :seq ::seq)) (s/def ::sym (s/and simple-symbol? (complement…
Sam Estep
  • 12,974
  • 2
  • 37
  • 75
7
votes
1 answer

Clojure Spec vs Typed vs Schema

In my Clojure project, I am using Clojure Spec but If I need to use some lib like compojure-api then I need to use Schema. What is the advantage one over the others? Why would I consider one over the others? Which one is good for compile type…
Mamun
  • 512
  • 3
  • 10
7
votes
1 answer

clojure.spec human readable shape?

With clojure.spec, is there a way to define a more “human-readable” spec for nested maps? The following doesn't read very well: (s/def ::my-domain-entity (s/keys :req-un [:a :b]) (s/def :a (s/keys :req-un [:c :d])) (s/def :b boolean?) (s/def :c…
Clev3r
  • 1,568
  • 1
  • 15
  • 28
7
votes
3 answers

clojure spec: map containing either a :with or a :height (XOR)

The following clojure spec ::my permits maps having either the key :width or the key :height, however it does not permit having both of them: (s/def ::width int?) (s/def ::height int?) (defn one-of-both? [a b] (or (and a (not b)) (and b…
Anton Harald
  • 5,772
  • 4
  • 27
  • 61
7
votes
1 answer

Metaprogramming with clojure.spec values?

I've been trying out clojure.spec, and one idea I have for how to use it is to generate the UI for editing an instance of the map I'm specifying. For example, it might generate a web form with a datepicker field for a key that's specified to be a…
Derek Thurn
  • 14,953
  • 9
  • 42
  • 64
7
votes
2 answers

clojure.spec custom generator for Java objects

I just saw one of Rich's talks on clojure.spec, and really want to give it a try on my project. I'm writing a series of tools for parsing C code using the eclipse CDT library, and I would like to spec that my functions accept and emit AST objects. I…
user12341234
  • 6,573
  • 6
  • 23
  • 48
1
2 3
11 12