0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-08 16:18:11 -05:00

Enhance validation script

This commit is contained in:
Andrés Moya 2023-09-21 10:16:30 +02:00
parent 003dec6c6b
commit 878f1d4090
3 changed files with 54 additions and 45 deletions

View file

@ -126,9 +126,12 @@
repair-shape repair-shape
(fn [shape] (fn [shape]
; Detach the shape and convert it to non instance. ;; ; Detach the shape and convert it to non instance.
(log/debug :hint " -> Detach shape" :shape-id (:id shape)) ;; (log/debug :hint " -> Detach shape" :shape-id (:id shape))
(ctk/detach-shape shape))] ;; (ctk/detach-shape shape))]
; There is no solution that may recover it with confidence
(log/warn :hint " -> CANNOT REPAIR THIS AUTOMATICALLY.")
shape)]
(log/info :hint "Repairing shape :component-not-found" :id (:id shape) :name (:name shape) :page-id page-id) (log/info :hint "Repairing shape :component-not-found" :id (:id shape) :name (:name shape) :page-id page-id)
(-> (pcb/empty-changes nil page-id) (-> (pcb/empty-changes nil page-id)
@ -315,9 +318,12 @@
[_ {:keys [shape page-id] :as error} file-data _] [_ {:keys [shape page-id] :as error} file-data _]
(let [repair-shape (let [repair-shape
(fn [shape] (fn [shape]
; Detach the shape and convert it to non instance. ;; ; Detach the shape and convert it to non instance.
(log/debug :hint " -> Detach shape" :shape-id (:id shape)) ;; (log/debug :hint " -> Detach shape" :shape-id (:id shape))
(ctk/detach-shape shape))] ;; (ctk/detach-shape shape))]
; There is no solution that may recover it with confidence
(log/warn :hint " -> CANNOT REPAIR THIS AUTOMATICALLY.")
shape)]
(log/info :hint "Repairing shape :not-head-main-not-allowed" :id (:id shape) :name (:name shape) :page-id page-id) (log/info :hint "Repairing shape :not-head-main-not-allowed" :id (:id shape) :name (:name shape) :page-id page-id)
(-> (pcb/empty-changes nil page-id) (-> (pcb/empty-changes nil page-id)

View file

@ -140,7 +140,7 @@
shape file page) shape file page)
(do (do
(when-not (= (:main-instance-id component) (:id shape)) (when-not (= (:main-instance-id component) (:id shape))
(report-error :nvalid-main-instance-id (report-error :invalid-main-instance-id
(str/format "Main instance id of component %s is not valid" (:component-id shape)) (str/format "Main instance id of component %s is not valid" (:component-id shape))
shape file page)) shape file page))
(when-not (= (:main-instance-page component) (:id page)) (when-not (= (:main-instance-page component) (:id page))
@ -317,57 +317,60 @@
(validate-parent-children shape file page) (validate-parent-children shape file page)
(validate-frame shape file page) (validate-frame shape file page)
(if (ctk/main-instance? shape) (validate-parent-children shape file page)
(validate-frame shape file page)
(if (ctk/instance-root? shape) (if (ctk/instance-head? shape)
(if (ctk/instance-root? shape)
(if (ctk/main-instance? shape)
(if (not= context :not-component) (if (not= context :not-component)
(report-error :root-main-not-allowed (report-error :root-main-not-allowed
(str/format "Root main component not allowed inside other component") (str/format "Root main component not allowed inside other component")
shape file page) shape file page)
(validate-shape-main-root-top shape file page libraries)) (validate-shape-main-root-top shape file page libraries))
(if (not= context :not-component)
(report-error :root-main-not-allowed
(str/format "Root main component not allowed inside other component")
shape file page)
(validate-shape-copy-root-top shape file page libraries)))
(if (ctk/main-instance? shape)
(if (= context :not-component) (if (= context :not-component)
(report-error :nested-main-not-allowed (report-error :nested-main-not-allowed
(str/format "Nested main component only allowed inside other component") (str/format "Nested main component only allowed inside other component")
shape file page) shape file page)
(validate-shape-main-root-nested shape file page libraries))) (validate-shape-main-root-nested shape file page libraries))
(if (ctk/instance-head? shape) (if (= context :not-component)
(report-error :nested-main-not-allowed
(str/format "Nested main component only allowed inside other component")
shape file page)
(validate-shape-copy-root-nested shape file page libraries))))
(if (ctk/instance-root? shape) (if (ctk/in-component-copy? shape)
(if (not= context :not-component) (if-not (#{:copy-top :copy-nested :copy-any} context)
(report-error :root-copy-not-allowed (report-error :not-head-copy-not-allowed
(str/format "Root copy not allowed inside other component") (str/format "Non-root copy only allowed inside a copy")
shape file page) shape file page)
(validate-shape-copy-root-top shape file page libraries)) (validate-shape-copy-not-root shape file page libraries))
(if (= context :not-component) (if (ctn/inside-component-main? (:objects page) shape)
(report-error :nested-copy-not-allowed (if-not (#{:main-top :main-nested :main-any} context)
(str/format "Nested copy only allowed inside other component") (report-error :not-head-main-not-allowed
shape file page) (str/format "Non-root main only allowed inside a main component")
(validate-shape-copy-root-nested shape file page libraries))) shape file page)
(validate-shape-main-not-root shape file page libraries))
(if (ctn/component-main? (:objects page) shape) (if (#{:main-top :main-nested :main-any} context)
(if-not (#{:main-top :main-nested :main-any} context) (report-error :not-component-not-allowed
(report-error :not-head-main-not-allowed (str/format "Not compoments are not allowed inside a main")
(str/format "Non-root main only allowed inside a main component") shape file page)
shape file page) (validate-shape-not-component shape file page libraries)))))
(validate-shape-main-not-root shape file page libraries))
(if (ctk/in-component-copy? shape) (deref *errors*))))
(if-not (#{:copy-top :copy-nested :copy-any} context)
(report-error :not-head-copy-not-allowed
(str/format "Non-root copy only allowed inside a copy")
shape file page)
(validate-shape-copy-not-root shape file page libraries))
(if (#{:main-top :main-nested :main-any} context)
(report-error :not-component-not-allowed
(str/format "Not compoments are not allowed inside a main")
shape file page)
(validate-shape-not-component shape file page libraries))))))
(deref *errors*))))
(defn validate-file (defn validate-file
"Validate referencial integrity and semantic coherence of all contents of a file." "Validate referencial integrity and semantic coherence of all contents of a file."

View file

@ -156,7 +156,7 @@
(let [parent (cph/get-parent objects (:id shape))] (let [parent (cph/get-parent objects (:id shape))]
(or (get-copy-root objects parent) shape)))) (or (get-copy-root objects parent) shape))))
(defn component-main? (defn inside-component-main?
"Check if the shape is a component main instance or is inside one." "Check if the shape is a component main instance or is inside one."
[objects shape] [objects shape]
(cond (cond
@ -167,7 +167,7 @@
(ctk/instance-head? shape) (ctk/instance-head? shape)
false false
:else :else
(component-main? objects (get objects (:parent-id shape))))) (inside-component-main? objects (get objects (:parent-id shape)))))
(defn in-any-component? (defn in-any-component?
"Check if the shape is part of any component (main or copy), wether it's "Check if the shape is part of any component (main or copy), wether it's
@ -175,7 +175,7 @@
[objects shape] [objects shape]
(or (ctk/in-component-copy? shape) (or (ctk/in-component-copy? shape)
(ctk/instance-head? shape) (ctk/instance-head? shape)
(component-main? objects shape))) (inside-component-main? objects shape)))
(defn make-component-shape (defn make-component-shape
"Clone the shape and all children. Generate new ids and detach "Clone the shape and all children. Generate new ids and detach