0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-13 02:28:18 -05:00

Add parameters validation to binfile write-export! fn

This commit is contained in:
Andrey Antukh 2022-07-06 15:39:25 +02:00
parent d90b4370fb
commit f4f58bc163
4 changed files with 66 additions and 5 deletions

View file

@ -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)))

View file

@ -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}}]

View file

@ -23,6 +23,9 @@
#?(:clj
(:import linked.set.LinkedSet)))
(def boolean-or-nil?
(some-fn nil? boolean?))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Data Structures
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

View file

@ -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