Repository: penpot/penpot
File: frontend/src/app/main/ui/workspace/tokens/management/create/form.cljs (ClojureScript)

Code before:
(defn invalidate-font-family-token-self-reference [token]
  (invalidate-coll-self-reference (:name token) (:value token)))

(defn validate-font-family-token
  [props]
  (-> props
      (update :token-value ctt/split-font-family)
      (assoc :validators [(fn [token]
                            (when (empty? (:value token))
                              (wte/get-error-code :error.token/empty-input)))
                          invalidate-font-family-token-self-reference])
      (default-validate-token)))

(defn invalidate-typography-token-self-reference
  "Invalidate token when any of the attributes in token value have a self refernce."
  [token]
  (let [token-name (:name token)
        token-values (:value token)]
    (some (fn [[k v]]
            (when-let [err (case k
                             :font-family (invalidate-coll-self-reference token-name v)
                             (invalidate-self-reference token-name v))]
              (assoc err :typography-key k)))
          token-values)))

(defn invalidate-empty-typography-token [token]
  (when (empty? (:value token))
    (wte/get-error-code :error.token/empty-input)))

(defn validate-typography-token
  [{:keys [token-value] :as props}]
  (cond
    ;; Entering form without a value - show no error just resolve nil
    (nil? token-value) (rx/of nil)
    ;; Validate refrence string
    (ctt/typography-composite-token-reference? token-value) (default-validate-token props)
    ;; Validate composite token
    :else
    (-> props
        (update :token-value
                (fn [v]
                  (-> (or v {})
                      (d/update-when :font-family #(if (string? %) (ctt/split-font-family %) %)))))
        (assoc :validators [invalidate-empty-typography-token
                            invalidate-typography-token-self-reference])
        (default-validate-token))))

(defn use-debonced-resolve-callback
  "Resolves a token values using `StyleDictionary`.
   This function is debounced as the resolving might be an expensive calculation.
   Uses a custom debouncing logic, as the resolve function is async."

Review comment (suggestion):
To be consistent with other forms in the code, I'll suggest the name `check-empty-typography-token` (and same in other in `invalidate-xxx` functions in the module).

Code after:
(defn check-font-family-token-self-reference [token]
  (check-coll-self-reference (:name token) (:value token)))

(defn validate-font-family-token
  [props]
  (-> props
      (update :token-value ctt/split-font-family)
      (assoc :validators [(fn [token]
                            (when (empty? (:value token))
                              (wte/get-error-code :error.token/empty-input)))
                          check-font-family-token-self-reference])
      (default-validate-token)))

(defn check-typography-token-self-reference
  "Check token when any of the attributes in token value have a self-reference."
  [token]
  (let [token-name (:name token)
        token-values (:value token)]
    (some (fn [[k v]]
            (when-let [err (case k
                             :font-family (check-coll-self-reference token-name v)
                             (check-self-reference token-name v))]
              (assoc err :typography-key k)))
          token-values)))

(defn check-empty-typography-token [token]
  (when (empty? (:value token))
    (wte/get-error-code :error.token/empty-input)))

(defn validate-typography-token
  [{:keys [token-value] :as props}]
  (cond
    ;; Entering form without a value - show no error just resolve nil
    (nil? token-value) (rx/of nil)
    ;; Validate refrence string
    (ctt/typography-composite-token-reference? token-value) (default-validate-token props)
    ;; Validate composite token
    :else
    (-> props
        (update :token-value
                (fn [v]
                  (-> (or v {})
                      (d/update-when :font-family #(if (string? %) (ctt/split-font-family %) %)))))
        (assoc :validators [check-empty-typography-token
                            check-typography-token-self-reference])
        (default-validate-token))))

(defn use-debonced-resolve-callback
  "Resolves a token values using `StyleDictionary`.
   This function is debounced as the resolving might be an expensive calculation.
   Uses a custom debouncing logic, as the resolve function is async."