From 3224ba26f193c82548f4c9f3b9bfed6509c0b621 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 14 May 2025 17:25:23 +0200 Subject: [PATCH] :recycle: Replace :any schema with own ::sm/any That a more specific, json friendly generator --- backend/src/app/rpc/commands/audit.clj | 4 +- backend/src/app/rpc/commands/files.clj | 2 +- backend/src/app/rpc/commands/fonts.clj | 4 +- common/src/app/common/files/changes.cljc | 18 ++++---- .../src/app/common/files/changes_builder.cljc | 21 +++++----- common/src/app/common/schema.cljc | 2 + common/src/app/common/schema/generators.cljc | 42 +++++++++++++------ common/src/app/common/types/color.cljc | 2 +- common/src/app/common/types/tokens_lib.cljc | 5 ++- 9 files changed, 61 insertions(+), 39 deletions(-) diff --git a/backend/src/app/rpc/commands/audit.clj b/backend/src/app/rpc/commands/audit.clj index f43195dd72..a9bed1db44 100644 --- a/backend/src/app/rpc/commands/audit.clj +++ b/backend/src/app/rpc/commands/audit.clj @@ -92,9 +92,9 @@ [:string {:max 250}] [::sm/one-of {:format "string"} valid-event-types]]] [:props - [:map-of :keyword :any]] + [:map-of :keyword ::sm/any]] [:context {:optional true} - [:map-of :keyword :any]]]) + [:map-of :keyword ::sm/any]]]) (def schema:push-audit-events [:map {:title "push-audit-events"} diff --git a/backend/src/app/rpc/commands/files.clj b/backend/src/app/rpc/commands/files.clj index bdb2fcbc59..aff510d4cf 100644 --- a/backend/src/app/rpc/commands/files.clj +++ b/backend/src/app/rpc/commands/files.clj @@ -189,7 +189,7 @@ [:is-shared ::sm/boolean] [:project-id ::sm/uuid] [:created-at ::dt/instant] - [:data {:optional true} :any]]) + [:data {:optional true} ::sm/any]]) (def schema:permissions-mixin [:map {:title "PermissionsMixin"} diff --git a/backend/src/app/rpc/commands/fonts.clj b/backend/src/app/rpc/commands/fonts.clj index 43b90305e1..4c4e243669 100644 --- a/backend/src/app/rpc/commands/fonts.clj +++ b/backend/src/app/rpc/commands/fonts.clj @@ -80,9 +80,9 @@ (def ^:private schema:create-font-variant [:map {:title "create-font-variant"} [:team-id ::sm/uuid] - [:data [:map-of :string :any]] + [:data [:map-of ::sm/text ::sm/any]] [:font-id ::sm/uuid] - [:font-family :string] + [:font-family ::sm/text] [:font-weight [::sm/one-of {:format "number"} valid-weight]] [:font-style [::sm/one-of {:format "string"} valid-style]]]) diff --git a/common/src/app/common/files/changes.cljc b/common/src/app/common/files/changes.cljc index 023491a9ca..d3ee9f2180 100644 --- a/common/src/app/common/files/changes.cljc +++ b/common/src/app/common/files/changes.cljc @@ -47,14 +47,14 @@ [:type [:= :assign]] ;; NOTE: the full decoding is happening on the handler because it ;; needs a proper context of the current shape and its type - [:value [:map-of :keyword :any]] + [:value [:map-of :keyword ::sm/any]] [:ignore-touched {:optional true} :boolean] [:ignore-geometry {:optional true} :boolean]]] [:set [:map {:title "SetOperation"} [:type [:= :set]] [:attr :keyword] - [:val :any] + [:val ::sm/any] [:ignore-touched {:optional true} :boolean] [:ignore-geometry {:optional true} :boolean]]] [:set-touched @@ -238,9 +238,9 @@ [:component-id {:optional true} ::sm/uuid] [:ignore-touched {:optional true} :boolean] [:parent-id ::sm/uuid] - [:shapes :any] + [:shapes ::sm/any] [:index {:optional true} [:maybe :int]] - [:after-shape {:optional true} :any] + [:after-shape {:optional true} ::sm/any] [:component-swap {:optional true} :boolean]]] [:reorder-children @@ -250,14 +250,14 @@ [:component-id {:optional true} ::sm/uuid] [:ignore-touched {:optional true} :boolean] [:parent-id ::sm/uuid] - [:shapes :any]]] + [:shapes ::sm/any]]] [:add-page [:map {:title "AddPageChange"} [:type [:= :add-page]] [:id {:optional true} ::sm/uuid] [:name {:optional true} :string] - [:page {:optional true} :any]]] + [:page {:optional true} ::sm/any]]] [:mod-page [:map {:title "ModPageChange"} @@ -327,14 +327,14 @@ [:type [:= :add-component]] [:id ::sm/uuid] [:name :string] - [:shapes {:optional true} [:vector {:gen/max 3} :any]] + [:shapes {:optional true} [:vector {:gen/max 3} ::sm/any]] [:path {:optional true} :string]]] [:mod-component [:map {:title "ModCompoenentChange"} [:type [:= :mod-component]] [:id ::sm/uuid] - [:shapes {:optional true} [:vector {:gen/max 3} :any]] + [:shapes {:optional true} [:vector {:gen/max 3} ::sm/any]] [:name {:optional true} :string] [:variant-id {:optional true} ::sm/uuid] [:variant-properties {:optional true} [:vector ::ctv/variant-property]]]] @@ -411,7 +411,7 @@ [:set-tokens-lib [:map {:title "SetTokensLib"} [:type [:= :set-tokens-lib]] - [:tokens-lib :any]]] + [:tokens-lib ::sm/any]]] [:set-token-set [:map {:title "SetTokenSetChange"} diff --git a/common/src/app/common/files/changes_builder.cljc b/common/src/app/common/files/changes_builder.cljc index 692bde840c..8429503f63 100644 --- a/common/src/app/common/files/changes_builder.cljc +++ b/common/src/app/common/files/changes_builder.cljc @@ -25,18 +25,19 @@ ;; Auxiliary functions to help create a set of changes (undo + redo) -(sm/register! - ^{::sm/type ::changes} - [:map {:title "changes"} - [:redo-changes vector?] - [:undo-changes seq?] - [:origin {:optional true} any?] - [:save-undo? {:optional true} boolean?] - [:stack-undo? {:optional true} boolean?] - [:undo-group {:optional true} any?]]) +(def schema:changes + (sm/register! + ^{::sm/type ::changes} + [:map {:title "changes"} + [:redo-changes vector?] + [:undo-changes seq?] + [:origin {:optional true} ::sm/any] + [:save-undo? {:optional true} boolean?] + [:stack-undo? {:optional true} boolean?] + [:undo-group {:optional true} ::sm/any]])) (def check-changes! - (sm/check-fn ::changes)) + (sm/check-fn schema:changes)) (defn empty-changes ([origin page-id] diff --git a/common/src/app/common/schema.cljc b/common/src/app/common/schema.cljc index 241f484a0c..27a9ca2a21 100644 --- a/common/src/app/common/schema.cljc +++ b/common/src/app/common/schema.cljc @@ -1045,6 +1045,8 @@ {:title "agent" :description "instance of clojure agent"}})) +(register! ::any (mu/update-properties :any assoc :gen/gen sg/any)) + ;; ---- PREDICATES (def valid-safe-number? diff --git a/common/src/app/common/schema/generators.cljc b/common/src/app/common/schema/generators.cljc index 57bc3703f6..2f90ab2260 100644 --- a/common/src/app/common/schema/generators.cljc +++ b/common/src/app/common/schema/generators.cljc @@ -5,7 +5,7 @@ ;; Copyright (c) KALEIDOS INC (ns app.common.schema.generators - (:refer-clojure :exclude [set subseq uuid filter map let boolean]) + (:refer-clojure :exclude [set subseq uuid filter map let boolean vector keyword int double]) #?(:cljs (:require-macros [app.common.schema.generators])) (:require [app.common.schema.registry :as sr] @@ -38,10 +38,6 @@ ([s opts] (mg/generator s (assoc opts :registry sr/default-registry)))) -(defn filter - [pred gen] - (tg/such-that pred gen 100)) - (defn small-double [& {:keys [min max] :or {min -100 max 100}}] (tg/double* {:min min, :max max, :infinite? false, :NaN? false})) @@ -61,7 +57,7 @@ (defn word-keyword [] (->> (word-string) - (tg/fmap keyword))) + (tg/fmap c/keyword))) (defn email [] @@ -100,12 +96,11 @@ (c/map second)) (c/map list bools elements))))))) -(def any tg/any) -(def boolean tg/boolean) - -(defn set - [g] - (tg/set g)) +(defn map-of + ([kg vg] + (tg/map kg vg {:min-elements 1 :max-elements 3})) + ([kg vg opts] + (tg/map kg vg opts))) (defn elements [s] @@ -119,6 +114,10 @@ [f g] (tg/fmap f g)) +(defn filter + [pred gen] + (tg/such-that pred gen 100)) + (defn mcat [f g] (tg/bind g f)) @@ -126,3 +125,22 @@ (defn tuple [& opts] (apply tg/tuple opts)) + +(defn vector + [& opts] + (apply tg/vector opts)) + +(defn set + [g] + (tg/set g)) + +;; Static Generators + +(def boolean tg/boolean) +(def text (word-string)) +(def double (small-double)) +(def int (small-int)) +(def keyword (word-keyword)) + +(def any + (tg/one-of [text boolean double int keyword])) diff --git a/common/src/app/common/types/color.cljc b/common/src/app/common/types/color.cljc index f93f756174..29ea0b08c1 100644 --- a/common/src/app/common/types/color.cljc +++ b/common/src/app/common/types/color.cljc @@ -35,7 +35,7 @@ (.. r (toString 16) (padStart 2 "0")) (.. g (toString 16) (padStart 2 "0")) (.. b (toString 16) (padStart 2 "0")))))) - sg/any)) + sg/int)) (defn rgb-color-string? [o] diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index ffed9c3949..5e2d17ddf8 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -118,7 +118,7 @@ [:map {:title "Token"} [:name cto/token-name-ref] [:type [::sm/one-of cto/token-types]] - [:value :any] + [:value ::sm/any] [:description {:optional true} :string] [:modified-at {:optional true} ::sm/inst]]) @@ -389,7 +389,8 @@ [:description {:optional true} :string] [:modified-at {:optional true} ::sm/inst] [:tokens {:optional true - :gen/gen (->> (sg/generator [:map-of ::sm/text schema:token]) + :gen/gen (->> (sg/map-of (sg/generator ::sm/text) + (sg/generator schema:token)) (sg/fmap #(into (d/ordered-map) %)))} [:and [:map-of {:gen/max 5