浏览代码

enhance: add schema validate

Tienson Qin 3 年之前
父节点
当前提交
c51cdc15bf
共有 2 个文件被更改,包括 54 次插入14 次删除
  1. 2 4
      src/main/frontend/components/property/schema.cljs
  2. 52 10
      src/main/frontend/handler/property.cljs

+ 2 - 4
src/main/frontend/components/property/schema.cljs

@@ -30,13 +30,11 @@
                   (fn [item] (if (= schema-type (:value item))
                                (assoc item :selected true)
                                item))
-                  [{:label "Text" :value "text"}
+                  [{:label "Any" :value "any"}
                    {:label "Number" :value "number"}
                    {:label "Date" :value "date"}
-                   {:label "Choice" :value "choice"}
                    {:label "Url" :value "url"}
-                   {:label "Object" :value "object"}
-                   {:label "Any" :value "any"}])]
+                   {:label "Object" :value "object"}])]
     (property-item
      (t :schema/type)
      (ui/select options

+ 52 - 10
src/main/frontend/handler/property.cljs

@@ -7,8 +7,11 @@
             [clojure.string :as string]
             [logseq.graph-parser.mldoc :as gp-mldoc]
             [logseq.graph-parser.block :as gp-block]
+            [logseq.graph-parser.util :as gp-util]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [frontend.modules.outliner.core :as outliner-core]
-            [frontend.modules.outliner.transaction :as outliner-tx]))
+            [frontend.modules.outliner.transaction :as outliner-tx]
+            [frontend.handler.notification :as notification]))
 
 (defn toggle-properties
   [id]
@@ -80,18 +83,57 @@
             [:block/uuid (uuid %)]
             (block/page-name->map % true)) refs')))
 
+(defn validate
+  "Check whether the `value` validate against the `schema`."
+  [schema value]
+  (if (string/blank? value)
+    [true value]
+    (case (:type schema)
+      "any" [true value]
+      "number" (when-let [n (parse-double value)]
+                 (let [[min-n max-n] [(:min schema) (:max schema)]
+                       min-result (if min-n (>= n min-n) true)
+                       max-result (if max-n (<= n max-n) true)]
+                   (cond
+                     (and min-result max-result)
+                     [true value]
+
+                     (false? min-result)
+                     [false (str "the min value is " min-n)]
+
+                     (false? max-result)
+                     [false (str "the max value is " max-n)])))
+      "date" (if-let [result (js/Date. value)]
+               (if (not= (str result) "invalid Date")
+                 [true value]
+                 [false "invalid date"])
+               [false "invalid date"])
+      "url" (if (gp-util/url? value)
+              [true value]
+              [false "invalid URL"])
+      "object" (let [page-name (page-ref/get-page-name value)]
+                 [true page-name]
+                 [false "invalid Object"]))))
+
 (defn add-property-value!
   [entity property-id property-value]
   (when (not= property-id (:block/uuid entity))
-    (let [properties (:block/properties entity)
-          properties' (assoc properties property-id property-value)
-          refs (extract-refs entity properties')]
-      (outliner-tx/transact!
-        {:outliner-op :save-block}
-        (outliner-core/save-block!
-         {:block/uuid (:block/uuid entity)
-          :block/properties properties'
-          :block/refs refs})))))
+    (when-let [property (db/pull [:block/uuid property-id])]
+      (let [schema (:block/property-schema property)
+            [success? property-value-or-error] (validate schema property-value)]
+        (if success?
+          (let [properties (:block/properties entity)
+                properties' (assoc properties property-id property-value-or-error)
+                refs (extract-refs entity properties')]
+            (outliner-tx/transact!
+              {:outliner-op :save-block}
+              (outliner-core/save-block!
+               {:block/uuid (:block/uuid entity)
+                :block/properties properties'
+                :block/refs refs})))
+          (notification/show!
+           (str (:block/original-name property) ": " property-value-or-error)
+           :warning))))))
 
 (defn delete-property!
   [entity property-id]