Add url validation (#2428)

Adds validation for urls we use for our shapes and assets. This PR
includes a migration so we should check that existing rooms still load
correctly. There might be some that won't, but that means that they had
invalid url set.

### 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. Existing rooms should still load correctly (there should be no
validation errors).
2. Adding new images and videos should also work (test both local and
multiplayer rooms as they handle assets differently).

- [x] Unit Tests
- [ ] End to end tests

### Release Notes

- Add validation to urls.

---------

Co-authored-by: alex <alex@dytry.ch>
This commit is contained in:
Mitja Bezenšek 2024-01-09 11:49:57 +01:00 committed by GitHub
parent 4821ba0ba3
commit 6e9fe0c8be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 393 additions and 91 deletions

View file

@ -1650,6 +1650,34 @@
"endIndex": 5
}
},
{
"kind": "Variable",
"canonicalReference": "@tldraw/validate!T.linkUrl:var",
"docComment": "/**\n * Validates that a value is a url safe to use as a link.\n *\n * @public\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "linkUrl: "
},
{
"kind": "Reference",
"text": "Validator",
"canonicalReference": "@tldraw/validate!Validator:class"
},
{
"kind": "Content",
"text": "<string>"
}
],
"fileUrlPath": "packages/validate/src/lib/validation.ts",
"isReadonly": true,
"releaseTag": "Public",
"name": "linkUrl",
"variableTypeTokenRange": {
"startIndex": 1,
"endIndex": 3
}
},
{
"kind": "Function",
"canonicalReference": "@tldraw/validate!T.literal:function(1)",
@ -2585,6 +2613,34 @@
],
"name": "setEnum"
},
{
"kind": "Variable",
"canonicalReference": "@tldraw/validate!T.srcUrl:var",
"docComment": "/**\n * Validates that a valid is a url safe to load as an asset.\n *\n * @public\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "srcUrl: "
},
{
"kind": "Reference",
"text": "Validator",
"canonicalReference": "@tldraw/validate!Validator:class"
},
{
"kind": "Content",
"text": "<string>"
}
],
"fileUrlPath": "packages/validate/src/lib/validation.ts",
"isReadonly": true,
"releaseTag": "Public",
"name": "srcUrl",
"variableTypeTokenRange": {
"startIndex": 1,
"endIndex": 3
}
},
{
"kind": "Variable",
"canonicalReference": "@tldraw/validate!T.string:var",
@ -3509,6 +3565,59 @@
"isAbstract": false,
"name": "check"
},
{
"kind": "Method",
"canonicalReference": "@tldraw/validate!T.Validator#isValid:member(1)",
"docComment": "/**\n * Checks that the passed value is of the correct type.\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "isValid(value: "
},
{
"kind": "Content",
"text": "unknown"
},
{
"kind": "Content",
"text": "): "
},
{
"kind": "Reference",
"text": "value",
"canonicalReference": "@tldraw/validate!~value"
},
{
"kind": "Content",
"text": " is T"
},
{
"kind": "Content",
"text": ";"
}
],
"isStatic": false,
"returnTypeTokenRange": {
"startIndex": 3,
"endIndex": 5
},
"releaseTag": "Public",
"isProtected": false,
"overloadIndex": 1,
"parameters": [
{
"parameterName": "value",
"parameterTypeTokenRange": {
"startIndex": 1,
"endIndex": 2
},
"isOptional": false
}
],
"isOptional": false,
"isAbstract": false,
"name": "isValid"
},
{
"kind": "Method",
"canonicalReference": "@tldraw/validate!T.Validator#nullable:member(1)",
@ -4219,6 +4328,59 @@
"isAbstract": false,
"name": "check"
},
{
"kind": "Method",
"canonicalReference": "@tldraw/validate!Validator#isValid:member(1)",
"docComment": "/**\n * Checks that the passed value is of the correct type.\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "isValid(value: "
},
{
"kind": "Content",
"text": "unknown"
},
{
"kind": "Content",
"text": "): "
},
{
"kind": "Reference",
"text": "value",
"canonicalReference": "@tldraw/validate!~value"
},
{
"kind": "Content",
"text": " is T"
},
{
"kind": "Content",
"text": ";"
}
],
"isStatic": false,
"returnTypeTokenRange": {
"startIndex": 3,
"endIndex": 5
},
"releaseTag": "Public",
"isProtected": false,
"overloadIndex": 1,
"parameters": [
{
"parameterName": "value",
"parameterTypeTokenRange": {
"startIndex": 1,
"endIndex": 2
},
"isOptional": false
}
],
"isOptional": false,
"isAbstract": false,
"name": "isValid"
},
{
"kind": "Method",
"canonicalReference": "@tldraw/validate!Validator#nullable:member(1)",