From b786b8e07a718701be814ebde6441dcc30f44158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Tue, 9 Dec 2025 11:09:38 +0100 Subject: [PATCH] :wrench: Use automatic validation in token proxies --- common/src/app/common/schema.cljc | 4 ++-- frontend/src/app/plugins/tokens.cljs | 21 ++++++++++----------- frontend/src/app/plugins/utils.cljs | 16 ++++++++++++++++ frontend/src/app/util/object.cljc | 5 +++++ 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/common/src/app/common/schema.cljc b/common/src/app/common/schema.cljc index 7a95da1b8f..9a3f2f205d 100644 --- a/common/src/app/common/schema.cljc +++ b/common/src/app/common/schema.cljc @@ -95,12 +95,12 @@ (defn assoc-key "Add a key & value to a schema" [s k v] - (mu/assoc s k v)) + (mu/assoc (schema s) k v)) (defn dissoc-key "Remove a key from a schema" [s k] - (mu/dissoc s k)) + (mu/dissoc (schema s) k)) (defn ref? [s] diff --git a/frontend/src/app/plugins/tokens.cljs b/frontend/src/app/plugins/tokens.cljs index 67e7d7d8dc..b7c1b6abf3 100644 --- a/frontend/src/app/plugins/tokens.cljs +++ b/frontend/src/app/plugins/tokens.cljs @@ -114,7 +114,8 @@ (defn token-set-proxy [plugin-id file-id id] - (obj/reify {:name "TokenSetProxy"} + (obj/reify {:name "TokenSetProxy" + :wrap u/wrap-errors} :$plugin {:enumerable false :get (constantly plugin-id)} :$file-id {:enumerable false :get (constantly file-id)} :$id {:enumerable false :get (constantly id)} @@ -206,16 +207,14 @@ (token-proxy plugin-id file-id id token-id))))) :addToken - (fn [attrs] - (let [schema (-> (sm/schema (cfo/make-token-schema - (-> (u/locate-tokens-lib file-id) - (ctob/get-tokens id)))) - (sm/dissoc-key :id)) ;; We don't allow plugins to set the id - attrs (u/coerce attrs schema :addToken "invalid token attrs")] - (when attrs - (let [token (ctob/make-token attrs)] - (st/emit! (dwtl/create-token id token)) - (token-proxy plugin-id file-id (:id set) (:id token)))))) + {:schema [:tuple (-> (cfo/make-token-schema + (-> (u/locate-tokens-lib file-id) + (ctob/get-tokens id))) + (sm/dissoc-key :id))] ;; We don't allow plugins to set the id + :fn (fn [attrs] + (let [token (ctob/make-token attrs)] + (st/emit! (dwtl/create-token id token)) + (token-proxy plugin-id file-id (:id set) (:id token))))} :duplicate (fn [] diff --git a/frontend/src/app/plugins/utils.cljs b/frontend/src/app/plugins/utils.cljs index bec1a5e710..0dbc1f6c91 100644 --- a/frontend/src/app/plugins/utils.cljs +++ b/frontend/src/app/plugins/utils.cljs @@ -254,3 +254,19 @@ [values] (let [s (set values)] (if (= (count s) 1) (first s) "mixed"))) + +(defn wrap-errors + "Function wrapper to be used in plugin proxies methods to handle errors. + When an exception is thrown, a readable error message is output to the console + and the exception is captured." + [f] + (fn [] + (let [args (js-arguments)] + (try + (.apply f nil args) + (catch :default cause + (display-not-valid (ex-message cause) (obj/stringify args)) + (if-let [explain (-> cause ex-data ::sm/explain)] + (println (sm/humanize-explain explain)) + (js/console.log (ex-data cause))) + nil))))) \ No newline at end of file diff --git a/frontend/src/app/util/object.cljc b/frontend/src/app/util/object.cljc index bb37d05c9f..fd33c4b836 100644 --- a/frontend/src/app/util/object.cljc +++ b/frontend/src/app/util/object.cljc @@ -106,6 +106,11 @@ (identical? (.getPrototypeOf js/Object o) (.-prototype js/Object))))) +#?(:cljs + (defn stringify + [obj] + (js/JSON.stringify obj))) + ;; EXPERIMENTAL: unsafe, does not checks and not validates the input, ;; should be improved over time, for now it works for define a class ;; extending js/Error that is more than enought for a first, quick and