Skip to content

Commit

Permalink
Add schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
HeinrichvonStein committed Dec 18, 2024
1 parent 195fffd commit 96e01f7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
6 changes: 6 additions & 0 deletions packages/common/src/db/schema/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ export class Table<Columns extends ColumnsType = ColumnsType> {
}

validate() {
if (this.options.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."
);
}

if (InvalidSQLCharacters.test(this.name)) {
throw new Error(`Invalid characters in table name: ${this.name}`);
}
Expand Down
23 changes: 18 additions & 5 deletions packages/common/tests/db/schema/Schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@ 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 } });
const schema = new Schema([table1, table2]);
expect(() => schema.validate()).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');
Expand Down

0 comments on commit 96e01f7

Please sign in to comment.