mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Override casting logic for individual schema path

Open vkarpov15 opened this issue 6 years ago • 4 comments

Re: #7045, #8300

const schema = new Schema({
  myId: {
    type: ObjectId,
    cast: false // Disable casting just for this path
  },
  myNum: {
    type: Number,
    cast: v => { const ret = +v; assert.equal(typeof ret, 'number'); return ret; } // Override casting just for this path
  }
});

vkarpov15 avatar Dec 07 '19 17:12 vkarpov15

hello, @vkarpov15 ,not used this setting, const schema = new Schema({ myId: { type: String, cast: false // Disable casting just for this path } }); how to disable 'myId' not cast .eg: new schema(myId: 123)

hughdev avatar Jul 30 '20 08:07 hughdev

@hughdev this feature is not implemented yet, that’s why this issue is still open :)

Can you please elaborate what you’re trying to accomplish with this functionality?

vkarpov15 avatar Aug 04 '20 23:08 vkarpov15

Hi, good day, also having this issue. I saw previous issues and saw that they solved the case for mongoose.Number.cast(false) but not for mongoose.String.cast(false).

I believe it is also important to extend this functionality also to String. My example is simple, I have a unit test which tests if field is number instead of string, this should throw an error, but instead it passes because before it is saved, the number is casted to a string.

SirMauri avatar Aug 17 '20 22:08 SirMauri

@SirMauri this functionality hasn't been implemented yet. We will implement it for the next minor release.

vkarpov15 avatar Aug 26 '20 00:08 vkarpov15