Skip to content

Commit 5a28aca

Browse files
add changeset, test case and format
1 parent 4b091bb commit 5a28aca

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

.changeset/early-lines-feel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"monarch-orm": patch
3+
---
4+
5+
Fix update operators `$addToSet` failing schema validation when parsing data

src/schema/update.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ export function updateParser<T extends AnyMonarchType>(
8989
return input;
9090
}
9191

92-
function unwrapTo<T extends new (...args: any) => AnyMonarchType>(
93-
type: AnyMonarchType,
94-
target: T
95-
): InstanceType<T> {
92+
function unwrapTo<T extends new (...args: any) => AnyMonarchType>(type: AnyMonarchType, target: T): InstanceType<T> {
9693
let unwrapped: any = type;
9794
while (!(unwrapped instanceof target)) {
9895
if (unwrapped instanceof MonarchDefaulted) unwrapped = MonarchDefaulted.type(unwrapped);

tests/query/update.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@ describe("Update Operations", async () => {
299299
expect(updated?.tags).toEqual(["x", "y"]);
300300
});
301301

302+
it("$addToSet parses element through type", async () => {
303+
const doc = await db.collections.arrays.insertOne({ name: "a", tags: [], nums: [] });
304+
await db.collections.arrays.updateOne({ _id: doc._id }, { $addToSet: { tags: "UPPER" } });
305+
const updated = await db.collections.arrays.findOne({ _id: doc._id });
306+
expect(updated?.tags).toEqual(["upper"]);
307+
});
308+
302309
it("$push rejects non-array field", async () => {
303310
const doc = await db.collections.numeric.insertOne({ name: "a", count: 0 });
304311
// @ts-expect-error

0 commit comments

Comments
 (0)