🐛 Fix stroke width validation (#6124)

This commit is contained in:
Eva Marco 2025-03-20 17:35:19 +01:00 committed by GitHub
parent f98009ec54
commit b3a3cca9fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 49 additions and 5 deletions

View file

@ -44,6 +44,10 @@
{:error/code :error.style-dictionary/invalid-token-value-opacity
:error/fn #(str/join "\n" [(str (tr "workspace.token.invalid-value" %) ".") (tr "workspace.token.opacity-range")])}
:error.style-dictionary/invalid-token-value-stroke-width
{:error/code :error.style-dictionary/invalid-token-value-stroke-width
:error/fn #(str/join "\n" [(str (tr "workspace.token.invalid-value" %) ".") (tr "workspace.token.stroke-width-range")])}
:error/unknown
{:error/code :error/unknown
:error/fn #(tr "labels.unknown-error")}})

View file

@ -87,8 +87,34 @@
(and (not has-references?) out-of-scope)
{:errors [(wte/error-with-value :error.style-dictionary/invalid-token-value-opacity value)]}
(and has-references? out-of-scope)
(assoc parsed-value :warnings [(wtw/warning-with-value :warning.style-dictionary/invalid-referenced-token-value value)])
(and has-references? out-of-scope parsed-value)
(assoc parsed-value :warnings [(wtw/warning-with-value :warning.style-dictionary/invalid-referenced-token-value-opacity value)])
:else {:errors [(wte/error-with-value :error.style-dictionary/invalid-token-value value)]})))
(defn- parse-sd-token-stroke-width-value
"Parses `value` of a dimensions `sd-token` into a map like `{:value 1 :unit \"px\"}`.
If the `value` is not parseable and/or has missing references returns a map with `:errors`.
If the `value` is parseable but is out of range returns a map with `warnings`."
[value has-references?]
(let [parsed-value (wtt/parse-token-value value)
out-of-scope (< (:value parsed-value) 0)
references (seq (ctob/find-token-value-references value))]
(cond
(and parsed-value (not out-of-scope))
parsed-value
references
{:errors [(wte/error-with-value :error.style-dictionary/missing-reference references)]
:references references}
(and (not has-references?) out-of-scope)
{:errors [(wte/error-with-value :error.style-dictionary/invalid-token-value-stroke-width value)]}
(and has-references? out-of-scope parsed-value)
(assoc parsed-value :warnings [(wtw/warning-with-value :warning.style-dictionary/invalid-referenced-token-value-stroke-width value)])
:else {:errors [(wte/error-with-value :error.style-dictionary/invalid-token-value value)]})))
@ -132,6 +158,7 @@
parsed-token-value (case (:type origin-token)
:color (parse-sd-token-color-value value)
:opacity (parse-sd-token-opacity-value value has-references?)
:stroke-width (parse-sd-token-stroke-width-value value has-references?)
(parse-sd-token-numeric-value value))
output-token (cond (:errors parsed-token-value)
(merge origin-token parsed-token-value)

View file

@ -1,11 +1,16 @@
(ns app.main.ui.workspace.tokens.warnings
(:require
[app.util.i18n :refer [tr]]
[cuerdas.core :as str]))
(def warning-codes
{:warning.style-dictionary/invalid-referenced-token-value
{:warning/code :warning.style-dictionary/invalid-referenced-token-value
:warning/fn (fn [value] (str/join "\n" [(str "Resolved value " value ".") "Opacity must be between 0 and 100% or 0 and 1 (e.g. 50% or 0.5)"]))}
{:warning.style-dictionary/invalid-referenced-token-value-opacity
{:warning/code :warning.style-dictionary/invalid-referenced-token-value-opacity
:warning/fn (fn [value] (str/join "\n" [(str (tr "workspace.token.resolved-value" value) ".") (tr "workspace.token.opacity-range")]))}
:warning.style-dictionary/invalid-referenced-token-value-stroke-width
{:warning/code :warning.style-dictionary/invalid-referenced-token-value-stroke-width
:warning/fn (fn [value] (str/join "\n" [(str (tr "workspace.token.resolved-value" value) ".") (tr "workspace.token.stroke-width-range")]))}
:warning/unknown
{:warning/code :warning/unknown