From 15faa57e0106d0b87c2eaf0fb4df7c7993332ce2 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 9 Sep 2024 09:37:43 +0200 Subject: [PATCH 1/2] :bug: Fix decoding on sm/set schema --- common/src/app/common/schema.cljc | 95 +++++++++++++++++++------------ frontend/src/app/util/router.cljs | 2 +- 2 files changed, 59 insertions(+), 38 deletions(-) diff --git a/common/src/app/common/schema.cljc b/common/src/app/common/schema.cljc index c741ee6829..493311af2c 100644 --- a/common/src/app/common/schema.cljc +++ b/common/src/app/common/schema.cljc @@ -476,7 +476,7 @@ ::oapi/type "string" ::oapi/format "email"}}) -(def non-empty-strings-xf +(def xf:filter-word-strings (comp (filter string?) (remove str/empty?) @@ -489,11 +489,8 @@ :min 0 :max 1 :compile - (fn [{:keys [coerce kind max min] :as props} children _] - (let [xform (if coerce - (comp non-empty-strings-xf (map coerce)) - non-empty-strings-xf) - kind (or (last children) kind) + (fn [{:keys [kind max min] :as props} children _] + (let [kind (or (last children) kind) pred (cond @@ -501,9 +498,6 @@ (nil? kind) any? :else (validator kind)) - encode-child - (encoder kind string-transformer) - pred (cond (and max min) @@ -531,31 +525,49 @@ (fn [value] (every? pred value))) - decode + + decode-string-child + (decoder kind string-transformer) + + decode-string (fn [v] - (let [v (if (string? v) (str/split v #"[\s,]+") v)] - (into #{} xform v))) + (let [v (if (string? v) (str/split v #"[\s,]+") v) + x (comp xf:filter-word-strings (map decode-string-child))] + (into #{} x v))) + + decode-json-child + (decoder kind json-transformer) + + decode-json + (fn [v] + (let [v (if (string? v) (str/split v #"[\s,]+") v) + x (comp xf:filter-word-strings (map decode-json-child))] + (into #{} x v))) + + encode-string-child + (encoder kind string-transformer) + + encode-string + (fn [o] + (if (set? o) + (str/join ", " (map encode-string-child o)) + o)) encode-json (fn [o] (if (set? o) (vec o) - o)) - - encode-string - (fn [o] - (if (set? o) - (str/join ", " (map encode-child o)) o))] + {:pred pred :type-properties {:title "set" :description "Set of Strings" :error/message "should be a set of strings" :gen/gen (-> kind sg/generator sg/set) - :decode/string decode - :decode/json decode + :decode/string decode-string + :decode/json decode-json :encode/string encode-string :encode/json encode-json ::oapi/type "array" @@ -569,21 +581,14 @@ :min 0 :max 1 :compile - (fn [{:keys [coerce kind max min] :as props} children _] - (let [xform (if coerce - (comp non-empty-strings-xf (map coerce)) - non-empty-strings-xf) - - kind (or (last children) kind) + (fn [{:keys [kind max min] :as props} children _] + (let [kind (or (last children) kind) pred (cond (fn? kind) kind (nil? kind) any? :else (validator kind)) - encode-child - (encoder kind string-transformer) - pred (cond (and max min) @@ -611,15 +616,31 @@ (fn [value] (every? pred value))) - decode + decode-string-child + (decoder kind string-transformer) + + decode-json-child + (decoder kind json-transformer) + + decode-string (fn [v] - (let [v (if (string? v) (str/split v #"[\s,]+") v)] - (into [] xform v))) + (let [v (if (string? v) (str/split v #"[\s,]+") v) + x (comp xf:filter-word-strings (map decode-string-child))] + (into #{} x v))) + + decode-json + (fn [v] + (let [v (if (string? v) (str/split v #"[\s,]+") v) + x (comp xf:filter-word-strings (map decode-json-child))] + (into #{} x v))) + + encode-string-child + (encoder kind string-transformer) encode-string (fn [o] (if (vector? o) - (str/join ", " (map encode-child o)) + (str/join ", " (map encode-string-child o)) o))] {:pred pred @@ -628,8 +649,8 @@ :description "Set of Strings" :error/message "should be a set of strings" :gen/gen (-> kind sg/generator sg/set) - :decode/string decode - :decode/json decode + :decode/string decode-string + :decode/json decode-json :encode/string encode-string ::oapi/type "array" ::oapi/format "set" @@ -646,7 +667,7 @@ :gen/gen (-> :string sg/generator sg/set) :decode/string (fn [v] (let [v (if (string? v) (str/split v #"[\s,]+") v)] - (into #{} non-empty-strings-xf v))) + (into #{} xf:filter-word-strings v))) ::oapi/type "array" ::oapi/format "set" ::oapi/items {:type "string"} @@ -662,7 +683,7 @@ :gen/gen (-> :keyword sg/generator sg/set) :decode/string (fn [v] (let [v (if (string? v) (str/split v #"[\s,]+") v)] - (into #{} (comp non-empty-strings-xf (map keyword)) v))) + (into #{} (comp xf:filter-word-strings (map keyword)) v))) ::oapi/type "array" ::oapi/format "set" ::oapi/items {:type "string" :format "keyword"} diff --git a/frontend/src/app/util/router.cljs b/frontend/src/app/util/router.cljs index 221c4b4997..b556bd84b3 100644 --- a/frontend/src/app/util/router.cljs +++ b/frontend/src/app/util/router.cljs @@ -13,7 +13,7 @@ [app.main.data.events :as ev] [app.util.browser-history :as bhistory] [app.util.dom :as dom] - [app.util.globals :as globals] + [app.util.globals :as globals] [app.util.timers :as ts] [beicon.v2.core :as rx] [cuerdas.core :as str] From 1c69a9fd8ae68b3fb7eb958171d4002fb265ad22 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 9 Sep 2024 09:38:07 +0200 Subject: [PATCH 2/2] :bug: Fix config parsing on exporter --- exporter/src/app/config.cljs | 44 +++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/exporter/src/app/config.cljs b/exporter/src/app/config.cljs index 4c00880776..6ca84f584c 100644 --- a/exporter/src/app/config.cljs +++ b/exporter/src/app/config.cljs @@ -26,16 +26,24 @@ (def ^:private schema:config - (sm/define - [:map {:title "config"} - [:public-uri {:optional true} ::sm/uri] - [:host {:optional true} :string] - [:tenant {:optional true} :string] - [:flags {:optional true} ::sm/set-of-keywords] - [:redis-uri {:optional true} :string] - [:tempdir {:optional true} :string] - [:browser-pool-max {:optional true} :int] - [:browser-pool-min {:optional true} :int]])) + [:map {:title "config"} + [:public-uri {:optional true} ::sm/uri] + [:host {:optional true} :string] + [:tenant {:optional true} :string] + [:flags {:optional true} [::sm/set :keyword]] + [:redis-uri {:optional true} :string] + [:tempdir {:optional true} :string] + [:browser-pool-max {:optional true} ::sm/int] + [:browser-pool-min {:optional true} ::sm/int]]) + +(def ^:private decode-config + (sm/decoder schema:config sm/string-transformer)) + +(def ^:private explain-config + (sm/explainer schema:config)) + +(def ^:private valid-config? + (sm/validator schema:config)) (defn- parse-flags [config] @@ -60,15 +68,15 @@ [] (let [env (read-env "penpot") env (d/without-nils env) - data (merge defaults env)] + data (merge defaults env) + data (decode-config data)] - (try - (sm/conform! schema:config data) - (catch :default cause - (if-let [explain (some->> cause ex-data ::sm/explain)] - (println (sm/humanize-explain explain)) - (js/console.error cause)) - (process/exit -1))))) + (when-not (valid-config? data) + (let [explain (explain-config data)] + (println (sm/humanize-explain explain)) + (process/exit -1))) + + data)) (def config (prepare-config))