Skip to content

taoensso/truss

Repository files navigation

Taoensso open source
Documentation | Latest releases | Get support

Truss

Assertions micro-library for Clojure/Script

Truss is a tiny Clojure/Script library that provides fast and flexible runtime assertions with terrific error messages. Use it as a complement or alternative to clojure.spec, core.typed, etc.

Egyptian ship with rope truss, the oldest known use of trusses (about 1250 BC).

A doubtful friend is worse than a certain enemy. Let a man be one thing or the other, and we then know how to meet him. - Aesop

Latest release/s

Main tests Graal tests

See here for earlier releases.

Why Truss?

  • Tiny cross-platform Clj/s codebase with zero dependencies
  • Trivially easy to learn, use, and understand
  • Terrific error messages for quick+easy debugging
  • Terrific performance: miniscule (!) runtime cost
  • Easy elision for zero runtime cost
  • No commitment or costly buy-in: use it just when+where needed
  • Perfect for library authors: no bulky dependencies

Video demo

See for intro and usage:

Truss demo video

Quick example

(require '[taoensso.truss :as truss :refer [have have?]])

;; Truss uses the simple `(predicate arg)` pattern familiar to Clojure users:
(defn square [n]
  (let [n (have integer? n)] ; <- A Truss assertion
    (* n n)))

;; This assertion basically expands to:
;; (if (integer? n) n (throw-detailed-assertion-error!))

(square 5)   ; => 25
(square nil) ; =>
;; Invariant failed at truss-examples[9,11]: (integer? n)
;; {:dt #inst "2023-07-31T09:56:10.295-00:00",
;;  :pred clojure.core/integer?,
;;  :arg {:form n, :value nil, :type nil},
;;  :env {:elidable? true, :*assert* true},
;;  :loc
;;  {:ns truss-examples,
;;   :line 9,
;;   :column 11,
;;   :file "examples.cljc"}}

That's everything most users will need to know, but see the documentation below for more!

Documentation

Funding

You can help support continued work on this project, thank you!! 🙏

License

Copyright © 2014-2024 Peter Taoussanis.
Licensed under EPL 1.0 (same as Clojure).