Comments (6)
I am having this issue. Any news regarding fixing it?
I also cannot make "unique: true" to work at all - duplicates can be inserted into the database even with this property set. The index on the actual column is created, but not marked as unique. Any ideas?
My package.json says I am using the following version:
"typegoose": "^3.4.5"
from typegoose.
This is happening with @arrayProp() too. Can't use required: true unless the items type is a primitive one.
from typegoose.
I am able to reproduce this, and will investigate this asap. Thanks for your report!
from typegoose.
I have been digging a little in your code so I could give you more light on this...
How is your lib working right now:
You can't mark required to any nested element. And If any nested element has any of its attributes marked as required, then the parent is also required automatically. This is bad... since you cant make a nested element optional if one of his attributes is marked as required.
I tried with the following change in props.ts for the not-primitive not-array flow:
return schema[name][key] = {
...schema[name][key],
...options,
type: {...subSchema}, // changes here!
};
// this will transpile to { type: subSchema }
With this change I can use @prop({required: true}) over nested elements and they validate their existence just fine. But this generates a problem: there is now no effect in marking require on any of the attributes of the nested element. In Brief: We can now test if the element exists, but not their inner elements.
After investigating a little, I see this is a problem regarding with mongoose, since if you have this..:
const UserSchema = new Schema({
child: { type: {attr1: { type: String, required: true}, attr2: String}, required: true}
});
..the behaviour is the same: it ignores completely the attr1's required flag.
This seems to be an old issue (or bad design): Automattic/mongoose#1860
Possible solution:
This problem is only related with explicit embedded documents. If you use sub-documents instead...:
const childSchema = new Schema({
attr1: { type: String, required: true },
attr2: String
});
const UserSchema = new Schema({
firstName: { type: String, required: true },
child: { type: childSchema, required: true}
});
..the behaviour is the expected one: you have control over the whole sub-doc and the sub-attributes separately.
So... it seems that your implementation mimics the embedded documents way when dealing with nested documents. I think the solution would be to implement it as the sub-documents way. I tried to do it but I think I'm not following your code right, since I'm getting..:
TypeError: Undefined type Job
at job
..where Job is the child prop, you know, like in your example.
So well, I hope all this help you debugging this bug/feature.
note: The only posibble downside of implementing this, is that using sub-documents you get an _id field for the child sub document... Really not a big concern here.
note2: I have not tested the "type: {...subSchema}" solution for the case of Arrays... I think the solution there should be similar, but not quite sure. Anyway, as I already said, with this solution you will still get a mimic for the embedded documents style (but fully functional, without the actual crashes). And I think that the final solution should not be that... since it would be great having granular control just like when using sub-documents.
Cheers!
from typegoose.
Hello, thanks for your investigation. I was going down the same path as you did however I couldn't craft a good solution at the time of writing. I will try to get back to it and figure this out.
from typegoose.
@szokodiakos take a look at my PR. The hints from @JoseLuisGarciaOtt got it working. Please check it out :)
from typegoose.
Related Issues (20)
- Please use the new Repository to open new issues HOT 4
- Pre hook supplied function error HOT 56
- TypeError: Class constructor Typegoose cannot be invoked without 'new' HOT 1
- [Request] Setting Collection Name and other Schema options via annotation and reflections
- [Question] Import ObjectId from Typegoose? HOT 4
- Please archive this repo
- Is there any way to achieve schema inheritance with typegoose? HOT 2
- [Question] Type of User.x property is not a y HOT 1
- Is it possible to use getModelForClass with generics? HOT 1
- [Question] HOT 1
- my populate field cant be queried out HOT 3
- [Question] HOT 1
- [Question] Problems reusing the same schema for both collection and subdocument HOT 1
- [Question] Can we add custom error messages when @prop options fail? HOT 2
- [Question] Are transactions supported for deleteMany? HOT 1
- [Question] Possible to have Query type support? HOT 1
- bi-directional virtuals population issue HOT 1
- Cannot use same model on two connections. HOT 2
- [Question] Is there a `required if` feature on model classes? HOT 2
- Both updatedBy and CreatedBy are getting updated after update operation on model object HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typegoose.