Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
wschurman committed Jun 4, 2024
1 parent 5819e0d commit 672e31a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
15 changes: 6 additions & 9 deletions packages/entity/src/EntityConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { mapMap, invertMap, reduceMap } from './utils/collections/maps';
* The data storage configuration for a type of Entity. Contains information relating to IDs,
* cachable fields, field mappings, and types of cache and database adapter.
*/
export default class EntityConfiguration<TFields extends Record<string, any>> {
export default class EntityConfiguration<TFields extends Omit<Record<string, any>, 'constuctor'>> {
readonly idField: keyof TFields;
readonly tableName: string;
readonly cacheableKeys: ReadonlySet<keyof TFields>;
Expand Down Expand Up @@ -86,15 +86,12 @@ export default class EntityConfiguration<TFields extends Record<string, any>> {
}

private static validateSchema<TFields extends Record<string, any>>(schema: TFields): void {
const disallowedFieldsKeys = Object.getOwnPropertyNames(Object.getPrototypeOf({}));
const disallowedFieldsKeys = Object.getOwnPropertyNames(Object.prototype);
for (const disallowedFieldsKey of disallowedFieldsKeys) {
// when `hasOwnProperty` is a field name, we can't call it as a method. it's still invalid though.
if (typeof schema.hasOwnProperty !== 'function') {
throw new Error(`Entity field name not allowed: hasOwnProperty`);
}

if (schema.hasOwnProperty(disallowedFieldsKey)) {
throw new Error(`Entity field name not allowed: ${disallowedFieldsKey}`);
if (Object.hasOwn(schema, disallowedFieldsKey)) {
throw new Error(
`Entity field name not allowed to prevent conflicts with standard Object prototype fields: ${disallowedFieldsKey}`
);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/entity/src/__tests__/EntityConfiguration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ describe(EntityConfiguration, () => {
databaseAdapterFlavor: 'postgres',
cacheAdapterFlavor: 'redis',
})
).toThrow(`Entity field name not allowed: ${keyName}`);
).toThrow(
`Entity field name not allowed to prevent conflicts with standard Object prototype fields: ${keyName}`
);
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2021",
"lib": ["es2021"],
"target": "es2022",
"lib": ["es2023"],
"module": "commonjs",
"sourceMap": true,
"moduleResolution": "node",
Expand Down

0 comments on commit 672e31a

Please sign in to comment.