An opinionated assertions (micro) library for Clojure/Script.
(have x)
(have pred (:in) x)
(have pred (:in) x & more-xs)
Takes a pred and one or more vals. Tests pred against each val, trapping errors. If any pred test fails, throws a detailed assertion error. Otherwise returns input val/vals for convenient inline-use/binding. Respects *assert* value so tests can be elided from production for zero runtime costs. Provides a small, simple, flexible feature subset to alternative tools like clojure.spec, core.typed, prismatic/schema, etc. ;; Will throw a detailed error message on invariant violation: (fn my-fn [x] (str/trim (have string? x))) You may attach arbitrary debug info to assertion violations like: `(have string? x :data {:my-arbitrary-debug-info "foo"})` Re: use of Truss assertions within other macro bodies: Due to CLJ-865, call site information (e.g. line number) of outer macro will unfortunately be lost. See `keep-callsite` util for a workaround. See also `have?`, `have!`.
(have! x)
(have! pred (:in) x)
(have! pred (:in) x & more-xs)
Like `have` but ignores *assert* value (so can never be elided). Useful for important conditions in production (e.g. security checks).
(have!? x)
(have!? pred (:in) x)
(have!? pred (:in) x & more-xs)
Specialized cross between `have?` and `have!`. Not used often but can be handy for semantic clarification and/or to improve multi-val performance when the return vals aren't necessary. **WARNING**: Do NOT use in :pre/:post conds since those are ALWAYS subject to *assert*, directly contradicting the intention of the bang (`!`) here.
(have? x)
(have? pred (:in) x)
(have? pred (:in) x & more-xs)
Like `have` but returns `true` on successful tests. In particular, this can be handy for use with :pre/:post conditions. Compare: (fn my-fn [x] {:post [(have nil? %)]} nil) ; {:post [nil]} FAILS (fn my-fn [x] {:post [(have? nil? %)]} nil) ; {:post [true]} passes as intended
(keep-callsite & body)
CLJ-865 unfortunately means that it's currently not possible for an inner macro to access the &form metadata of an outer macro. This means that inner macros lose call site information like the line number of the outer macro. This util offers a workaround for macro authors: (defmacro my-macro1 [x] `(truss/have ~x)) ; W/o call site info (defmacro my-macro2 [x] (keep-callsite `(truss/have ~x))) ; With call site info
(set-error-fn! f)
Sets the root (fn [data-map-delay]) called on invariant violations.
(with-data data & body)
Executes body with dynamic assertion data bound to given value. This data will be included in any violation errors thrown by body.
(with-dynamic-assertion-data data & body)
Prefer `with-data`
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close