Does a value conform to a specification?

clojure.spec.alpha/conform takes two arguments

  • a specification
  • a value to test against the specification

:clojure.spec.alpha/invalid is returned when a value does not conform to a specification.

If the value does conform to the specification, then the value is returned. This value is referred to as a conformed value.

Require the Clojure spec library

Set the namespace for the page and require clojure.spec.alpha library, setting the alias to spec

  1. (ns practicalli.clojure.specifications
  2. (:require [clojure.spec.alpha :as spec]))

Using conform

If the value conforms to the spec, a conformed value is returned

  1. (spec/conform odd? 101)

When a value does not conform to a spec, the value :clojure.spec.alpha/invalid is returned

  1. (spec/conform even? 101)
  1. (spec/conform integer? 1)
  1. (spec/conform seq? [1 2 3])
  1. (spec/conform seq? (range 10))
  1. (spec/conform map? {})
  1. (spec/conform map? (hash-map :a 1 :b 2))