模型中的摇摇欲坠定义

时间:2019-06-24 14:29:00

标签: node.js mongoose yaml swagger

我有这个数据模型:

"use strict";

const mongoose = require("mongoose");
const Schema = mongoose.Schema;


let ProviderSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  emailTemplate: {
    type: Schema.ObjectId,
    ref: "EmailTemplate",
    required: false
  }
});


let ProductsSchema = new Schema({
    brand: {
        type: Schema.ObjectId,
        ref: "Brand",
        required: true 
    },
    providers: {
        type: [ProviderSchema],
        required: false
    }
});


let ListSchema = new Schema({
  code: {
    type: String,
    unique: true,
    trim: true,
    required: true
  },
  products: {
    type: [ProductsSchema],
    default: [],
    required: true
  }
}, { collection: 'list' });


module.exports = mongoose.model("List", ListSchema);

但是当我尝试建立粗俗的定义时,我遇到了问题。如何定义模型的“产品”属性? 我尝试使用类型数组来完成此操作,但它使我无法进行全面验证,因为它构建得很差。我是个新手,定义这个很复杂

这是我的.yaml

required:
  - code
properties:
  code:
    type: string
  products:
    type: array
        items:
            $ref: '#/definitions/Brand'

1 个答案:

答案 0 :(得分:1)

缩进在YAML中很重要。确保itemstype: array处于同一级别。

required:
  - code
properties:
  code:
    type: string
  products:
    type: array
    items:      # <-------
      $ref: '#/definitions/Brand'