以下是相关的架构路径:

foo : {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Foo',
    validate: {
        validator: mongoose.Types.ObjectId.isValid,
        type: 'custom',
        message: 'proper foo is required'
    }
},
bar {
    type: Number,
    required: 'bar should be specified'
}

foo 作为空字符串(默认值)提交时,将抛出验证错误:

“在路径”foo“处”转换为ObjectID因值“而失败”...

在这种情况下,不会触发验证器 . 即使 validator 更改为 () => false ,它也没有机会提供可读的 message .

但是当提供 null 而不是空字符串时它会变好 . 如果只能通过验证来处理,我宁愿避免预处理 foo .

数字字段中出现相同的错误 bar

对于路径“bar”处的值“undefined”,转换为数字失败...

为什么没有触发验证器?这种行为是否可配置?如何在这里处理不正确的数据类型,因此可以给出人性化的消息?