但是我能够尝试,您的示例有效。我使用了以下模式:
import { Schema } from 'mongoose';
const LicenseTime = new Schema({
from: {
type: Date,
required: true,
},
to: {
type: Date,
required: true,
},
},
{
_id: false, // Used so mongoose/mongo doesn't create id
id: false, // Used so mongoose/mongo doesn't create id
});
export const LicenseSchema = new Schema({
customer: {
type: String,
required: true,
},
appId: {
type: String,
required: true,
},
purchaseDate: {
type: Date,
required: true,
},
valid: {
type: LicenseTime,
required: true,
},
});
当我尝试保存其他内容时使用此架构:
{
customer: 'example customer',
appId: 'example appId',
purchaseDate: new Date(),
valid: {
to: new Date(),
from: new Date(),
},
}
保存失败。
示例:如果我尝试保存:
{
customer: 'example customer',
appId: 'example appId',
purchaseDate: new Date(),
valid: {
to: new Date(),
},
}
代码失败:
[Nest] 8895 - 2019 年 6 月 12 日下午 1:42 [ExceptionsHandler] Cat 验证失败:有效。来自:需要路径 from。有效:验证失败:来自:需要路径 from . +829 毫秒
[0] ValidationError:Cat 验证失败:valid.from:路径from 是必需的。有效:验证失败:来自:路径from 是必需的。
[0] 在新的 ValidationError (/home/adrian/work/try/node_modules/mongoose/lib/error/validation.js:30:11)
[0] 在 model.Document.invalidate (/home/adrian/work/try/node_modules/mongoose/lib/document.js:2260:32)
[0] 在 SingleNested.Subdocument.invalidate (/home/adrian/work/try/node_modules/mongoose/lib/types/subdocument.js:147:18)
[0] 在 p.doValidate.skipSchemaValidators (/home/adrian/work/try/node_modules/mongoose/lib/document.js:2109:17)
[0] 在 /home/adrian/work/try/node_modules/mongoose/lib/schematype.js:981:9
[0] 在 processTicksAndRejections (internal/process/task_queues.js:82:9)
最后,我使用了以下依赖项:
{
"dependencies": {
"@nestjs/common": "^6.0.0",
"@nestjs/core": "^6.0.0",
"@nestjs/mongoose": "^6.1.2",
"@nestjs/platform-express": "^6.0.0",
"mongoose": "^5.5.14",
"reflect-metadata": "^0.1.12",
"rimraf": "^2.6.2",
"rxjs": "^6.3.3"
},
"devDependencies": {
"@nestjs/testing": "^6.0.0",
"@types/express": "^4.16.0",
"@types/jest": "^23.3.13",
"@types/mongoose": "^5.5.6",
"@types/node": "^10.12.18",
"@types/supertest": "^2.0.7",
"concurrently": "^4.1.0",
"jest": "^23.6.0",
"nodemon": "^1.18.9",
"prettier": "^1.15.3",
"supertest": "^3.4.1",
"ts-jest": "24.0.2",
"ts-node": "8.1.0",
"tsconfig-paths": "3.8.0",
"tslint": "5.16.0",
"typescript": "3.4.3",
"wait-on": "^3.2.0"
},
}