🐛 Fix incorrect validation of shape geom attrs

Requied validation in a subset of supported shapes
This commit is contained in:
Andrey Antukh 2024-01-19 15:51:52 +01:00
parent 35da01bac9
commit 3bbd2023a4
2 changed files with 51 additions and 34 deletions

View file

@ -483,6 +483,9 @@
(sm/lazy-explainer ::ctf/data)) (sm/lazy-explainer ::ctf/data))
(defn validate-file-schema! (defn validate-file-schema!
"Validates the file itself, without external dependencies, it
performs the schema checking and some semantical validation of the
content."
[{:keys [id data] :as file}] [{:keys [id data] :as file}]
(when-not (valid-fdata? data) (when-not (valid-fdata? data)
(ex/raise :type :validation (ex/raise :type :validation

View file

@ -127,21 +127,24 @@
[:stroke-color-gradient {:optional true} ::ctc/gradient] [:stroke-color-gradient {:optional true} ::ctc/gradient]
[:stroke-image {:optional true} ::ctc/image-color]]) [:stroke-image {:optional true} ::ctc/image-color]])
(sm/define! ::minimal-shape-attrs (sm/define! ::shape-base-attrs
[:map {:title "ShapeMinimalRecord"} [:map {:title "ShapeMinimalRecord"}
[:id {:optional false} ::sm/uuid] [:id ::sm/uuid]
[:name {:optional false} :string] [:name :string]
[:type {:optional false} [::sm/one-of shape-types]] [:type [::sm/one-of shape-types]]
[:x {:optional false} [:maybe ::sm/safe-number]] [:selrect ::selrect]
[:y {:optional false} [:maybe ::sm/safe-number]] [:points ::points]
[:width {:optional false} [:maybe ::sm/safe-number]] [:transform ::gmt/matrix]
[:height {:optional false} [:maybe ::sm/safe-number]] [:transform-inverse ::gmt/matrix]
[:selrect {:optional false} ::selrect] [:parent-id ::sm/uuid]
[:points {:optional false} ::points] [:frame-id ::sm/uuid]])
[:transform {:optional false} ::gmt/matrix]
[:transform-inverse {:optional false} ::gmt/matrix] (sm/define! ::shape-geom-attrs
[:parent-id {:optional false} ::sm/uuid] [:map {:title "ShapeGeometryAttrs"}
[:frame-id {:optional false} ::sm/uuid]]) [:x ::sm/safe-number]
[:y ::sm/safe-number]
[:width ::sm/safe-number]
[:height ::sm/safe-number]])
(sm/define! ::shape-attrs (sm/define! ::shape-attrs
[:map {:title "ShapeAttrs"} [:map {:title "ShapeAttrs"}
@ -273,74 +276,82 @@
[:multi {:dispatch :type :title "Shape"} [:multi {:dispatch :type :title "Shape"}
[:group [:group
[:and {:title "GroupShape"} [:and {:title "GroupShape"}
::shape-base-attrs
::shape-geom-attrs
::shape-attrs ::shape-attrs
::minimal-shape-attrs
::group-attrs ::group-attrs
::ctsl/layout-child-attrs]] ::ctsl/layout-child-attrs]]
[:frame [:frame
[:and {:title "FrameShape"} [:and {:title "FrameShape"}
::minimal-shape-attrs ::shape-base-attrs
::shape-geom-attrs
::frame-attrs ::frame-attrs
::ctsl/layout-attrs ::ctsl/layout-attrs
::ctsl/layout-child-attrs]] ::ctsl/layout-child-attrs]]
[:bool [:bool
[:and {:title "BoolShape"} [:and {:title "BoolShape"}
::shape-base-attrs
::shape-attrs ::shape-attrs
::minimal-shape-attrs
::bool-attrs ::bool-attrs
::ctsl/layout-child-attrs]] ::ctsl/layout-child-attrs]]
[:rect [:rect
[:and {:title "RectShape"} [:and {:title "RectShape"}
::shape-base-attrs
::shape-geom-attrs
::shape-attrs ::shape-attrs
::minimal-shape-attrs
::rect-attrs ::rect-attrs
::ctsl/layout-child-attrs]] ::ctsl/layout-child-attrs]]
[:circle [:circle
[:and {:title "CircleShape"} [:and {:title "CircleShape"}
::shape-base-attrs
::shape-geom-attrs
::shape-attrs ::shape-attrs
::minimal-shape-attrs
::circle-attrs ::circle-attrs
::ctsl/layout-child-attrs]] ::ctsl/layout-child-attrs]]
[:image [:image
[:and {:title "ImageShape"} [:and {:title "ImageShape"}
::shape-base-attrs
::shape-geom-attrs
::shape-attrs ::shape-attrs
::minimal-shape-attrs
::image-attrs ::image-attrs
::ctsl/layout-child-attrs]] ::ctsl/layout-child-attrs]]
[:svg-raw [:svg-raw
[:and {:title "SvgRawShape"} [:and {:title "SvgRawShape"}
::shape-base-attrs
::shape-geom-attrs
::shape-attrs ::shape-attrs
::minimal-shape-attrs
::svg-raw-attrs ::svg-raw-attrs
::ctsl/layout-child-attrs]] ::ctsl/layout-child-attrs]]
[:path [:path
[:and {:title "PathShape"} [:and {:title "PathShape"}
::shape-base-attrs
::shape-attrs ::shape-attrs
::minimal-shape-attrs
::path-attrs ::path-attrs
::ctsl/layout-child-attrs]] ::ctsl/layout-child-attrs]]
[:text [:text
[:and {:title "TextShape"} [:and {:title "TextShape"}
::shape-base-attrs
::shape-geom-attrs
::shape-attrs ::shape-attrs
::minimal-shape-attrs
::text-attrs ::text-attrs
::ctsl/layout-child-attrs]]]) ::ctsl/layout-child-attrs]]])
(sm/define! ::shape (sm/define! ::shape
[:and [:and
{:title "Shape" {:title "Shape"
:gen/gen (->> (sg/generator ::minimal-shape-attrs) :gen/gen (->> (sg/generator ::shape-base-attrs)
(sg/mcat (fn [{:keys [type] :as shape}] (sg/mcat (fn [{:keys [type] :as shape}]
(sg/let [attrs1 (sg/generator ::shape-attrs) (sg/let [attrs1 (sg/generator ::shape-attrs)
attrs2 (case type attrs2 (sg/generator ::shape-geom-attrs)
attrs3 (case type
:text (sg/generator ::text-attrs) :text (sg/generator ::text-attrs)
:path (sg/generator ::path-attrs) :path (sg/generator ::path-attrs)
:svg-raw (sg/generator ::svg-raw-attrs) :svg-raw (sg/generator ::svg-raw-attrs)
@ -350,7 +361,10 @@
:bool (sg/generator ::bool-attrs) :bool (sg/generator ::bool-attrs)
:group (sg/generator ::group-attrs) :group (sg/generator ::group-attrs)
:frame (sg/generator ::frame-attrs))] :frame (sg/generator ::frame-attrs))]
(merge attrs1 shape attrs2)))) (if (or (= type :path)
(= type :bool))
(merge attrs1 shape attrs3)
(merge attrs1 shape attrs2 attrs3)))))
(sg/fmap map->Shape))} (sg/fmap map->Shape))}
::shape-map ::shape-map
[:fn shape?]]) [:fn shape?]])