Skip to content

Commit

Permalink
Improved error messaging for conflicting link defs in schema.ts (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
markyfyi authored Oct 1, 2024
1 parent d1c5640 commit 7a24bbf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
8 changes: 4 additions & 4 deletions client/sandbox/cli-nodejs/instant.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ const graph = i.graph(
},
postsTags: {
forward: {
on: "posts",
on: "tags",
has: "many",
label: "tags",
label: "posts",
},
reverse: {
on: "tags",
on: "posts",
has: "many",
label: "posts",
label: "tags",
},
},
},
Expand Down
38 changes: 28 additions & 10 deletions server/src/instant/model/schema.clj
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,39 @@
". "
"Check your schema file for duplicate link definitions."))

(defn backwards-link-message [etype label]
(str "Conflicting link found for attribute: "
etype
"->"
label
". "
"It's possible that you already have a link with the same label names, but in the reverse direction. "
"We cannot automatically swap the direction of the link, so you will need to do this manually. "
"Check your schema in https://www.instantdb.com/dash for a link with the same label names. "
"If you find one, you can either delete the existing link in the dashboard, or swap the `forward` and `reverse` parameters for this link in your schema file."))

(defn assert-unique-idents! [current-attrs steps]
(let [current-ident-names (->> current-attrs
(mapcat attr-ident-names)
(map vec))
ident-names (->>
steps
(mapcat (fn [[op data]]
(when (= op :add-attr)
(attr-ident-names data)))))
dups (->> (concat current-ident-names ident-names)
(let [current-fwd-ident-names (->> current-attrs
(map attr-model/fwd-ident-name)
(map vec))
current-all-ident-names (->> current-attrs
(mapcat attr-ident-names)
(map vec))
new-ident-names (->>
steps
(mapcat (fn [[op data]]
(when (= op :add-attr)
(attr-ident-names data)))))
dups (->> (concat current-all-ident-names new-ident-names)
(frequencies)
(filter (fn [[_ freq]] (> freq 1))))
errors (map (fn [[[etype label]]]
{:in [:schema]
:message (dup-message etype label)}) dups)]
:message (if
(some #(= % [etype label]) current-fwd-ident-names)
(dup-message etype label)
(backwards-link-message etype label))})
dups)]
(ex/assert-valid! :schema
:steps
errors)))
Expand Down

0 comments on commit 7a24bbf

Please sign in to comment.