[英]What is the difference between Joi.object() and Joi.object().keys()?
[英]Joi getting validationSchema: Joi.object() error
在nestjs
框架上安裝joi
后,我試圖通過joi
驗證兩個屬性,例如:
import Joi from 'joi';
...
ConfigModule.forRoot({
isGlobal: true,
validationSchema: Joi.object(
{
PORT: Joi.number().required(),
MONGODB_URI: Joi.string().required(),
}
)
}),
...
但我收到此錯誤:
Cannot read properties of undefined (reading 'object')
main.ts
:
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// @ts-ignore
app.useGlobalFilters(new ValidationPipe());
const configService = app.get(ConfigService);
const port = configService.get<string>('PORT');
await app.listen(port);
}
完整錯誤:
D:\develop\api\src\app.module.ts:16
validationSchema: Joi.object(
^
TypeError: Cannot read properties of undefined (reading 'object')
at Object.<anonymous> (D:\develop\api\src\app.module.ts:16:35)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (D:\develop\api\src\main.ts:2:1)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
PS D:\develop\api>
在 tsconfig.json 中使用import * as Joi from 'joi'
或設置tsconfig.json
esModuleInterop: true
。 Joi 默認不使用默認導出,因此需要使用命名/命名空間導入(第一個選項),或者您需要告訴 Typescript 以類似方式處理默認和命名導入(第二個選項)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.