Closed ADRFranklin closed 1 year ago
Hey @ADRFranklin thank you for the nice words.
Could you please provide the code that would lead to that error, or ideally the repository with a minimal reproduction?
What I tried is creating a new IssueDTOMongo
document using .save()
method, and it worked just fine. I then tried to retrieve that document back, and it did not populate the user
field (even though I also created a user with that ID) - likely because user
virtual clashes with the field with the same name? When I renamed user
virtual to something else, the population succeeded. Either way I haven't encountered any validation errors on my way.
Hi, I'm going to have to chalk this up to some crazy thing I must have done, cause I am no longer able to cause the validation errors I was getting. Since I posted this issue, I have done quite of bit of refactoring on my project, so it's totally possible I managed to fix the problem without really knowing it.
I had figured out a workaround which was to use the normal schema as the mongoose schema, as it didn't seem to error when returning populated fields that didn't match the schema, so then I would just re parse the output with the dto schema. But even changing back the data to what I previously had no longer gives errors, so it's probably safe to assume this was just user error and not the fault of the library,
Thanks anyway, if I do manage to trigger the same issue again, I will re-open with more info, just in case this might uncover some weird behaviour.
Hello There,
I really like this library and as a big user of zod, I was quite happy when I found a type safe way to deal with mongoose, so thank you for making it.
That being said, I have ran into a problem which I can not for the life of me figure out how to get around, that is if I try to use unions for my return objects the validation seems to think the field is empty.
initial schema
This schema is acts like a base schema, that is later extended by my DTO schema that is returned from the database, you'll notice that this schema has
user
set to just a string (this represents the reference from the db).The reason for extending was to get around the circular dependency issue, which I solved in this way by just extending it and overriding the initial field with a union, so the user schema can be be returned if it's populate method is used when pulling the data from the database, else it will just return the string as it would normally.
The problem is that no matter the combination, the validation errors out thinking the user field is undefined? Not sure if I have missed something in the documentation, or something long those lines.
Here is also the seed data I am using to test with.
Any help with this is appreciated. Thanks.