-
Notifications
You must be signed in to change notification settings - Fork 6
ENG-1975 Add schema file contract and shared foundation for Obsidian export/import #1180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { z } from "zod"; | ||
| import type { DiscourseSchemaFile } from "~/types"; | ||
|
|
||
| export const DG_SCHEMA_EXPORT_VERSION = 1; | ||
|
|
||
| const discourseNodeSchema = z.object({ | ||
| id: z.string(), | ||
| name: z.string(), | ||
| format: z.string(), | ||
| template: z.string().optional(), | ||
| description: z.string().optional(), | ||
| shortcut: z.string().optional(), | ||
| color: z.string().optional(), | ||
| tag: z.string().optional(), | ||
| keyImage: z.boolean().optional(), | ||
| folderPath: z.string().optional(), | ||
| created: z.number(), | ||
| modified: z.number(), | ||
| importedFromRid: z.string().optional(), | ||
| authorId: z.number().optional(), | ||
| }); | ||
|
|
||
| const relationImportStatusSchema = z.enum(["provisional", "accepted"]); | ||
|
|
||
| const discourseRelationTypeSchema = z.object({ | ||
| id: z.string(), | ||
| label: z.string(), | ||
| complement: z.string(), | ||
| color: z.string(), | ||
| created: z.number(), | ||
| modified: z.number(), | ||
| importedFromRid: z.string().optional(), | ||
| status: relationImportStatusSchema.optional(), | ||
| authorId: z.number().optional(), | ||
| }); | ||
|
|
||
| const discourseRelationSchema = z.object({ | ||
| id: z.string(), | ||
| sourceId: z.string(), | ||
| destinationId: z.string(), | ||
| relationshipTypeId: z.string(), | ||
| created: z.number(), | ||
| modified: z.number(), | ||
| importedFromRid: z.string().optional(), | ||
| status: relationImportStatusSchema.optional(), | ||
| authorId: z.number().optional(), | ||
| }); | ||
|
|
||
| const templateExportSchema = z.object({ | ||
| name: z.string(), | ||
| content: z.string(), | ||
| }); | ||
|
|
||
| export const dgSchemaFileSchema = z.object({ | ||
| version: z.literal(DG_SCHEMA_EXPORT_VERSION), | ||
| exportedAt: z.string(), | ||
| pluginVersion: z.string(), | ||
| vaultName: z.string(), | ||
| nodeTypes: z.array(discourseNodeSchema), | ||
| relationTypes: z.array(discourseRelationTypeSchema), | ||
| discourseRelations: z.array(discourseRelationSchema), | ||
| templates: z.array(templateExportSchema), | ||
| }); | ||
|
Comment on lines
+54
to
+63
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚩 Zod default behavior silently strips extra properties from imported schema files The Zod schemas use Was this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
| const normalizeToKebabCase = (value: string): string => { | ||
| return value | ||
| .trim() | ||
| .toLowerCase() | ||
| .replace(/[^a-z0-9]+/g, "-") | ||
| .replace(/^-+|-+$/g, "") | ||
| .replace(/-{2,}/g, "-"); | ||
| }; | ||
|
|
||
| export const getDgSchemaFileName = (vaultName?: string): string => { | ||
| const normalizedVaultName = vaultName ? normalizeToKebabCase(vaultName) : ""; | ||
| const safeVaultName = | ||
| normalizedVaultName.length > 0 ? normalizedVaultName : "vault"; | ||
| return `dg-schema-${safeVaultName}.json`; | ||
| }; | ||
|
|
||
| export const parseDgSchemaFile = (value: unknown): DiscourseSchemaFile => { | ||
| return dgSchemaFileSchema.parse(value) as DiscourseSchemaFile; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Schema validation accepts any color string but downstream types require one of 13 specific color names
The relation-type color is validated as any string (
z.string()atapps/obsidian/src/utils/specValidation.ts:29), then force-cast to a type that only allows 13 specific color names, so an imported file with a bogus color like"rainbow"passes validation and silently violates the type contract.Impact: Imported schema files with invalid color names pass validation, potentially causing unexpected rendering in the canvas UI.
Type mismatch between Zod schema and DiscourseRelationType.color
The
DiscourseRelationTypetype atapps/obsidian/src/types.ts:27definescolor: TldrawColorName, which is a union of 13 specific string literals defined atapps/obsidian/src/utils/tldrawColors.ts:5-19(e.g."black","blue","red", etc.).However, the Zod schema at line 29 uses
z.string()which accepts any string. TheparseDgSchemaFilefunction at line 82 then casts the resultas DiscourseSchemaFile, hiding this mismatch from the type system.Downstream code like
COLOR_PALETTE[relationType.color]atapps/obsidian/src/components/canvas/utils/relationTypeUtils.ts:135or direct prop assignment atapps/obsidian/src/components/canvas/overlays/DragHandleOverlay.tsx:371relies on this being a validTldrawColorName. While some call sites have fallbacks (e.g.toTldrawColor()), the validation layer should catch this at parse time rather than allowing invalid data through.The fix would be to use
z.enum(TLDRAW_COLOR_NAMES)instead ofz.string()for the color field, matching the actual type constraint.Was this helpful? React with 👍 or 👎 to provide feedback.