From 94a7a7d4696f4718c04515880ca78b23ae99a595 Mon Sep 17 00:00:00 2001 From: Hein Date: Wed, 18 Dec 2024 10:41:14 +0200 Subject: [PATCH 1/4] Add schema validation --- packages/common/src/db/schema/Schema.ts | 12 ++++++++++ .../common/tests/db/schema/Schema.test.ts | 22 ++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/common/src/db/schema/Schema.ts b/packages/common/src/db/schema/Schema.ts index 0b72499a1..8d283a080 100644 --- a/packages/common/src/db/schema/Schema.ts +++ b/packages/common/src/db/schema/Schema.ts @@ -19,6 +19,18 @@ export class Schema { constructor(tables: Table[] | S) { if (Array.isArray(tables)) { + /* + We need to validate that the tables have a name here because a user could pass in an array + of Tables that don't have a name because they are using the V2 syntax. + Therefore, 'convertToClassicTables' won't be called on the tables resulting in a runtime error. + */ + for (const table of tables) { + if (table.name === '') { + throw new Error( + "It appears you are trying to create a new Schema with an array instead of an object. Passing in an object instead of an array into 'new Schema()' may resolve your issue." + ); + } + } this.tables = tables; } else { this.props = tables as S; diff --git a/packages/common/tests/db/schema/Schema.test.ts b/packages/common/tests/db/schema/Schema.test.ts index 031dc80a8..c731eb622 100644 --- a/packages/common/tests/db/schema/Schema.test.ts +++ b/packages/common/tests/db/schema/Schema.test.ts @@ -2,14 +2,26 @@ import { describe, it, expect } from 'vitest'; import { Schema } from '../../../src/db/schema/Schema'; import { Table } from '../../../src/db/schema/Table'; import { column, ColumnType } from '../../../src/db/schema/Column'; +import { Column } from '../../../lib'; describe('Schema', () => { + it('should fail if array is passed and not a table object', () => { + const table1 = new Table({ name: column.text }); + const table2 = new Table({ age: { type: ColumnType.INTEGER } }); + expect(() => new Schema([table1, table2])).toThrow(); + }); + it('should create a schema with an array of tables', () => { - const tables = [ - new Table({ name: column.text, }), - new Table({ age: { type: ColumnType.INTEGER } }) - ]; - const schema = new Schema(tables); + const table1 = new Table({ + name: 'table1', + columns: [new Column({ name: 'name', type: ColumnType.TEXT })] + }); + const table2 = new Table({ + name: 'table2', + columns: [new Column({ name: 'age', type: ColumnType.INTEGER })] + }); + const schema = new Schema([table1, table2]); + expect(() => schema.validate()).not.toThrow(); expect(schema.tables).toHaveLength(2); expect(schema.tables[0].columns[0].name).toBe('name'); From 09d758b04698cc831c4a29f8088b7f6c1b46edad Mon Sep 17 00:00:00 2001 From: Hein Date: Wed, 18 Dec 2024 11:59:25 +0200 Subject: [PATCH 2/4] Update test descriptions --- packages/common/tests/db/schema/Schema.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/common/tests/db/schema/Schema.test.ts b/packages/common/tests/db/schema/Schema.test.ts index c731eb622..2dd472d12 100644 --- a/packages/common/tests/db/schema/Schema.test.ts +++ b/packages/common/tests/db/schema/Schema.test.ts @@ -5,13 +5,13 @@ import { column, ColumnType } from '../../../src/db/schema/Column'; import { Column } from '../../../lib'; describe('Schema', () => { - it('should fail if array is passed and not a table object', () => { + it('should fail if an array of tables using the new syntax is passed to schema', () => { const table1 = new Table({ name: column.text }); const table2 = new Table({ age: { type: ColumnType.INTEGER } }); expect(() => new Schema([table1, table2])).toThrow(); }); - it('should create a schema with an array of tables', () => { + it('should create a schema with an array of tables using the old syntax', () => { const table1 = new Table({ name: 'table1', columns: [new Column({ name: 'name', type: ColumnType.TEXT })] From 2aba912d1fd297ddc1ede9a480ade47c3ed66790 Mon Sep 17 00:00:00 2001 From: Hein Date: Wed, 18 Dec 2024 14:16:25 +0200 Subject: [PATCH 3/4] Update import --- packages/common/tests/db/schema/Schema.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/common/tests/db/schema/Schema.test.ts b/packages/common/tests/db/schema/Schema.test.ts index 2dd472d12..a0e0dd2ab 100644 --- a/packages/common/tests/db/schema/Schema.test.ts +++ b/packages/common/tests/db/schema/Schema.test.ts @@ -1,8 +1,7 @@ import { describe, it, expect } from 'vitest'; import { Schema } from '../../../src/db/schema/Schema'; import { Table } from '../../../src/db/schema/Table'; -import { column, ColumnType } from '../../../src/db/schema/Column'; -import { Column } from '../../../lib'; +import { column, ColumnType, Column } from '../../../src/db/schema/Column'; describe('Schema', () => { it('should fail if an array of tables using the new syntax is passed to schema', () => { From 4f234f6598956181d14def9d13454a42a6b3e011 Mon Sep 17 00:00:00 2001 From: Hein Date: Wed, 18 Dec 2024 14:42:25 +0200 Subject: [PATCH 4/4] Add changeset --- .changeset/rude-snakes-report.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/rude-snakes-report.md diff --git a/.changeset/rude-snakes-report.md b/.changeset/rude-snakes-report.md new file mode 100644 index 000000000..d89ce952f --- /dev/null +++ b/.changeset/rude-snakes-report.md @@ -0,0 +1,5 @@ +--- +'@powersync/common': patch +--- + +Add schema validation