Share Validation Logic Across Clojure and ClojureScript with .cljc Reader Conditionals
Use reader conditionals in .cljc files to share pure functions between Clojure and ClojureScript without duplicating code.
16 Sept 2025, 05:11 UTC

You are maintaining the same rule in two languages
Your Clojure server validates user input before writing to the database. Your ClojureScript frontend validates the same input before sending it. The rules are simple now—an email must match a pattern, a name must not be empty—but they will change. When they do, someone has to remember to edit both copies. Miss one, and the client accepts data the server rejects, or vice versa.
The useful takeaway: you can store pure logic in a .cljc file and load it unchanged from both Clojure and ClojureScript. Reader conditionals (#?(:clj ... :cljs ...)) let you handle platform differences at read time, so one namespace serves both targets without file duplication.
How reader conditionals work
A .cljc file is a Clojure source file that both the Clojure reader and the ClojureScript reader can process. When the reader encounters #?(:clj form :cljs form), it keeps only the branch for the current target and discards the other before compilation. This happens at read time, not while the program is running, so the branch choice cannot depend on runtime data.
Because the discarded branch never reaches the compiler, ClojureScript does not see JVM-only code in the :clj branch, and Clojure does not see ClojureScript-only code in the :cljs branch.
Worked example: shared email validation
Create src/shared/validation.cljc in your project root. You need normal user write access to the source tree.
;; src/shared/validation.cljc
(ns shared.validation
(:require [clojure.string :as str]))
(defn valid-email? [email]
(and (some? email)
(re-matches #".+@.+\..+" email)))
(defn normalize [value]
#?(:clj (str/trim value)
:cljs (str/trim value)))
clojure.string exists on both platforms, and trim behaves the same for this example, so the reader conditional is optional here. It becomes necessary when the two platforms provide different APIs for the same operation.
Require the namespace from a Clojure REPL:
;; Run in a Clojure REPL (lein repl, clj, or equivalent)
(require '[shared.validation :as val])
(val/valid-email? "[contact removed]")
;; expected: true
Require the same namespace from ClojureScript:
;; ns app.core
(:require [shared.validation :as val])
(val/valid-email? "[contact removed]")
;; expected: true
Build configuration and verification
Run your ClojureScript build from the project directory as a normal user. For example, with Shadow CLJS:
npx shadow-cljs watch app
Replace app with your build id. After the build completes, open the emitted JavaScript for your application and search for shared.validation. You should see the :cljs branch compiled into JavaScript. You should not see the :clj branch or any JVM interop from it.
To verify equivalence, call valid-email? with the same inputs under both hosts and compare the results. Do not assume parity; check it.
Limitations
- Reader conditionals choose at read time. They cannot switch based on a value that only exists while the program runs.
- A shared namespace must avoid JVM-only Java interop. ClojureScript cannot load arbitrary Java classes, so a
:cljbranch that calls Java directly will break the ClojureScript build if that branch is ever read by the ClojureScript reader. - Macros in
.cljcrequire explicit:require-macrosand may expand differently on each host. Start with plain functions.
Practical check before you ship
- Add one pure function to a new
.cljcfile. - Require it from a Clojure REPL and confirm the result.
- Require it from a ClojureScript build and confirm the same result.
- Inspect the compiled JavaScript to confirm the unused branch was dropped.
If the results match, extract the next shared rule. If they do not, keep the platform-specific code separate until the logic is truly portable.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.