我有这个问题架构:
// 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`."
您能否看到这些模式的任何问题,以了解为什么两个嵌入文档未能注册与角色或故事点相关的任何错误?
所以我通过在问题架构代码中使用此函数解决了这个问题:
issuesSchema.path('teamRoles').validate(function(teamRoles){
if(!teamRoles){return false}
else if(teamRoles.length === 0){return false}
return true;
}, 'There must be at least one role added to each issue');
现在您可以看到包含错误:
{
"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"
},
"teamRoles": {
"name": "ValidatorError",
"message": "There must be at least one role added to each issue",
"properties": {
"message": "There must be at least one role added to each issue",
"type": "user defined",
"path": "teamRoles",
"value": []
},
"kind": "user defined",
"path": "teamRoles",
"value": []
}
},
"_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., teamRoles: There must be at least one role added to each issue"
}