From f9530c5a10d8d31e499abad00a7ed74ef624f4d7 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Wed, 26 Jun 2024 16:01:41 +0200 Subject: [PATCH] Restrict token naming --- .../app/main/ui/workspace/tokens/form.cljs | 29 ++++++++++++++++++- .../test/token_tests/token_form_test.cljs | 26 +++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 frontend/test/token_tests/token_form_test.cljs diff --git a/frontend/src/app/main/ui/workspace/tokens/form.cljs b/frontend/src/app/main/ui/workspace/tokens/form.cljs index 9dd1930a4..47c39b263 100644 --- a/frontend/src/app/main/ui/workspace/tokens/form.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/form.cljs @@ -22,6 +22,30 @@ ;; Schemas --------------------------------------------------------------------- +(def valid-token-name-regexp + "Only allow letters and digits for token names. + Also allow one `.` for a namespace separator. + + Caution: This will allow a trailing dot like `token-name.`, + But we will trim that in the `finalize-name`, + to not throw too many errors while the user is editing." + #"([a-zA-Z0-9]+\.?)*") + +(def valid-token-name-schema + (m/-simple-schema + {:type :token/invalid-token-name + :pred #(re-matches valid-token-name-regexp %) + :type-properties {:error/fn #(str (:value %) " is not a valid token name. +Token names should only contain letters and digits separated by . characters.")}})) + +(comment + (m/validate valid-token-name-schema "Hey [1]") + (m/valid? valid-token-name-schema "Hey") + (m/validate valid-token-name-schema "Hey.foo.") + (m/validate valid-token-name-schema "🤣") + (m/validate valid-token-name-schema ".") + nil) + (defn token-name-schema "Generate a dynamic schema validation to check if a token name already exists. `existing-token-names` should be a set of strings." @@ -35,6 +59,7 @@ (m/schema [:and [:string {:min 1 :max 255}] + valid-token-name-schema non-existing-token-schema]))) (def token-description-schema @@ -44,7 +69,9 @@ ;; Helpers --------------------------------------------------------------------- (defn finalize-name [name] - (str/trim name)) + (-> (str/trim name) + ;; Remove trailing dots + (str/replace #"\.+$" ""))) (defn valid-name? [name] (seq (finalize-name (str name)))) diff --git a/frontend/test/token_tests/token_form_test.cljs b/frontend/test/token_tests/token_form_test.cljs new file mode 100644 index 000000000..2bc14df32 --- /dev/null +++ b/frontend/test/token_tests/token_form_test.cljs @@ -0,0 +1,26 @@ +;; 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 token-tests.token-form-test + (:require + [app.main.ui.workspace.tokens.form :as wtf] + [cljs.test :as t :include-macros true] + [malli.core :as m])) + +(t/deftest test-valid-token-name-schema + ;; Allow regular namespace token names + (t/is (some? (m/validate wtf/valid-token-name-schema "Foo"))) + (t/is (some? (m/validate wtf/valid-token-name-schema "foo"))) + (t/is (some? (m/validate wtf/valid-token-name-schema "FOO"))) + (t/is (some? (m/validate wtf/valid-token-name-schema "Foo.Bar.Baz"))) + ;; Allow trailing tokens + (t/is (nil? (m/validate wtf/valid-token-name-schema "Foo.Bar.Baz...."))) + ;; Disallow multiple separator dots + (t/is (nil? (m/validate wtf/valid-token-name-schema "Foo..Bar.Baz"))) + ;; Disallow any special characters + (t/is (nil? (m/validate wtf/valid-token-name-schema "Hey Foo.Bar"))) + (t/is (nil? (m/validate wtf/valid-token-name-schema "Hey😈Foo.Bar"))) + (t/is (nil? (m/validate wtf/valid-token-name-schema "Hey%Foo.Bar"))))