Skip to content

Instantly share code, notes, and snippets.

@arrdem
Created January 7, 2019 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arrdem/94dd85f5c18bbed22822b36efffa61dd to your computer and use it in GitHub Desktop.
Save arrdem/94dd85f5c18bbed22822b36efffa61dd to your computer and use it in GitHub Desktop.
(ns spec.extras.valid
(:require [clojure.spec.alpha :as s]))
(defn valid!
"`#'s/valid?` but throws an `ex-info` with `#'s/explain-data` of the
failure when `val` doesn't conform to `spec`.
Intended for always-on postconditions because `#'s/assert` is off by
default."
[spec val]
(if (s/valid? spec val)
true
(throw (ex-info (format "Failed to validate value %s as spec %s"
(pr-str val) spec)
(s/explain-data spec val)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment