clojure-guardrails

Ramblurr's avatarfrom Ramblurr

Reference for Guardrails library with Malli in Clojure. Use when working with `>defn`, `>defn-`, `>def`, `>fdef` macros, gspec syntax, or function validation. Triggers on guardrails imports, `com.fulcrologic.guardrails.malli`, function specs with `=>` operator, or questions about runtime validation in Clojure.

⭐0stars🔀0forks📁View on GitHub🕐Updated Dec 22, 2025

When & Why to Use This Skill

This Claude skill serves as a comprehensive technical reference for the Clojure Guardrails library integrated with Malli. It empowers developers to implement robust runtime validation and function specifications using specialized macros like `>defn`, `>def`, and `>fdef`. By providing instant access to Gspec syntax, schema registry patterns, and configuration best practices, it ensures high code reliability, type safety, and better documentation for Clojure applications.

Use Cases

  • Implementing runtime type checking and contract validation for Clojure functions to identify bugs during development and testing.
  • Defining complex data schemas and domain constraints using Malli's Gspec syntax for enhanced data integrity.
  • Refactoring Clojure codebases by adding inline function specifications to improve code readability and maintainability.
  • Configuring project dependencies and environment settings in `deps.edn` to enable or disable Guardrails validation across different environments.
  • Troubleshooting macro usage and syntax errors when working with multi-arity or variadic functions in Clojure.
nameclojure-guardrails
descriptionReference for Guardrails library with Malli in Clojure. Use when working with `>defn`, `>defn-`, `>def`, `>fdef` macros, gspec syntax, or function validation. Triggers on guardrails imports, `com.fulcrologic.guardrails.malli`, function specs with `=>` operator, or questions about runtime validation in Clojure.

Clojure Guardrails (Malli)

Detection

Check if guardrails is in use:

grep -r "guardrails.enabled" deps.edn shadow-cljs.edn 2>/dev/null

Setup

deps.edn:

{:deps {com.fulcrologic/guardrails {:mvn/version "1.2.16"}
        metosin/malli             {:mvn/version "0.20.0"}}
 :aliases {:dev {:jvm-opts ["-Dguardrails.enabled=true"]}}}

See https://clojars.org/com.fulcrologic/guardrails for the latest version.

Enable at runtime: -Dguardrails.enabled=true

Import

(require '[com.fulcrologic.guardrails.malli.core :refer [>defn >defn- >def >fdef | ? =>]])

Core Macros

Macro Purpose
>defn Define function with inline spec
>defn- Private function with spec
>def Register malli schema
>fdef Declare spec without body
? Nilable shorthand [:maybe schema]
| "Such that" constraints
=> Separates args from return

Gspec Syntax

[arg-specs* (| arg-preds+)? => ret-spec (| ret-preds+)?]

Basic:

(>defn add [a b]
  [:int :int => :int]
  (+ a b))

With constraints:

(>defn ranged-rand [start end]
  [:int :int | #(< start end)
   => :int | #(>= % start) #(< % end)]
  (+ start (long (rand (- end start)))))

Nilable:

(>defn find-user [id]
  [:int => (? :map)]
  (get users id))

Multi-arity:

(>defn greet
  ([name]
   [:string => :string]
   (str "Hello, " name))
  ([greeting name]
   [:string :string => :string]
   (str greeting ", " name)))

Variadic:

(>defn sum [x & more]
  [:int [:* :int] => :int]
  (apply + x more))

Map schemas:

(>defn process-user [user]
  [[:map [:name :string] [:age :int]] => :string]
  (str (:name user) " is " (:age user)))

Schema Registry

This is an optional feature.

(require '[com.fulcrologic.guardrails.malli.registry :as gr.reg])

;; Register schemas
(>def :user/name :string)
(>def :user/age :int)
(>def :user/record [:map :user/name :user/age])

;; Use in functions
(>defn get-user [id]
  [:int => (? :user/record)]
  (lookup id))

Detailed Reference

See gspec-syntax.md for complete syntax documentation.