|
@@ -2,19 +2,22 @@
|
|
|
"Validate frontend db for DB graphs"
|
|
|
(:require [datascript.core :as d]
|
|
|
[logseq.db.frontend.malli-schema :as db-malli-schema]
|
|
|
- [malli.util :as mu]
|
|
|
[malli.core :as m]
|
|
|
- [malli.error :as me]))
|
|
|
+ [malli.error :as me]
|
|
|
+ [malli.util :as mu]))
|
|
|
|
|
|
-(defn update-schema
|
|
|
- "Updates the db schema to add a datascript db for property validations
|
|
|
- and to optionally close maps"
|
|
|
- [db-schema db {:keys [closed-schema?]}]
|
|
|
- (cond-> db-schema
|
|
|
- true
|
|
|
- (db-malli-schema/update-properties-in-schema db)
|
|
|
- closed-schema?
|
|
|
- mu/closed-schema))
|
|
|
+(def ^:private db-schema-validator (m/validator db-malli-schema/DB))
|
|
|
+(def ^:private db-schema-explainer (m/explainer db-malli-schema/DB))
|
|
|
+(def ^:private closed-db-schema-validator (m/validator (mu/closed-schema db-malli-schema/DB)))
|
|
|
+(def ^:private closed-db-schema-explainer (m/explainer (mu/closed-schema db-malli-schema/DB)))
|
|
|
+
|
|
|
+(defn- get-schema-validator
|
|
|
+ [closed-schema?]
|
|
|
+ (if closed-schema? closed-db-schema-validator db-schema-validator))
|
|
|
+
|
|
|
+(defn- get-schema-explainer
|
|
|
+ [closed-schema?]
|
|
|
+ (if closed-schema? closed-db-schema-explainer db-schema-explainer))
|
|
|
|
|
|
(defn validate-ents-before-after!
|
|
|
[changed-ids db-before db-after tx-data tx-meta]
|
|
@@ -48,25 +51,26 @@
|
|
|
(assoc m :db/id db-id))
|
|
|
(db-malli-schema/datoms->entity-maps tx-datoms {:entity-fn #(d/entity db-after %)}))
|
|
|
ent-maps (db-malli-schema/update-properties-in-ents db-after ent-maps*)
|
|
|
- db-schema (update-schema db-malli-schema/DB db-after validate-options)
|
|
|
- invalid-ent-maps (remove
|
|
|
- ;; remove :db/id as it adds needless declarations to schema
|
|
|
- #(m/validate db-schema [(dissoc % :db/id)])
|
|
|
- ent-maps)]
|
|
|
- (js/console.log "changed eids:" changed-ids tx-meta)
|
|
|
- (if (seq invalid-ent-maps)
|
|
|
- (do
|
|
|
- (js/console.error "Invalid datascript entities detected amongst changed entity ids:" changed-ids)
|
|
|
- (doseq [m invalid-ent-maps]
|
|
|
+ validator (get-schema-validator (:closed-schema? validate-options))]
|
|
|
+ (binding [db-malli-schema/*db-for-validate-fns* db-after]
|
|
|
+ (let [invalid-ent-maps (remove
|
|
|
+ ;; remove :db/id as it adds needless declarations to schema
|
|
|
+ #(validator [(dissoc % :db/id)])
|
|
|
+ ent-maps)]
|
|
|
+ (js/console.log "changed eids:" changed-ids tx-meta)
|
|
|
+ (if (seq invalid-ent-maps)
|
|
|
+ (let [explainer (get-schema-explainer (:closed-schema? validate-options))]
|
|
|
+ (js/console.error "Invalid datascript entities detected amongst changed entity ids:" changed-ids)
|
|
|
+ (doseq [m invalid-ent-maps]
|
|
|
|
|
|
- (prn {:entity-map m
|
|
|
- :errors (me/humanize (m/explain db-schema [m]))})
|
|
|
+ (prn {:entity-map m
|
|
|
+ :errors (me/humanize (explainer [m]))})
|
|
|
;; FIXME: pprint fails sometime
|
|
|
;; (pprint/pprint {;; :entity-map (map #(into {} %) m)
|
|
|
;; :errors (me/humanize (m/explain db-schema [m]))})
|
|
|
- )
|
|
|
- false)
|
|
|
- true)))
|
|
|
+ )
|
|
|
+ false)
|
|
|
+ true)))))
|
|
|
|
|
|
(defn group-errors-by-entity
|
|
|
"Groups malli errors by entities. db is used for providing more debugging info"
|
|
@@ -109,12 +113,11 @@
|
|
|
[db]
|
|
|
(let [datoms (d/datoms db :eavt)
|
|
|
ent-maps* (db-malli-schema/datoms->entities datoms)
|
|
|
- schema (update-schema db-malli-schema/DB db {:closed-schema? true})
|
|
|
ent-maps (mapv
|
|
|
;; Remove some UI interactions adding this e.g. import
|
|
|
#(dissoc % :block.temp/fully-loaded?)
|
|
|
(db-malli-schema/update-properties-in-ents db ent-maps*))
|
|
|
- errors (->> ent-maps (m/explain schema) :errors)]
|
|
|
+ errors (-> ent-maps closed-db-schema-explainer :errors)]
|
|
|
(cond-> {:datom-count (count datoms)
|
|
|
:entities ent-maps}
|
|
|
(some? errors)
|