From f4f58bc1637f87420ff6ee5b793aad0a03d766ae Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 6 Jul 2022 15:39:25 +0200 Subject: [PATCH] :sparkles: Add parameters validation to binfile write-export! fn --- backend/src/app/rpc/commands/binfile.clj | 29 ++++++++++++++++++++---- backend/src/app/util/bytes.clj | 12 ++++++++++ common/src/app/common/data.cljc | 3 +++ common/src/app/common/spec.cljc | 27 ++++++++++++++++++++++ 4 files changed, 66 insertions(+), 5 deletions(-) diff --git a/backend/src/app/rpc/commands/binfile.clj b/backend/src/app/rpc/commands/binfile.clj index 621b8fae7..abdb44081 100644 --- a/backend/src/app/rpc/commands/binfile.clj +++ b/backend/src/app/rpc/commands/binfile.clj @@ -394,12 +394,31 @@ same file library all assets used from external libraries. " - [{:keys [pool storage ::output ::file-id ::include-libraries? ::embed-assets?]}] + [{:keys [pool storage ::output ::file-id ::include-libraries? ::embed-assets?] :as options}] - (when (and include-libraries? embed-assets?) - (ex/raise :type :restriction - :code :mutual-exclusive-options-provided - :hint "the `include-libraries?` option is mutually exclusive with `embed-assets?`")) + (us/assert! :spec ::db/pool :val pool) + (us/assert! :spec ::sto/storage :val storage) + + (us/assert! + :expr (uuid? file-id) + :hint "`file-id` should be an uuid") + + (us/assert! + :expr (bs/data-output-stream? output) + :hint "`output` should be an instance of OutputStream") + + (us/assert! + :expr (d/boolean-or-nil? include-libraries?) + :hint "invalid value provided for `include-libraries?` option, expected boolean") + + (us/assert! + :expr (d/boolean-or-nil? embed-assets?) + :hint "invalid value provided for `embed-assets?` option, expected boolean") + + (us/assert! + :always? true + :expr (not (and include-libraries? embed-assets?)) + :hint "the `include-libraries?` and `embed-assets?` are mutally excluding options") (let [libs (when include-libraries? (retrieve-libraries pool file-id)) rels (when include-libraries? (retrieve-library-relations pool (cons file-id libs))) diff --git a/backend/src/app/util/bytes.clj b/backend/src/app/util/bytes.clj index 5be58f405..1a36151ab 100644 --- a/backend/src/app/util/bytes.clj +++ b/backend/src/app/util/bytes.clj @@ -28,6 +28,18 @@ (def ^:const default-buffer-size (:xnio/buffer-size yt/defaults)) +(defn input-stream? + [s] + (instance? InputStream s)) + +(defn output-stream? + [s] + (instance? OutputStream s)) + +(defn data-output-stream? + [s] + (instance? DataOutputStream s)) + (defn copy! [src dst & {:keys [offset size buffer-size] :or {offset 0 buffer-size default-buffer-size}}] diff --git a/common/src/app/common/data.cljc b/common/src/app/common/data.cljc index 874ee56b9..baa0f2fe3 100644 --- a/common/src/app/common/data.cljc +++ b/common/src/app/common/data.cljc @@ -23,6 +23,9 @@ #?(:clj (:import linked.set.LinkedSet))) +(def boolean-or-nil? + (some-fn nil? boolean?)) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Data Structures ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/common/src/app/common/spec.cljc b/common/src/app/common/spec.cljc index 5fc4b8d9f..ffc24188e 100644 --- a/common/src/app/common/spec.cljc +++ b/common/src/app/common/spec.cljc @@ -261,6 +261,33 @@ message (str "spec verify: '" (pr-str spec) "'")] `(spec-assert* ~spec ~x ~message ~context))) +(defmacro assert! + "General purpose assertion macro." + [& {:keys [expr spec always? hint val]}] + (cond + (some? spec) + (let [context (if-let [nsdata (:ns &env)] + {:ns (str (:name nsdata)) + :name (pr-str spec) + :line (:line &env) + :file (:file (:meta nsdata))} + {:ns (str (ns-name *ns*)) + :name (pr-str spec) + :line (:line (meta &form))}) + message (or hint (str "spec assert: " (pr-str spec)))] + (when (or always? *assert*) + `(spec-assert* ~spec ~val ~message ~context))) + + (some? expr) + (let [message (or hint (str "expr assert: " (pr-str expr)))] + (when (or always? *assert*) + `(when-not ~expr + (ex/raise :type :assertion + :code :expr-validation + :hint ~message)))) + + :else nil)) + ;; --- Public Api (defn conform