mirror of
https://github.com/penpot/penpot.git
synced 2025-05-18 21:26:11 +02:00
🐛 Add validation and repair for files with nil in component :objects
This commit is contained in:
parent
0eb66464ab
commit
6c1c780758
4 changed files with 53 additions and 9 deletions
|
@ -271,11 +271,24 @@
|
||||||
(let [nearest-frame (cph/get-frame objects (:parent-id shape))
|
(let [nearest-frame (cph/get-frame objects (:parent-id shape))
|
||||||
frame-id (or (:id nearest-frame) uuid/zero)]
|
frame-id (or (:id nearest-frame) uuid/zero)]
|
||||||
(update objects (:id shape) assoc :frame-id frame-id))
|
(update objects (:id shape) assoc :frame-id frame-id))
|
||||||
objects)))]
|
objects)))]
|
||||||
|
|
||||||
(-> file-data
|
(-> file-data
|
||||||
(update :pages-index update-vals fix-container)
|
(update :pages-index update-vals fix-container)
|
||||||
(update :components update-vals fix-container))))]
|
(update :components update-vals fix-container))))
|
||||||
|
|
||||||
|
fix-component-nil-objects
|
||||||
|
(fn [file-data]
|
||||||
|
;; Ensure that objects of all components is not null
|
||||||
|
(letfn [(fix-component
|
||||||
|
[component]
|
||||||
|
(if (and (contains? component :objects) (nil? (:objects component)))
|
||||||
|
(if (:deleted component)
|
||||||
|
(assoc component :objects [])
|
||||||
|
(dissoc component :objects))
|
||||||
|
component))]
|
||||||
|
(-> file-data
|
||||||
|
(update :components update-vals fix-component))))]
|
||||||
|
|
||||||
(-> file-data
|
(-> file-data
|
||||||
(fix-orphan-shapes)
|
(fix-orphan-shapes)
|
||||||
|
@ -286,7 +299,8 @@
|
||||||
(fix-copies-of-detached)
|
(fix-copies-of-detached)
|
||||||
(transform-to-frames)
|
(transform-to-frames)
|
||||||
(remap-frame-ids)
|
(remap-frame-ids)
|
||||||
(fix-frame-ids))))
|
(fix-frame-ids)
|
||||||
|
(fix-component-nil-objects))))
|
||||||
|
|
||||||
(defn- migrate-components
|
(defn- migrate-components
|
||||||
"If there is any component in the file library, add a new 'Library
|
"If there is any component in the file library, add a new 'Library
|
||||||
|
|
|
@ -728,7 +728,7 @@
|
||||||
:main-instance-id (:main-instance-id new-component)
|
:main-instance-id (:main-instance-id new-component)
|
||||||
:main-instance-page (:main-instance-page new-component)
|
:main-instance-page (:main-instance-page new-component)
|
||||||
:annotation (:annotation new-component)
|
:annotation (:annotation new-component)
|
||||||
:objects (:objects new-component)}) ;; this won't exist in components-v2
|
:objects (:objects new-component)}) ;; this won't exist in components-v2 (except for deleted components)
|
||||||
(update :undo-changes conj {:type :mod-component
|
(update :undo-changes conj {:type :mod-component
|
||||||
:id id
|
:id id
|
||||||
:name (:name prev-component)
|
:name (:name prev-component)
|
||||||
|
|
|
@ -404,6 +404,24 @@
|
||||||
(pcb/with-file-data file-data)
|
(pcb/with-file-data file-data)
|
||||||
(pcb/update-shapes [(:id shape)] repair-shape))))
|
(pcb/update-shapes [(:id shape)] repair-shape))))
|
||||||
|
|
||||||
|
(defmethod repair-error :component-nil-objects-not-allowed
|
||||||
|
[_ {:keys [shape] :as error} file-data _]
|
||||||
|
(let [repair-component
|
||||||
|
(fn [component]
|
||||||
|
; Remove the objects key, or set it to [] if the component is deleted
|
||||||
|
(if (:deleted component)
|
||||||
|
(do
|
||||||
|
(log/debug :hint " -> Set :objects []")
|
||||||
|
(assoc component :objects []))
|
||||||
|
(do
|
||||||
|
(log/debug :hint " -> Remove :objects")
|
||||||
|
(dissoc component :objects))))]
|
||||||
|
|
||||||
|
(log/info :hint "Repairing component :component-nil-objects-not-allowed" :id (:id shape) :name (:name shape))
|
||||||
|
(-> (pcb/empty-changes nil)
|
||||||
|
(pcb/with-library-data file-data)
|
||||||
|
(pcb/update-component (:id shape) repair-component))))
|
||||||
|
|
||||||
(defmethod repair-error :default
|
(defmethod repair-error :default
|
||||||
[_ error file _]
|
[_ error file _]
|
||||||
(log/error :hint "Unknown error code, don't know how to repair" :code (:code error))
|
(log/error :hint "Unknown error code, don't know how to repair" :code (:code error))
|
||||||
|
|
|
@ -413,6 +413,15 @@
|
||||||
(validate-shape! shape-id file page libraries)
|
(validate-shape! shape-id file page libraries)
|
||||||
(deref *errors*)))
|
(deref *errors*)))
|
||||||
|
|
||||||
|
(defn validate-component!
|
||||||
|
" Validate semantic coherence of a component. Raises an exception
|
||||||
|
on first error found."
|
||||||
|
[component file]
|
||||||
|
(when (and (contains? component :objects) (nil? (:objects component)))
|
||||||
|
(report-error! :component-nil-objects-not-allowed
|
||||||
|
"Objects list cannot be nil"
|
||||||
|
component file nil)))
|
||||||
|
|
||||||
(def valid-fdata?
|
(def valid-fdata?
|
||||||
"Structural validation of file data using defined schema"
|
"Structural validation of file data using defined schema"
|
||||||
(sm/lazy-validator ::ctf/data))
|
(sm/lazy-validator ::ctf/data))
|
||||||
|
@ -439,14 +448,17 @@
|
||||||
:file-id id
|
:file-id id
|
||||||
::sm/explain (get-fdata-explain data)))
|
::sm/explain (get-fdata-explain data)))
|
||||||
|
|
||||||
;; If `libraries` is provided, this means the fill file
|
;; If `libraries` is provided, this means the full file
|
||||||
;; validation is activated so we proceed to execute the
|
;; validation is activated so we proceed to execute the
|
||||||
;; validation
|
;; validation
|
||||||
(when (seq libraries)
|
(when (some? libraries)
|
||||||
(doseq [page (filter :id (ctpl/pages-seq data))]
|
(when (:components data)
|
||||||
(validate-shape! uuid/zero file page libraries)))
|
(doseq [component (vals (:components data))]
|
||||||
|
(validate-component! component file)))
|
||||||
|
(doseq [page (filter :id (ctpl/pages-seq data))]
|
||||||
|
(validate-shape! uuid/zero file page libraries)))
|
||||||
|
|
||||||
file))
|
file))
|
||||||
|
|
||||||
(defn validate-file
|
(defn validate-file
|
||||||
"Validate referencial integrity and semantic coherence of
|
"Validate referencial integrity and semantic coherence of
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue