mirror of
https://github.com/penpot/penpot.git
synced 2025-12-11 22:14:05 +01:00
🔧 Use automatic validation in token proxies
This commit is contained in:
@@ -95,12 +95,12 @@
|
|||||||
(defn assoc-key
|
(defn assoc-key
|
||||||
"Add a key & value to a schema"
|
"Add a key & value to a schema"
|
||||||
[s k v]
|
[s k v]
|
||||||
(mu/assoc s k v))
|
(mu/assoc (schema s) k v))
|
||||||
|
|
||||||
(defn dissoc-key
|
(defn dissoc-key
|
||||||
"Remove a key from a schema"
|
"Remove a key from a schema"
|
||||||
[s k]
|
[s k]
|
||||||
(mu/dissoc s k))
|
(mu/dissoc (schema s) k))
|
||||||
|
|
||||||
(defn ref?
|
(defn ref?
|
||||||
[s]
|
[s]
|
||||||
|
|||||||
@@ -114,7 +114,8 @@
|
|||||||
|
|
||||||
(defn token-set-proxy
|
(defn token-set-proxy
|
||||||
[plugin-id file-id id]
|
[plugin-id file-id id]
|
||||||
(obj/reify {:name "TokenSetProxy"}
|
(obj/reify {:name "TokenSetProxy"
|
||||||
|
:wrap u/wrap-errors}
|
||||||
:$plugin {:enumerable false :get (constantly plugin-id)}
|
:$plugin {:enumerable false :get (constantly plugin-id)}
|
||||||
:$file-id {:enumerable false :get (constantly file-id)}
|
:$file-id {:enumerable false :get (constantly file-id)}
|
||||||
:$id {:enumerable false :get (constantly id)}
|
:$id {:enumerable false :get (constantly id)}
|
||||||
@@ -206,16 +207,14 @@
|
|||||||
(token-proxy plugin-id file-id id token-id)))))
|
(token-proxy plugin-id file-id id token-id)))))
|
||||||
|
|
||||||
:addToken
|
:addToken
|
||||||
(fn [attrs]
|
{:schema [:tuple (-> (cfo/make-token-schema
|
||||||
(let [schema (-> (sm/schema (cfo/make-token-schema
|
|
||||||
(-> (u/locate-tokens-lib file-id)
|
(-> (u/locate-tokens-lib file-id)
|
||||||
(ctob/get-tokens id))))
|
(ctob/get-tokens id)))
|
||||||
(sm/dissoc-key :id)) ;; We don't allow plugins to set the id
|
(sm/dissoc-key :id))] ;; We don't allow plugins to set the id
|
||||||
attrs (u/coerce attrs schema :addToken "invalid token attrs")]
|
:fn (fn [attrs]
|
||||||
(when attrs
|
|
||||||
(let [token (ctob/make-token attrs)]
|
(let [token (ctob/make-token attrs)]
|
||||||
(st/emit! (dwtl/create-token id token))
|
(st/emit! (dwtl/create-token id token))
|
||||||
(token-proxy plugin-id file-id (:id set) (:id token))))))
|
(token-proxy plugin-id file-id (:id set) (:id token))))}
|
||||||
|
|
||||||
:duplicate
|
:duplicate
|
||||||
(fn []
|
(fn []
|
||||||
|
|||||||
@@ -254,3 +254,19 @@
|
|||||||
[values]
|
[values]
|
||||||
(let [s (set values)]
|
(let [s (set values)]
|
||||||
(if (= (count s) 1) (first s) "mixed")))
|
(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)))))
|
||||||
@@ -106,6 +106,11 @@
|
|||||||
(identical? (.getPrototypeOf js/Object o)
|
(identical? (.getPrototypeOf js/Object o)
|
||||||
(.-prototype js/Object)))))
|
(.-prototype js/Object)))))
|
||||||
|
|
||||||
|
#?(:cljs
|
||||||
|
(defn stringify
|
||||||
|
[obj]
|
||||||
|
(js/JSON.stringify obj)))
|
||||||
|
|
||||||
;; EXPERIMENTAL: unsafe, does not checks and not validates the input,
|
;; EXPERIMENTAL: unsafe, does not checks and not validates the input,
|
||||||
;; should be improved over time, for now it works for define a class
|
;; 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
|
;; extending js/Error that is more than enought for a first, quick and
|
||||||
|
|||||||
Reference in New Issue
Block a user