Но то, что я смог попробовать, твой пример работает.Я использовал следующие схемы:
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(),
},
}
код не удался с:
[Гнездо] 8895 - 12/12/2019, 13:42 [ExceptionsHandler] Ошибка проверки кошки: действительная.from: Требуется путь from
, действительный: проверка не пройдена: from: Требуется путь from
.+ 829ms [0] ValidationError: Ошибка проверки Cat: valid.from: путь 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] at processTicksAndRejected (внутренняя / 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"
},
}