francescov1 / mongoose-tsgen

A plug-n-play Typescript generator for Mongoose.
105 stars 24 forks source link

Error when schema has a field of type Schema.Types.Buffer with a default of null #47

Closed Narretz closed 3 years ago

Narretz commented 3 years ago

Hi, frist of all, thanks for this package. This is exactly what I wanted since I don't want to touch my existing schema defs. :)

I think I found a bug related to Buffer in a schema:

Consider this

const homeSchema = new Schema(
  {
    id: String,
    image: {
     type: Schema.Types.Buffer,
     default: null
   }
});

For me. this throws an error during gen: TypeError: Cannot read property 'required' of null coming from here: https://github.com/francescov1/mongoose-tsgen/blob/854a1f9289bb07107622914bf8e76dbde202d2e5/src/helpers/parser.ts#L468

Now I don't know 100% if Buffer types even can have a default value null?

francescov1 commented 3 years ago

Hey @Narretz, thanks for submitting the issue!

I investigated and it seems that the way you defined the Buffer type differs slightly from the Mongoose docs, which is why I missed adding a check to handle this.

I will add support for your format, but if you want to continue using the tool in the meantime you can define your Buffer using the format described here. Let me know if this solves the issue! 😊

francescov1 commented 3 years ago

Fixed in 8.0.2 🚀

Narretz commented 3 years ago

Thanks, this is great. It does make sense to use just Buffer same as other built ins ...