为什么猫鼬模式验证会忽略引用的模型类型

时间:2021-02-28 09:15:15

标签: mongodb mongoose-schema

刚刚学习 MongoDB / Mongoose。

当我的架构具有特定模型类型实例的引用属性时,使用“ref”限定符时,我希望在使用不兼容的值时验证会失败。

但这似乎并非如此 - 任何模型类型都通过了测试。

'use strict';

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ObjectId = mongoose.ObjectId;

const Apple = mongoose.model(
    'Apple',
    new Schema({
        name: {
            type: String,
            required: true,
        },
    })
);

const Banana = mongoose.model(
    'Banana',
    new Schema({
        weight: {
            type: Number,
            required: true,
        },
        color: {
            type: String,
            required: true,
        },
    })
);

const Dinner = mongoose.model(
    'Dinner',
    new Schema({
        fruit: {
            type: ObjectId,
            ref: 'Apple',
            required: true,
        },
    })
);

function showValidationErrors(ERR) {
    const errors = ERR.errors;
    for (let propName in errors) {
        console.log('  ' + propName, errors[propName].kind);
    }
}

function doValidation(model, object, expectingSuccess) {
    console.log('----------');
    const ERR = object.validateSync();
    if (ERR) {
        console.log(model.modelName + ' validation failed');
        showValidationErrors(ERR);
        if (expectingSuccess) {
            console.log(' --> expected validation to succeed');
        }
    } else {
        console.log(model.modelName + ' validation succeeded');
        if (!expectingSuccess) {
            console.log(' --> expected validation to fail');
        }
    }
}

// will validate - all parameters set correctly
const apple = new Apple({ name: 'Norwegian Blue' });
doValidation(Apple, apple, true);

// will validate - all parameters set correctly
const banana = new Banana({ name: 'Bob', color: 'yellow', weight: 17.5 });
doValidation(Banana, banana, true);

// will fail as expected - fruit has wrong type
const dinner1 = new Dinner({ fruit: 12 });
doValidation(Dinner, dinner1, false);

// will succeed as expected - fruit has correct type
const dinner2 = new Dinner({ fruit: apple });
doValidation(Dinner, dinner2, true);

// unexpected validation success - Dinner.fruit must be an apple
const dinner3 = new Dinner({ fruit: banana });
doValidation(Dinner, dinner3, false);

输出为:

----------
Apple validation succeeded
----------
Banana validation succeeded
----------
Dinner validation failed
  fruit ObjectId
----------
Dinner validation succeeded
----------
Dinner validation succeeded
 --> expected validation to fail

我可以向架构显式添加验证以获得预期结果:

const MoreSpecificDinner = mongoose.model(
    'MoreSpecificDinner',
    new Schema({
        fruit: {
            type: ObjectId,
            ref: 'Apple',
            required: true,
            validate: function (value) {
                if (!(value instanceof Apple)) {
                    this.invalidate(
                        'object',
                        'must be of type Apple',
                        this.object
                    );
                }
            },
        },
    })
);

但是这不应该已经被 ref: Apple 覆盖了吗?

经过一些调查:

看起来 ref 属性只定义了被引用对象所在的集合,而不是它的类型 - 有没有办法实现这一点,或者额外的验证方法是唯一的方法?

0 个答案:

没有答案
相关问题