Skip to content

Commit

Permalink
fix: Prevent setting over properties. (#1075)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenh committed May 7, 2024
1 parent a695d1e commit e1c9351
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/orm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getBaseMeta, getMetadata } from "./EntityMetadata";
import { setSyncDefaults } from "./defaults";
import { getProperties } from "./getProperties";
import { isAllSqlPaths } from "./loadLens";
import { isAsyncProperty, isReactiveField, isReactiveGetter, isReactiveQueryField } from "./relations";
import { AbstractRelationImpl } from "./relations/AbstractRelationImpl";
import { TimestampSerde } from "./serde";
import { fail } from "./utils";
Expand Down Expand Up @@ -196,6 +197,13 @@ export function setOpts<T extends Entity>(
} else {
current.set(value);
}
} else if (
isAsyncProperty(current) ||
isReactiveField(current) ||
isReactiveGetter(current) ||
isReactiveQueryField(current)
) {
throw new Error(`Invalid argument, cannot set over ${key} ${current.constructor.name}`);
} else {
(entity as any)[key] = value;
}
Expand Down
24 changes: 24 additions & 0 deletions packages/tests/integration/src/EntityManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,30 @@ describe("EntityManager", () => {
await expect(em.flush()).rejects.toThrow("firstName is required");
});

it("cannot set over an async field", async () => {
const em = newEntityManager();
const a1 = em.create(Author, { firstName: "a1" });
expect(() => {
a1.set({ latestComments: [] } as any);
}).toThrow("Invalid argument, cannot set over latestComments AsyncPropertyImpl");
});

it("cannot set over an hasOneDerived relation", async () => {
const em = newEntityManager();
const a1 = em.create(Author, { firstName: "a1" });
expect(() => {
a1.set({ latestComment: [] } as any);
}).toThrow("'set' not implemented on CustomReference");
});

it("cannot set over a reactive field", async () => {
const em = newEntityManager();
const a1 = em.create(Author, { firstName: "a1" });
expect(() => {
a1.set({ numberOfPublicReviews: 2 } as any);
}).toThrow("Invalid argument, cannot set over numberOfPublicReviews ReactiveFieldImpl");
});

it("can setPartial with a null required field", async () => {
const em = newEntityManager();
const a1 = em.create(Author, { firstName: "a1" });
Expand Down

0 comments on commit e1c9351

Please sign in to comment.