Skip to content

Add Spec to a projectλ︎

Create a new project or clone practicalli/leveraging-spec which includes several examples of using Clojure Spec.

Create a new Clojure CLI project using the Practicalli project templates

Create new project
clojure -T:project/create :template practicalli/minimal :name practicalli/leveraging-spec
Practicalli Clojure CLI Config - :project/create alias

Practicalli Clojure CLI Config repository includes the :project/create alias for creating new Clojure projects from a template using deps-new.

The project is created with Clojure as a dependency, which includes the clojure.spec.alpha library.

Clojure 1.9.0 or higher versions include Clojure Spec. Clojure 1.11.1 is recommended.

practicalli/leveraging-spec project includes Clojure Spec examples for values and functional arguments, along with unit tests using clojure spect-test.

https://github.com/practicalli/leveraging-spec.git

Project Dependenciesλ︎

Clojure Spec is included in Clojure so only org.clojure/clojure dependency is required in the deps.edn file for the project.

Clojure project dependency

deps.edn
{:paths ["src" "resources"]
 :deps {org.clojure/clojure {:mvn/version "1.11.1"}}}

Use spec in namespaceλ︎

Require the clojure.spec.alpha namespace in the ns definition using the spec alias name. Practicalli recommends using spec (rather than s as it is much clearer as to where the functions are defined)

Clojure project dependency

(ns practicalli.leveraging-spec
  (:require [clojure.spec.alpha :as spec]))

Evaluate the namespace definition to start using the functions from the namespace.

Clojure project - require namespace and evaluate

All functions from clojure.spec.alpha are now accessible using the spec alias, e.g. spec/conform, spec/valid?, spec/def.

Testing with specλ︎

Add the clojure.spec.test.alpha using the spec-test alias, along with clojure.spec.test.alpha as spec alias

Clojure project dependency

src/practicalli/leveraging_spec.clj
(ns practicalli.leveraging-spec
  (:require
    [clojure.spec.alpha :as spec]
    [clojure.spec.test.alpha :as spec-test]))