/
githubmirror
/
tldraw
Обзор
Документация
Войти
/
githubmirror
/
tldraw
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
apps/dotcom/sync-worker/src/utils/validateSnapshot.ts
53 строки
2 KB
David Sheldrick
Fix tldr file upload (#5350)
05 фев 2025, 12:30
Не верифицирован
05 фев 2025, 12:30
0aaab49
Код
Авторство
О чём код?
import { SerializedSchema, SerializedStore } from '@tldraw/store' import { TLRecord, createTLSchema } from '@tldraw/tlschema' import { Result, objectMapEntries } from '@tldraw/utils' interface SnapshotRequestBody { schema: SerializedSchema snapshot: SerializedStore<TLRecord> } const schema = createTLSchema() export function validateSnapshot( body: SnapshotRequestBody ): Result<SerializedStore<TLRecord>, string> { // Migrate the snapshot using the provided schema const migrationResult = schema.migrateStoreSnapshot({ store: body.snapshot, schema: body.schema }) if (migrationResult.type === 'error') { console.error('Migration error:', migrationResult.reason) return Result.err(migrationResult.reason) } try { for (const [id, record] of objectMapEntries(migrationResult.value)) { // Throw if any records have mis-matched ids if (id !== record.id) { throw new Error(`Record id ${id} does not match record id ${record.id}`) } // Get the corresponding record type from the provided schema const recordType = schema.types[record.typeName] // Throw if any records have missing record type definitions if (!recordType) { throw new Error(`Missing definition for record type ${record.typeName}`) } // Remove all records whose record type scopes are not 'document'. // This is legacy cleanup code. if (recordType.scope !== 'document') { delete migrationResult.value[id] continue } // Validate the record recordType.validate(record) } } catch (e: any) { console.error('Validation error:', e.message) return Result.err(e.message) } return Result.ok(migrationResult.value) }