【问题标题】:Mongoose - Validate ObjectID related documentMongoose - 验证 ObjectID 相关文档
【发布时间】:2020-05-22 09:25:54
【问题描述】:

我需要根据需要验证模型事件中的“产品”字段。产品是对产品模型的 ObjectID 引用。

我尝试了这两种方法,但它没有验证

 product: {
    type: [{
      type: Schema.Types.ObjectId,
      ref: 'Product',
      required: true
    }]
  },



product: {
    type: [{
      type: Schema.Types.ObjectId,
      ref: 'Product',
      required: function () {
        return this.product.length > 0
      },
    }]
  },

无论如何都在创建事件,当我不添加任何产品时,字段产品是一个空数组。

知道如何验证它吗?

型号:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Product = require('../models/Product');

const moment = require('moment');


const EventSchema = new Schema({

  client: {
    type: [{
      type: Schema.Types.ObjectId,
      ref: 'Client'
    }]
  },

  product: {
    type: [{
      type: Schema.Types.ObjectId,
      ref: 'Product',
      required: true
    }]
  },

  date: {
    type: Date,
    maxlength: 64,
    lowercase: true,
    trim: true
  },

  place: {
    type: String,
    maxlength: 1200,
    minlength: 1,
  },

  price: {
    type: Number
  },

  comment: {
    type: String,
    maxlength: 12000,
    minlength: 1,
  },

  status: {
    type: Number,
    min: 0,
    max: 1,
    default: 0,
    validate: {
      validator: Number.isInteger,
      message: '{VALUE} is not an integer value'
    }
  },
},
  {
    toObject: { virtuals: true },
    toJSON: { virtuals: true }
  },
  {
    timestamps: true
  },
);


const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Provider = require('./Provider')


const ProductSchema = new Schema({

  name: {
    type: String,
    maxlength: 64,
    minlength: 1,
    required: [true, 'Product name is required'],
  },

  brand: {
    type: String,
    maxlength: 64,
    minlength: 1,
  },

  description: {
    type: String,
    maxlength: 12000,
    min: 1,
  },

  comment: {
    type: String,
    maxlength: 12000,
    minlength: 1
  },

  state: {
    type: String,
    maxlength: 64,
    minlength: 0
  },

  disponible: {
    type: Boolean,
    default: true
  },

  price: {
    type: Number,
    default: 0,
    min: 0,
    max: 999999
  },

  provider: {
    type: [{
      type: Schema.Types.ObjectId,
      ref: 'Provider'
    }]
  },

  category: {
    type: [{
      type: Schema.Types.ObjectId,
      ref: 'Category'
    }]
  },

  event: {
    type: [{
      type: Schema.Types.ObjectId,
      ref: 'Event'
    }]
  },

  image: {
    type: [{
      type: Schema.Types.ObjectId,
      ref: 'Image'
    }]
  },
},
  {
    toObject: { virtuals: true },
    toJSON: { virtuals: true }
  },
  {
    timestamps: true
  });

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

    您可以使用猫鼬的custom validators功能。

    如果验证器函数返回未定义或真值,则验证成功。如果返回 falsy(未定义除外)或抛出错误,则验证失败。

        product: {
          type: [
            {
              type: Schema.Types.ObjectId,
              ref: "Product",
              required: true
            }
          ],
          validate: {
            validator: function(v) {
              return v !== null && v.length > 0;
            },
            message: props => "product is null or empty"
          }
        }
    

    现在,当您不发送产品字段或发送空数组时,会出现验证错误。

    【讨论】:

    • 谢谢!完美地工作。您能否简要解释一下验证器是如何工作的?同样在消息中,道具返回我未定义...为什么?
    • @nacho 不客气,我用简短的解释更新了答案。我还使用静态消息更新了消息功能。在这种情况下,动态消息不是很适用,因为 props.value 为 null 或空数组。
    【解决方案2】:
     const notEmpty = function(users){
       if(users.length === 0){return false}
       else { return true }
     }
    
     const EventSchema = new Schema({
      product: {
        type: [{
          type: Schema.Types.ObjectId,
          ref: 'Product',
          required: true,
          validate: [notEmpty, 'Please add at least one']
        }]
      }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-11
      • 2022-01-15
      • 2019-06-03
      • 2014-09-15
      • 2012-09-11
      • 1970-01-01
      • 2014-08-27
      • 2013-12-28
      相关资源
      最近更新 更多