我有这个问题模式:
// Require Mongoose
import mongoose from "mongoose";
import { rolesSchema } from "./roles.js";
import { storyPointSchema } from "./storyPoint.js";
// Define a schema
const Schema = mongoose.Schema;
/**
* Issues Report Schema
*/
export const issuesSchema = new Schema({
team: {
required: true,
type: String,
},
teamRoles: {
type: [rolesSchema],
required: true
},
ticketId: {
type: String,
required: true
},
issueName: {
type: String,
required: true,
},
description: {
type: String,
required: true
},
issueType: {
type: String,
enum: ['story', 'bug', 'task', 'sub-task', 'epic'],
default: 'story',
required: true
},
storyPoints: {
accepted: storyPointSchema,
committed: storyPointSchema,
completed: storyPointSchema,
estimated: storyPointSchema,
actual: storyPointSchema
}
}, {_id: false})
/**
* Calculate Full Name
* Virtual for ticket assignee's full name
*/
issuesSchema.virtual('fullname').get(function () {
let fullName = ""
if (this.firstName && this.lastName) {
fullName = `${this.firstName}, ${this.lastName}`
}
return fullName
})
export const Issues = mongoose.model('Issues', issuesSchema)
字符串
下面是两个嵌入的文档模式:
export const rolesSchema = new Schema({
firstName: {
type: String,
required: true
},
lastName:{
type: String,
required: true
},
role: {
type:String,
required: true
}
})
export const Roles = mongoose.model('Roles', rolesSchema)
// Require Mongoose
import mongoose from "mongoose";
// Define a schema
const Schema = mongoose.Schema;
/**
* Story Point Report Schema
*/
export const storyPointSchema = new Schema([{
storyPoint: {
type: Number,
required: true,
enum: [0,1, 2, 3, 5, 8, 13]
}
}])
型
在单元测试中,我可以得到验证器在需要数据丢失或格式错误时抛出的所有错误
以下是验证错误:
{
"errors": {
"description": {
"name": "ValidatorError",
"message": "Path `description` is required.",
"properties": {
"message": "Path `description` is required.",
"type": "required",
"path": "description"
},
"kind": "required",
"path": "description"
},
"issueName": {
"name": "ValidatorError",
"message": "Path `issueName` is required.",
"properties": {
"message": "Path `issueName` is required.",
"type": "required",
"path": "issueName"
},
"kind": "required",
"path": "issueName"
},
"ticketId": {
"name": "ValidatorError",
"message": "Path `ticketId` is required.",
"properties": {
"message": "Path `ticketId` is required.",
"type": "required",
"path": "ticketId"
},
"kind": "required",
"path": "ticketId"
},
"team": {
"name": "ValidatorError",
"message": "Path `team` is required.",
"properties": {
"message": "Path `team` is required.",
"type": "required",
"path": "team"
},
"kind": "required",
"path": "team"
},
"issueType": {
"name": "ValidatorError",
"message": "`foo` is not a valid enum value for path `issueType`.",
"properties": {
"message": "`foo` is not a valid enum value for path `issueType`.",
"type": "enum",
"enumValues": [
"story",
"bug",
"task",
"sub-task",
"epic"
],
"path": "issueType",
"value": "foo"
},
"kind": "enum",
"path": "issueType",
"value": "foo"
}
},
"_message": "Issues validation failed",
"name": "ValidationError",
"message": "Issues validation failed: description: Path `description` is required., issueName: Path `issueName` is required., ticketId: Path `ticketId` is required., team: Path `team` is required., issueType: `foo` is not a valid enum value for path `issueType`."
型
您是否发现了这些模式的任何问题,以了解为什么两个嵌入的文档都无法注册与角色或故事点相关的任何错误?
3条答案
按热度按时间rjee0c151#
所以我在issueSchema代码中使用这个函数验证了这个问题:
字符串
现在你可以看到错误包括在内:
型
2w2cym1i2#
下面是使用保存()生成错误的测试:
字符串
vyswwuz23#
为了让默认值或任何验证工作的故事点,我不得不扁平化这个模式(根据Mongoose的文档,它说嵌入的子文档可能是棘手的,因为他们实际上不属于文档,所以验证不会工作。
以下是更新后的storyPointSchema:
字符串
然后问题模式是:
型
现在有相应的错误:
型
我希望这对其他人有帮助:耸耸肩”