猫鼬-验证与ObjectID相关的文档

时间:2020-02-06 10:46:26

标签: mongodb mongoose

我需要根据需要验证Model Event中的“产品”字段。产品是对产品模型的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
  });

2 个答案:

答案 0 :(得分:1)

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

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

    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"
      }
    }

现在,当您不发送产品字段或将其发送为空数组时,它将给出验证错误。

答案 1 :(得分:0)

 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']
    }]
  }
})