Skip to content

Commit 79f670f

Browse files
committed
fix: allow editing a block's other properties if there's invalid URL
Related to logseq/db-test#556
1 parent 1444f2a commit 79f670f

File tree

4 files changed

+48
-39
lines changed

4 files changed

+48
-39
lines changed

deps/db/src/logseq/db/frontend/malli_schema.cljs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,13 @@
8989
expected to be a coll if the property has a :many cardinality. validate-fn is
9090
a fn that is called directly on each value to return a truthy value.
9191
validate-fn varies by property type"
92-
[db validate-fn [property property-val] & {:keys [new-closed-value?]}]
92+
[db validate-fn [property property-val] & {:keys [new-closed-value? _skip-strict-url-validate?]
93+
:as validate-option}]
9394
;; For debugging
9495
;; (when (not (internal-ident? (:db/ident property))) (prn :validate-val (dissoc property :property/closed-values) property-val))
9596
(let [validate-fn' (if (db-property-type/property-types-with-db (:logseq.property/type property))
9697
(fn [value]
97-
(validate-fn db value {:new-closed-value? new-closed-value?}))
98+
(validate-fn db value validate-option))
9899
validate-fn)
99100
validate-fn'' (if (and (db-property-type/closed-value-property-types (:logseq.property/type property))
100101
;; new closed values aren't associated with the property yet
@@ -221,6 +222,10 @@
221222
"Used by validate-fns which need db as input"
222223
nil)
223224

225+
(def ^:dynamic *skip-strict-url-validate?*
226+
"`true` allows updating a block's other property when it has invalid URL value"
227+
false)
228+
224229
(def property-tuple
225230
"A tuple of a property map and a property value"
226231
(into
@@ -235,7 +240,8 @@
235240
(when error-message
236241
{:error/message error-message})
237242
(fn [tuple]
238-
(validate-property-value *db-for-validate-fns* schema-fn tuple))])])
243+
(validate-property-value *db-for-validate-fns* schema-fn tuple
244+
{:skip-strict-url-validate? *skip-strict-url-validate?*}))])])
239245
db-property-type/built-in-validation-schemas)))
240246

241247
(def block-properties

deps/db/src/logseq/db/frontend/property/type.cljs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@
9999

100100
(defn- url-entity?
101101
"Empty string, url or macro url"
102-
[db val {:keys [new-closed-value?]}]
102+
[db val {:keys [new-closed-value? skip-strict-url-validate?]}]
103103
(if new-closed-value?
104104
(or (url? val) (macro-url? val))
105105
(when-let [ent (d/entity db val)]
106106
(let [title (:block/title ent)]
107-
(or (string/blank? title) (url? title) (macro-url? title))))))
107+
(if skip-strict-url-validate?
108+
(string? title)
109+
(or (string/blank? title) (url? title) (macro-url? title)))))))
108110

109111
(defn- entity?
110112
[db id]

deps/db/src/logseq/db/frontend/validate.cljs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,43 @@
2424
(defn validate-tx-report
2525
"Validates the datascript tx-report for entities that have changed. Returns
2626
boolean indicating if db is valid"
27-
[{:keys [db-after tx-data tx-meta]} validate-options]
28-
(let [changed-ids (->> tx-data (keep :e) distinct)
29-
tx-datoms (mapcat #(d/datoms db-after :eavt %) changed-ids)
30-
ent-maps* (map (fn [[db-id m]]
27+
[{:keys [db-after tx-data tx-meta]} {:keys [closed-schema?]}]
28+
(binding [db-malli-schema/*skip-strict-url-validate?* true]
29+
(let [changed-ids (->> tx-data (keep :e) distinct)
30+
tx-datoms (mapcat #(d/datoms db-after :eavt %) changed-ids)
31+
ent-maps* (map (fn [[db-id m]]
3132
;; Add :db/id for debugging
32-
(assoc m :db/id db-id))
33-
(db-malli-schema/datoms->entity-maps tx-datoms {:entity-fn #(d/entity db-after %)}))
34-
ent-maps (db-malli-schema/update-properties-in-ents db-after ent-maps*)
35-
validator (get-schema-validator (:closed-schema? validate-options))]
36-
(binding [db-malli-schema/*db-for-validate-fns* db-after]
37-
(let [invalid-ent-maps (remove
33+
(assoc m :db/id db-id))
34+
(db-malli-schema/datoms->entity-maps tx-datoms {:entity-fn #(d/entity db-after %)}))
35+
ent-maps (db-malli-schema/update-properties-in-ents db-after ent-maps*)
36+
validator (get-schema-validator closed-schema?)]
37+
(binding [db-malli-schema/*db-for-validate-fns* db-after]
38+
(let [invalid-ent-maps (remove
3839
;; remove :db/id as it adds needless declarations to schema
39-
#(validator [(dissoc % :db/id)])
40-
ent-maps)]
41-
(if (seq invalid-ent-maps)
42-
(do
43-
(prn "Invalid datascript entities detected amongst changed entity ids:" changed-ids :tx-meta tx-meta)
44-
(let [explainer (get-schema-explainer (:closed-schema? validate-options))
45-
errors (doall
46-
(map
47-
(fn [m]
48-
(let [m' (update m :block/properties (fn [properties]
49-
(map (fn [[p v]]
50-
[(:db/ident p) v])
51-
properties)))
52-
data {:entity-map m'
53-
:errors (me/humanize (explainer [(dissoc m :db/id)]))}]
54-
(try
55-
(pprint/pprint data)
56-
(catch :default _e
57-
(prn data)))
58-
data))
59-
invalid-ent-maps))]
40+
#(validator [(dissoc % :db/id)])
41+
ent-maps)]
42+
(if (seq invalid-ent-maps)
43+
(do
44+
(prn "Invalid datascript entities detected amongst changed entity ids:" changed-ids :tx-meta tx-meta)
45+
(let [explainer (get-schema-explainer closed-schema?)
46+
errors (doall
47+
(map
48+
(fn [m]
49+
(let [m' (update m :block/properties (fn [properties]
50+
(map (fn [[p v]]
51+
[(:db/ident p) v])
52+
properties)))
53+
data {:entity-map m'
54+
:errors (me/humanize (explainer [(dissoc m :db/id)]))}]
55+
(try
56+
(pprint/pprint data)
57+
(catch :default _e
58+
(prn data)))
59+
data))
60+
invalid-ent-maps))]
6061

61-
[false errors]))
62-
[true nil])))))
62+
[false errors]))
63+
[true nil]))))))
6364

6465
(defn group-errors-by-entity
6566
"Groups malli errors by entities. db is used for providing more debugging info"

src/main/frontend/components/block.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,7 @@
23742374
:color (when-not built-in-color? "white")}
23752375
:class "px-1 with-bg-color"})))
23762376

2377-
;; children
2377+
;; children
23782378
(let [area? (= :area (keyword (pu/lookup block :logseq.property.pdf/hl-type)))
23792379
hl-ref #(when (not (#{:default :whiteboard-shape} block-type))
23802380
[:div.prefix-link

0 commit comments

Comments
 (0)