From 8d50852cbe0f456122409080d400719043814f94 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 3 Nov 2021 13:08:39 +0100 Subject: [PATCH] :sparkles: Minor imrovements on general purpose specs naming. --- backend/src/app/config.clj | 2 +- common/src/app/common/spec.cljc | 50 +++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/backend/src/app/config.clj b/backend/src/app/config.clj index 2c1d77aba..ad121323c 100644 --- a/backend/src/app/config.clj +++ b/backend/src/app/config.clj @@ -85,7 +85,7 @@ ;; a server prop key where initial project is stored. :initial-project-skey "initial-project"}) -(s/def ::flags ::us/words) +(s/def ::flags ::us/set-of-keywords) ;; DEPRECATED PROPERTIES: should be removed in 1.10 (s/def ::registration-enabled ::us/boolean) diff --git a/common/src/app/common/spec.cljc b/common/src/app/common/spec.cljc index 88f8eab9a..e7614707b 100644 --- a/common/src/app/common/spec.cljc +++ b/common/src/app/common/spec.cljc @@ -111,16 +111,6 @@ (s/def ::point gpt/point?) (s/def ::id ::uuid) -(s/def ::words - (s/conformer - (fn [s] - (cond - (set? s) s - (string? s) (into #{} (map keyword) (str/words s)) - :else ::s/invalid)) - (fn [s] - (str/join " " (map name s))))) - (defn bytes? "Test if a first parameter is a byte array or not." @@ -134,7 +124,6 @@ (s/def ::bytes bytes?) - (s/def ::safe-integer #(and (int? %) @@ -149,8 +138,28 @@ (<= % max-safe-int))) +;; --- SPEC: set of Keywords + +(s/def ::set-of-keywords + (s/conformer + (fn [s] + (let [xform (comp + (map (fn [s] + (cond + (string? s) (keyword s) + (keyword? s) s + :else nil))) + (filter identity))] + (cond + (set? s) (into #{} xform s) + (string? s) (into #{} xform (str/words s)) + :else ::s/invalid))) + (fn [s] + (str/join " " (map name s))))) + ;; --- SPEC: email -(def email-re #"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+") + +(def email-re #"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+") (s/def ::email (s/conformer @@ -162,6 +171,23 @@ ::s/invalid)) str)) +(s/def ::set-of-emails + (s/conformer + (fn [v] + (cond + (string? v) + (into #{} (re-seq email-re v)) + + (or (set? v) (sequential? v)) + (->> (str/join " " v) + (re-seq email-re) + (into #{})) + + :else ::s/invalid)) + + (fn [v] + (str/join " " v)))) + ;; --- SPEC: set-of-str (s/def ::set-of-str