Bläddra i källkod

fix: closed validation with new properties

Also fix group error reporting incorrectly and improve it by adding
:db/id
Gabriel Horner 1 år sedan
förälder
incheckning
b2a22498ea

+ 4 - 2
deps/db/script/validate_client_db.cljs

@@ -17,7 +17,9 @@
 (defn validate-client-db
   "Validate datascript db as a vec of entity maps"
   [db ent-maps* {:keys [verbose group-errors closed-maps]}]
-  (let [ent-maps (db-malli-schema/update-properties-in-ents ent-maps*)
+  (let [ent-maps ent-maps*
+        ;; TODO: Fix
+        ;; ent-maps (db-malli-schema/update-properties-in-ents ent-maps*)
         schema (db-validate/update-schema db-malli-schema/DB db {:closed-schema? closed-maps})]
     (if-let [errors (->> ent-maps
                          (m/explain schema)
@@ -63,7 +65,7 @@
                     (println "Error: For graph" (str (pr-str graph-dir) ":") (str e))
                     (js/process.exit 1)))
         datoms (d/datoms @conn :eavt)
-        ent-maps (vals (db-malli-schema/datoms->entity-maps datoms))]
+        ent-maps (db-malli-schema/datoms->entities datoms)]
     (println "Read graph" (str db-name " with " (count datoms) " datoms, "
                                (count ent-maps) " entities, "
                                (count (filter :block/name ent-maps)) " pages, "

+ 12 - 4
deps/db/src/logseq/db/frontend/malli_schema.cljs

@@ -90,7 +90,7 @@
    ents))
 
 (defn datoms->entity-maps
-  "Returns entity maps for given :eavt datoms"
+  "Returns entity maps for given :eavt datoms indexed by db/id"
   [datoms]
   (->> datoms
        (reduce (fn [acc m]
@@ -99,6 +99,12 @@
                    (update acc (:e m) assoc (:a m) (:v m))))
                {})))
 
+(defn datoms->entities
+  "Returns a vec of entity maps given :eavt datoms"
+  [datoms]
+  (mapv (fn [[db-id m]] (with-meta m {:db/id db-id}))
+        (datoms->entity-maps datoms)))
+
 ;; Main malli schemas
 ;; ==================
 ;; These schemas should be data vars to remain as simple and reusable as possible
@@ -338,10 +344,12 @@
 
 (def property-pair
   [:map
+   ;; TODO: Validate property value(s)
+   {:closed false}
    [:property/pair-property :int]
-   [:block/tx-id {:optional true} :int]
-   [:block/created-at {:optional true} :int]
-   [:block/updated-at {:optional true} :int]])
+   [:block/created-at :int]
+   [:block/updated-at :int]
+   [:block/tx-id {:optional true} :int]])
 
 (def db-ident-key-val
   "A key-val map consists of a :db/ident and a specific key val"

+ 17 - 13
deps/db/src/logseq/db/frontend/validate.cljs

@@ -12,8 +12,9 @@
    and to optionally close maps"
   [db-schema db {:keys [closed-schema?]}]
   (cond-> db-schema
-    true
-    (db-malli-schema/update-properties-in-schema db)
+    ;; TODO: Fix
+    ;; true
+    ;; (db-malli-schema/update-properties-in-schema db)
     closed-schema?
     mu/closed-schema))
 
@@ -39,15 +40,20 @@
 (defn group-errors-by-entity
   "Groups malli errors by entities. db is used for providing more debugging info"
   [db ent-maps errors]
+  (assert (vector? ent-maps) "Must be a vec for grouping to work")
   (->> errors
        (group-by #(-> % :in first))
        (map (fn [[idx errors']]
-              {:entity (cond-> (get ent-maps idx)
-                         ;; Provide additional page info for debugging
-                         (:block/page (get ent-maps idx))
-                         (update :block/page
-                                 (fn [id] (select-keys (d/entity db id)
-                                                       [:block/name :block/type :db/id :block/created-at]))))
+              {:entity (let [ent (get ent-maps idx)
+                             db-id (:db/id (meta ent))]
+                         (cond-> ent
+                           db-id
+                           (assoc :db/id db-id)
+                           ;; Provide additional page info for debugging
+                           (:block/page ent)
+                           (update :block/page
+                                   (fn [id] (select-keys (d/entity db id)
+                                                         [:block/name :block/type :db/id :block/created-at])))))
                ;; Group by type to reduce verbosity
                :errors-by-type
                (->> (group-by :type errors')
@@ -69,12 +75,10 @@
   :errors and grouped by entity"
   [db]
   (let [datoms (d/datoms db :eavt)
-        ent-maps* (db-malli-schema/datoms->entity-maps datoms)
-        ent-maps (vals ent-maps*)
-        schema db-malli-schema/DB
-        ;; TODO: Fix these fns
+        ent-maps (db-malli-schema/datoms->entities datoms)
+        schema (update-schema db-malli-schema/DB db {:closed-schema? true})
+        ;; TODO: Fix
         ;; ent-maps (db-malli-schema/update-properties-in-ents (vals ent-maps*))
-        ;; (update-schema db-malli-schema/DB db {:closed-schema? true})
         errors (->> ent-maps (m/explain schema) :errors)]
     (cond-> {:datom-count (count datoms)
              :entities ent-maps}