Skip to content

Commit 96e01f7

Browse files
author
Hein
committed
Add schema validation
1 parent 195fffd commit 96e01f7

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

packages/common/src/db/schema/Table.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ export class Table<Columns extends ColumnsType = ColumnsType> {
238238
}
239239

240240
validate() {
241+
if (this.options.name === '') {
242+
throw new Error(
243+
"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."
244+
);
245+
}
246+
241247
if (InvalidSQLCharacters.test(this.name)) {
242248
throw new Error(`Invalid characters in table name: ${this.name}`);
243249
}

packages/common/tests/db/schema/Schema.test.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,27 @@ import { describe, it, expect } from 'vitest';
22
import { Schema } from '../../../src/db/schema/Schema';
33
import { Table } from '../../../src/db/schema/Table';
44
import { column, ColumnType } from '../../../src/db/schema/Column';
5+
import { Column } from '../../../lib';
56

67
describe('Schema', () => {
8+
it('should fail if array is passed and not a table object', () => {
9+
const table1 = new Table({ name: column.text });
10+
const table2 = new Table({ age: { type: ColumnType.INTEGER } });
11+
const schema = new Schema([table1, table2]);
12+
expect(() => schema.validate()).toThrow();
13+
});
14+
715
it('should create a schema with an array of tables', () => {
8-
const tables = [
9-
new Table({ name: column.text, }),
10-
new Table({ age: { type: ColumnType.INTEGER } })
11-
];
12-
const schema = new Schema(tables);
16+
const table1 = new Table({
17+
name: 'table1',
18+
columns: [new Column({ name: 'name', type: ColumnType.TEXT })]
19+
});
20+
const table2 = new Table({
21+
name: 'table2',
22+
columns: [new Column({ name: 'age', type: ColumnType.INTEGER })]
23+
});
24+
const schema = new Schema([table1, table2]);
25+
expect(() => schema.validate()).not.toThrow();
1326

1427
expect(schema.tables).toHaveLength(2);
1528
expect(schema.tables[0].columns[0].name).toBe('name');

0 commit comments

Comments
 (0)