From 9c91b2c4cd4b0e6866c5aae89253442dfb479f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mitja=20Bezen=C5=A1ek?= Date: Mon, 15 Jan 2024 13:33:46 +0100 Subject: [PATCH] Fix validation for local files. (#2447) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allow urls for local files. This addresses the comment from [here](https://github.com/tldraw/tldraw/pull/2428#issuecomment-1886221841). ### Change Type - [x] `patch` — Bug fix - [ ] `minor` — New feature - [ ] `major` — Breaking change - [ ] `dependencies` — Changes to package dependencies[^1] - [ ] `documentation` — Changes to the documentation only[^2] - [ ] `tests` — Changes to any test code only[^2] - [ ] `internal` — Any other changes that don't affect the published package[^2] - [ ] I don't know [^1]: publishes a `patch` release, for devDependencies use `internal` [^2]: will not publish a new version ### Test Plan 1. Local images example should now work. We use images from the public folder there. --- packages/validate/src/lib/validation.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/validate/src/lib/validation.ts b/packages/validate/src/lib/validation.ts index 77ba5b7e7..aaea52e8f 100644 --- a/packages/validate/src/lib/validation.ts +++ b/packages/validate/src/lib/validation.ts @@ -617,6 +617,13 @@ function parseUrl(str: string) { try { return new URL(str) } catch (error) { + if (str.startsWith('/') || str.startsWith('./')) { + try { + return new URL(str, 'http://example.com') + } catch (error) { + throw new ValidationError(`Expected a valid url, got ${JSON.stringify(str)}`) + } + } throw new ValidationError(`Expected a valid url, got ${JSON.stringify(str)}`) } }