-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: insert methods removing _ids #55
Fix: insert methods removing _ids #55
Conversation
test/zod-collection.test.ts
Outdated
const id = new ObjectId() | ||
const { insertedId } = await zodCol.insertOne({ | ||
a: 0, | ||
b: 'string', | ||
numbers: [0], | ||
_id: id, | ||
}) | ||
expect(insertedId.equals(id)).toBeTruthy() | ||
|
||
expect(() => | ||
zodCol.insertOne({ a: 'string', b: 'string', numbers: [0], _id: null }) | ||
).toThrow() | ||
|
||
expect(() => | ||
zodCol.insertOne({ a: 'string', b: 'string', numbers: [0], _id: 'idStr' }) | ||
).toThrow() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be more clear in a separate test "Should allow inserting documents with _id
even if not in schema"
a: 0, | ||
b: 'string', | ||
numbers: [0], | ||
_id: id, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also assert that a wrong input fails here both type wise and at runtime.
We can add a test where we pass a string to the _id
field which can be a common mistake
src/zod-collection.ts
Outdated
_id: z | ||
// Can't be instanceof(ObjectId) because the instanceof operator | ||
// doesn't work correctly when ts-mongo is imported by another lib | ||
// This is similar to what we do with zod instance checking |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to what we do with zod instance checking, where?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, in this zod
fork that is maintained in my GitHub account: cau777/zod@ee7f929
We use it instead of the official version of zod
: https://github.com/cau777/ts-mongo/blob/29ca813679e62731df514b97f91d18d773155930/package.json#L51-L51
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add this permalink as a comment here, to avoid any confusions:
https://github.com/cau777/zod/blob/ee7f929f6b145721e5f79ad8ba7a2357d32053f6/src/types.ts#L206-L226
} | ||
|
||
test('Documents should be parsed correctly on insert', async () => { | ||
const zodCol = await mkZodCollection() | ||
|
||
expect(() => | ||
// @ts-expect-error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 this is better than using any
test/zod-collection.test.ts
Outdated
expect(() => zodCol.insertMany([{ a: 0, b: 0 }])).toThrow() | ||
await expect( | ||
zodCol.insertMany([{ a: 0, b: 'string', numbers: [7] }]) | ||
).resolves.toBeTruthy() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should keep / add a test that uses insertMany
just to ensure the validation works there as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for noticing! Removing this was a mistake
The type system allows passing
_id
toinsertOne
andinsertMany
even if_id
is not in the Zod schema. However, our validation strips_id
out of the calls, which is unexpected.Before modifying
![image](https://private-user-images.githubusercontent.com/76797678/388174412-6731d503-c591-47ac-9d50-2ff1e55199eb.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk2MDgzOTYsIm5iZiI6MTczOTYwODA5NiwicGF0aCI6Ii83Njc5NzY3OC8zODgxNzQ0MTItNjczMWQ1MDMtYzU5MS00N2FjLTlkNTAtMmZmMWU1NTE5OWViLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE1VDA4MjgxNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWZjZDViYjNmNjhkMjdlOTJlODIyYWM1Y2QwMTRmYzE0Y2E0MjM1YzYxOTVlZTBhZDU0NzllZjIzMGRjYTJiZmQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.34viCxv340IIamGVAxxZqNSkGcFwc09PSC-a1QoW0Ns)
preInsert
:After:
![image](https://private-user-images.githubusercontent.com/76797678/388176744-27eb6be5-3b10-461b-a336-77b42d9803f2.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk2MDgzOTYsIm5iZiI6MTczOTYwODA5NiwicGF0aCI6Ii83Njc5NzY3OC8zODgxNzY3NDQtMjdlYjZiZTUtM2IxMC00NjFiLWEzMzYtNzdiNDJkOTgwM2YyLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE1VDA4MjgxNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWM3NjdjNmQyOThkNGMwY2RlNzgzMzNmZmJkN2IxZGYzYmQ4ZDk2NGFiMDRlNWRhYWE1MjYzNjJkNzdkNDc2NTAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.20tg-nQW0vWU3BTu_U96ZJBewBuSo-xERMx-uUzXhps)