Skip to content

Commit

Permalink
fix: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
alban bertolini committed Oct 2, 2024
1 parent 83e1dda commit 25bd200
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 13 deletions.
4 changes: 3 additions & 1 deletion packages/datasource-toolkit/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export class MissingFieldError extends MissingSchemaElementError {
const { typeOfField, fieldName, availableFields, collectionName } = options;
const path = collectionName ? `${collectionName}.${fieldName}` : fieldName;

super(`${typeOfField} '${path}' not found in the available list: [${availableFields}]`);
super(
`The '${path}' ${typeOfField.toLowerCase()} was not found. Available ${typeOfField.toLowerCase()}s are: [${availableFields}]. Please check if the ${typeOfField.toLowerCase()} name is correct.`,
);
}
}
6 changes: 3 additions & 3 deletions packages/datasource-toolkit/src/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ export default class SchemaUtils {
}

static isPrimaryKey(schema: CollectionSchema, fieldName: string): boolean {
const field = this.getColumn(schema, fieldName);
const field = this.getField(schema, fieldName);

return field.isPrimaryKey;
return field.type === 'Column' && field.isPrimaryKey;
}

static isForeignKey(schema: CollectionSchema, name: string): boolean {
const field = this.getColumn(schema, name);
const field = this.getField(schema, name);

return (
field.type === 'Column' &&
Expand Down
8 changes: 6 additions & 2 deletions packages/datasource-toolkit/test/utils/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,19 @@ describe('CollectionUtils', () => {

expect(() =>
CollectionUtils.getFieldSchema(dataSource.getCollection('books'), 'unknown:id'),
).toThrow("Relation not found 'books.unknown'");
).toThrow(
"The 'books.unknown' relation was not found. Available relations are: [author]. Please check if the relation name is correct.",
);
});

test('should throw if the field is missing', () => {
const { dataSource } = setupWithInverseRelationMissing();

expect(() =>
CollectionUtils.getFieldSchema(dataSource.getCollection('books'), 'author:something'),
).toThrow(`Column not found 'persons.something'`);
).toThrow(
`The 'persons.something' field was not found. Available fields are: [id]. Please check if the field name is correct.`,
);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ describe('FieldValidator', () => {

test('should throw if the field does not exists', () => {
expect(() => FieldValidator.validate(carsCollection, '__not_defined')).toThrow(
"Field 'cars.__not_defined' not found in the available list: [id,owner,drivers]",
"The 'cars.__not_defined' field was not found. Available fields are: [id,owner,drivers]. Please check if the field name is correct.",
);
});

test('should throw if the relation does not exists', () => {
expect(() => FieldValidator.validate(carsCollection, '__not_defined:id')).toThrow(
"Relation 'cars.__not_defined' not found in the available list: [owner]",
"The 'cars.__not_defined' relation was not found. Available relations are: [owner]. Please check if the relation name is correct.",
);
});

Expand Down
8 changes: 6 additions & 2 deletions packages/datasource-toolkit/test/validation/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ describe('RecordValidator', () => {
RecordValidator.validate(collection, {
unknownField: 'this field is not defined in the collection',
}),
).toThrow('Unknown field "unknownField"');
).toThrow(
"The 'a collection.unknownField' field was not found. Available fields are: [name]. Please check if the field name is correct.",
);
});
});

Expand Down Expand Up @@ -110,7 +112,9 @@ describe('RecordValidator', () => {
RecordValidator.validate(dataSourceBook.getCollection('book'), {
relation: { fieldNotExist: 'a name' },
}),
).toThrow('Unknown field "fieldNotExist');
).toThrow(
"The 'owner.fieldNotExist' field was not found. Available fields are: [name]. Please check if the field name is correct.",
);
});

test('should throw an error when the relation is an empty object', () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/plugin-flattener/test/flatten-column.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ describe('flattenColumn', () => {
customizer
.customizeCollection('book', book => book.use(flattenColumn, options))
.getDataSource(logger),
).rejects.toThrow("'book.doctor who?' cannot be flattened (not found).");
).rejects.toThrow(
"The 'book.doctor who?' column was not found. Available columns are: [id,title,tags,author,meta]. Please check if the column name is correct.",
);
});

it('should throw when target is a primitive', async () => {
Expand Down Expand Up @@ -106,7 +108,9 @@ describe('flattenColumn', () => {
customizer
.customizeCollection('book', book => book.use(flattenColumn, options))
.getDataSource(logger),
).rejects.toThrow("'book.myself' cannot be flattened (not a column).");
).rejects.toThrow(
"The 'book.myself' column was not found. Available columns are: [id,title,tags,author,meta]. Please check if the column name is correct.",
);
});

it('should throw level is invalid', async () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/plugin-flattener/test/flatten-relation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ describe('flattenRelation', () => {
book.use(flattenRelation, { relationName: 'owner', include: ['doesNotExist'] }),
)
.getDataSource(logger),
).rejects.toThrow(new MissingFieldError('doesNotExist', 'owner'));
).rejects.toThrow(
"The 'owner.doesNotExist' field was not found. Available fields are: [bookId,countryId,country,name]. Please check if the field name is correct.",
);
});
});
});
Expand Down

0 comments on commit 25bd200

Please sign in to comment.