From 0ea623487ca7103cd3557ae8131374176b328116 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 29 Jan 2024 18:31:55 +0100 Subject: [PATCH] :sparkles: Add better validation for point, matrix and rect types --- common/src/app/common/geom/matrix.cljc | 13 +++++++++---- common/src/app/common/geom/point.cljc | 18 ++++++++++------- common/src/app/common/geom/rect.cljc | 27 ++++++++++++++++++++++++++ common/src/app/common/types/shape.cljc | 23 ++-------------------- 4 files changed, 49 insertions(+), 32 deletions(-) diff --git a/common/src/app/common/geom/matrix.cljc b/common/src/app/common/geom/matrix.cljc index 52efef50e..d435d861c 100644 --- a/common/src/app/common/geom/matrix.cljc +++ b/common/src/app/common/geom/matrix.cljc @@ -67,7 +67,8 @@ ([a b c d e f] (pos->Matrix a b c d e f))) -(def number-regex #"[+-]?\d*(\.\d+)?(e[+-]?\d+)?") +(def number-regex + #"[+-]?\d*(\.\d+)?([eE][+-]?\d+)?") (defn str->matrix [matrix-str] @@ -76,8 +77,8 @@ (map (comp d/parse-double first)))] (apply matrix params))) -(sm/def! ::matrix-map - [:map {:title "MatrixMap"} +(def ^:private schema:matrix-attrs + [:map {:title "MatrixAttrs"} [:a ::sm/safe-double] [:b ::sm/safe-double] [:c ::sm/safe-double] @@ -85,6 +86,10 @@ [:e ::sm/safe-double] [:f ::sm/safe-double]]) +(def valid-matrix? + (sm/lazy-validator + [:and [:fn matrix?] schema:matrix-attrs])) + (sm/def! ::matrix (letfn [(decode [o] (if (map? o) @@ -101,7 +106,7 @@ (dm/get-prop o :f) ","))] {:type ::matrix - :pred matrix? + :pred valid-matrix? :type-properties {:title "matrix" :description "Matrix instance" diff --git a/common/src/app/common/geom/point.cljc b/common/src/app/common/geom/point.cljc index fbe7e8e41..0a04fa747 100644 --- a/common/src/app/common/geom/point.cljc +++ b/common/src/app/common/geom/point.cljc @@ -41,12 +41,6 @@ [v] (instance? Point v)) -(sm/def! ::point-map - [:map {:title "PointMap"} - [:x ::sm/safe-number] - [:y ::sm/safe-number]]) - - ;; FIXME: deprecated (s/def ::x ::us/safe-number) (s/def ::y ::us/safe-number) @@ -57,6 +51,16 @@ (s/def ::point (s/and ::point-attrs point?)) + +(def ^:private schema:point-attrs + [:map {:title "PointAttrs"} + [:x ::sm/safe-number] + [:y ::sm/safe-number]]) + +(def valid-point? + (sm/lazy-validator + [:and [:fn point?] schema:point-attrs])) + (sm/def! ::point (letfn [(decode [p] (if (map? p) @@ -71,7 +75,7 @@ (dm/get-prop p :y)))] {:type ::point - :pred point? + :pred valid-point? :type-properties {:title "point" :description "Point" diff --git a/common/src/app/common/geom/rect.cljc b/common/src/app/common/geom/rect.cljc index 445eb06cc..ce01fb0cb 100644 --- a/common/src/app/common/geom/rect.cljc +++ b/common/src/app/common/geom/rect.cljc @@ -12,6 +12,8 @@ [app.common.geom.point :as gpt] [app.common.math :as mth] [app.common.record :as rc] + [app.common.schema :as sm] + [app.common.schema.generators :as sg] [app.common.transit :as t])) (rc/defrecord Rect [x y width height x1 y1 x2 y2]) @@ -66,6 +68,31 @@ h (mth/max height 0.01)] (pos->Rect x y w h x y (+ x w) (+ y h)))))) +(def ^:private schema:rect-attrs + [:map {:title "RectAttrs"} + [:x ::sm/safe-number] + [:y ::sm/safe-number] + [:width ::sm/safe-number] + [:height ::sm/safe-number] + [:x1 ::sm/safe-number] + [:y1 ::sm/safe-number] + [:x2 ::sm/safe-number] + [:y2 ::sm/safe-number]]) + +(sm/define! ::rect + [:and + {:gen/gen (->> (sg/tuple (sg/small-double) + (sg/small-double) + (sg/small-double) + (sg/small-double)) + (sg/fmap #(apply make-rect %)))} + [:fn rect?] + schema:rect-attrs]) + +(def valid-rect? + (sm/lazy-validator + [:and [:fn rect?] schema:rect-attrs])) + (def empty-rect (make-rect 0 0 0.01 0.01)) diff --git a/common/src/app/common/types/shape.cljc b/common/src/app/common/types/shape.cljc index cdc7ccb16..46c2074b6 100644 --- a/common/src/app/common/types/shape.cljc +++ b/common/src/app/common/types/shape.cljc @@ -79,25 +79,6 @@ (def text-align-types #{"left" "right" "center" "justify"}) -(sm/define! ::selrect - [:and - {:title "Selrect" - :gen/gen (->> (sg/tuple (sg/small-double) - (sg/small-double) - (sg/small-double) - (sg/small-double)) - (sg/fmap #(apply grc/make-rect %)))} - [:fn grc/rect?] - [:map - [:x ::sm/safe-number] - [:y ::sm/safe-number] - [:x1 ::sm/safe-number] - [:x2 ::sm/safe-number] - [:y1 ::sm/safe-number] - [:y2 ::sm/safe-number] - [:width ::sm/safe-number] - [:height ::sm/safe-number]]]) - (sm/define! ::points [:vector {:gen/max 4 :gen/min 4} ::gpt/point]) @@ -133,7 +114,7 @@ [:id ::sm/uuid] [:name :string] [:type [::sm/one-of shape-types]] - [:selrect ::selrect] + [:selrect ::grc/rect] [:points ::points] [:transform ::gmt/matrix] [:transform-inverse ::gmt/matrix] @@ -156,7 +137,7 @@ [:main-instance {:optional true} :boolean] [:remote-synced {:optional true} :boolean] [:shape-ref {:optional true} ::sm/uuid] - [:selrect {:optional true} ::selrect] + [:selrect {:optional true} ::grc/rect] [:points {:optional true} ::points] [:blocked {:optional true} :boolean] [:collapsed {:optional true} :boolean]