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

玉米片

我需要根据需要验证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
  });
苏莱曼·萨

您可以使用猫鼬的自定义验证器功能。

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章