🔥 Remove redundant schemas (and add some tooling)

This commit is contained in:
Andrés Moya 2025-04-23 10:25:33 +02:00 committed by Andrés Moya
parent 2f20ccf289
commit c0315e2c30
6 changed files with 211 additions and 207 deletions

View file

@ -26,8 +26,6 @@
[app.common.types.pages-list :as ctpl]
[app.common.types.shape :as cts]
[app.common.types.shape-tree :as ctst]
[app.common.types.token :as cto]
[app.common.types.token-theme :as ctot]
[app.common.types.tokens-lib :as ctob]
[app.common.types.typographies-list :as ctyl]
[app.common.types.typography :as ctt]
@ -408,7 +406,7 @@
[:type [:= :set-token-theme]]
[:theme-name :string]
[:group :string]
[:theme [:maybe ::ctot/token-theme]]]]
[:theme [:maybe ctob/schema:token-theme-attrs]]]]
[:set-tokens-lib
[:map {:title "SetTokensLib"}
@ -420,14 +418,14 @@
[:type [:= :set-token-set]]
[:set-name :string]
[:group? :boolean]
[:token-set [:maybe ::ctot/token-set]]]]
[:token-set [:maybe ctob/schema:token-set-attrs]]]]
[:set-token
[:map {:title "SetTokenChange"}
[:type [:= :set-token]]
[:set-name :string]
[:token-name :string]
[:token [:maybe ::cto/token]]]]]])
[:token [:maybe ctob/schema:token-attrs]]]]]])
(def schema:changes
[:sequential {:gen/max 5 :gen/min 1} schema:change])

View file

@ -113,6 +113,10 @@
[schema]
(mu/optional-keys schema default-options))
(defn required-keys
[schema]
(mu/required-keys schema default-options))
(defn transformer
[& transformers]
(apply mt/transformer transformers))
@ -145,11 +149,30 @@
;; :else
;; o))
(defn -transform-map-keys
([f]
(let [xform (map (fn [[k v]] [(f k) v]))]
#(cond->> % (map? %) (into (empty %) xform))))
([ks f]
(let [xform (map (fn [[k v]] [(cond-> k (contains? ks k) f) v]))]
#(cond->> % (map? %) (into (empty %) xform)))))
(defn json-transformer
[]
(mt/transformer
(mt/json-transformer)
(mt/collection-transformer)))
(let [map-of-key-decoders (mt/-string-decoders)]
(mt/transformer
{:name :json
:decoders (-> (mt/-json-decoders)
(assoc :map-of {:compile (fn [schema _]
(let [key-schema (some-> schema (m/children) (first))]
(or (some-> key-schema (m/type) map-of-key-decoders
(mt/-interceptor schema {}) (m/-intercepting)
(m/-comp m/-keyword->string)
(mt/-transform-if-valid key-schema)
(-transform-map-keys))
(-transform-map-keys m/-keyword->string))))}))
:encoders (mt/-json-encoders)}
(mt/collection-transformer))))
(defn string-transformer
[]
@ -874,7 +897,7 @@
{:title "inst"
:description "Satisfies Inst protocol"
:error/message "should be an instant"
:gen/gen (->> (sg/small-int)
:gen/gen (->> (sg/small-int :min 0 :max 100000)
(sg/fmap (fn [v] (tm/parse-instant v))))
:decode/string tm/parse-instant

View file

@ -66,16 +66,6 @@
[n]
(string? n))
;; TODO Move this to tokens-lib
(sm/register!
^{::sm/type ::token}
[:map {:title "Token"}
[:name token-name-ref]
[:type [::sm/one-of token-types]]
[:value :any]
[:description {:optional true} [:maybe :string]]
[:modified-at {:optional true} ::sm/inst]])
(sm/register!
^{::sm/type ::color}
[:map

View file

@ -1,28 +0,0 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC
(ns app.common.types.token-theme
(:require
[app.common.schema :as sm]))
(sm/register!
^{::sm/type ::token-theme}
[:map {:title "TokenTheme"}
[:name :string]
[:group :string]
[:description [:maybe :string]]
[:is-source :boolean]
[:id :string]
[:modified-at {:optional true} ::sm/inst]
[:sets :any]])
(sm/register!
^{::sm/type ::token-set}
[:map {:title "TokenSet"}
[:name :string]
[:description {:optional true} [:maybe :string]]
[:modified-at {:optional true} ::sm/inst]
[:tokens {:optional true} :any]])

View file

@ -11,6 +11,7 @@
[app.common.data.macros :as dm]
[app.common.files.helpers :as cfh]
[app.common.schema :as sm]
[app.common.schema.generators :as sg]
[app.common.time :as dt]
[app.common.transit :as t]
[app.common.types.token :as cto]
@ -118,12 +119,15 @@
[:name cto/token-name-ref]
[:type [::sm/one-of cto/token-types]]
[:value :any]
[:description [:maybe :string]]
[:modified-at ::sm/inst]])
[:description {:optional true} :string]
[:modified-at {:optional true} ::sm/inst]])
(declare make-token)
(def schema:token
[:and
schema:token-attrs
[:and {:gen/gen (->> (sg/generator schema:token-attrs)
(sg/fmap #(make-token %)))}
(sm/required-keys schema:token-attrs)
[:fn token?]])
(def check-token
@ -382,15 +386,32 @@
(def schema:token-set-attrs
[:map {:title "TokenSet"}
[:name :string]
[:description [:maybe :string]]
[:modified-at ::sm/inst]
[:tokens [:and
[:map-of {:gen/max 5} :string schema:token]
[:fn d/ordered-map?]]]])
[:description {:optional true} :string]
[:modified-at {:optional true} ::sm/inst]
[:tokens {:optional true
:gen/gen (->> (sg/generator [:map-of ::sm/text schema:token])
(sg/fmap #(into (d/ordered-map) %)))}
[:and
[:map-of {:gen/max 5
:decode/json (fn [v]
(cond
(d/ordered-map? v)
v
(map? v)
(into (d/ordered-map) v)
:else
v))}
:string schema:token]
[:fn d/ordered-map?]]]])
(declare make-token-set)
(def schema:token-set
[:and
schema:token-set-attrs
[:and {:gen/gen (->> (sg/generator schema:token-set-attrs)
(sg/fmap #(make-token-set %)))}
(sm/required-keys schema:token-set-attrs)
[:fn token-set?]])
(sm/register! ::token-set schema:token-set)
@ -554,16 +575,16 @@
(def schema:token-theme-attrs
[:map {:title "TokenTheme"}
[:name :string]
[:group :string]
[:description [:maybe :string]]
[:is-source [:maybe :boolean]]
[:id :string]
[:modified-at ::sm/inst]
[:sets [:set {:gen/max 5} :string]]])
[:group {:optional true} :string]
[:description {:optional true} :string]
[:is-source {:optional true} :boolean]
[:id {:optional true} :string]
[:modified-at {:optional true} ::sm/inst]
[:sets {:optional true} [:set {:gen/max 5} :string]]])
(def schema:token-theme
[:and
schema:token-theme-attrs
(sm/required-keys schema:token-theme-attrs)
[:fn token-theme?]])
(sm/register! ::token-theme schema:token-theme)

File diff suppressed because it is too large Load diff