Skip to content

Commit

Permalink
Validate :render-fn in process-viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
philippamarkovics committed Oct 7, 2024
1 parent 4e1a6ad commit a7bb15d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/nextjournal/clerk/viewer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1555,13 +1555,22 @@
(.update hasher (goog.crypt/stringToUtf8ByteArray (pr-str x)))
(.digest hasher))))

(defn validate-viewer! [{:as viewer :keys [render-fn]}]
(when (and render-fn (not (or (list? render-fn)
(symbol? render-fn))))
(throw (ex-info (format "`:render-fn` must to be a quoted form or symbol, got a %s instead." (if (fn? render-fn)
"function" (type render-fn)))
{:viewer viewer
:render-fn-type (type render-fn)}))))

(defn process-viewer [viewer {:nextjournal/keys [render-evaluator]}]
;; TODO: drop wrapped-value arg here and handle this elsewhere by
;; passing modified viewer stack
;; `(clerk/update-viewers viewers {:render-fn #(assoc % :render-evaluator :cherry)})`
(if-not (map? viewer)
viewer
(-> viewer
(-> (doto viewer
validate-viewer!)
(cond-> (and (not (:render-evaluator viewer)) render-evaluator)
(assoc :render-evaluator render-evaluator))
(dissoc :add-viewers :pred :transform-fn :update-viewers-fn)
Expand Down
6 changes: 5 additions & 1 deletion test/nextjournal/clerk/viewer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@

(is (= (v/present (v/with-viewer v/html-viewer [:h1 "ahoi"]))
(v/present {:nextjournal/value (v/with-viewer v/html-viewer [:h1 "ahoi"])})
(v/present {:nextjournal/value {:nextjournal/value (v/with-viewer v/html-viewer [:h1 "ahoi"])}})))))
(v/present {:nextjournal/value {:nextjournal/value (v/with-viewer v/html-viewer [:h1 "ahoi"])}}))))

(testing "invalid `:render-fn` throws error"
(is (thrown? clojure.lang.ExceptionInfo
(v/present (v/with-viewer {:render-fn (fn [x] [:h1 x])} "Mike"))))))

(deftest present-exceptions
(testing "can represent ex-data in a readable way"
Expand Down

0 comments on commit a7bb15d

Please sign in to comment.