Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
Objeto anidado en esquema Joi

He definido el esquema de validación a través de Joi con un objeto anidado en el valor de AWS:

 const schema = Joi.object({ NODE_ENV: Joi.string() .valid('development', 'production', 'test') .default('development'), PORT: Joi.number().default(3000), AWS: Joi.object({ accessKeyId: Joi.string().required(), secretAccessKey: Joi.string().required(), region: Joi.string().required(), bucket: Joi.string().required(), }).required(), });

Luego puse mi esquema en el módulo de configuración.

 @Module({ imports: [ ConfigModule.forRoot({ isGlobal: true, validationSchema: schema, validationOptions: { abortEarly: false, cache: false, }, }), FilesModule, UsersModule, PostsModule, SharedModule, ], controllers: [AppController], providers: [AppService], }) export class AppModule {}

He agregado dentro del archivo .env el siguiente valor para la variable AWS:

 AWS={"region": "string", "accessKeyId":"string", "secretAccessKey": "string", "bucket": "string"}

pero recibí el siguiente mensaje de error después de iniciar nest:

 > project-8v@0.0.1 start /Volumes/MacDATA/NestJs/project-8v > nest start /Volumes/MacDATA/Lern/NestJs/project-8v/node_modules/@nestjs/config/dist/config.module.js:66 throw new Error(`Config validation error: ${error.message}`); ^ Error: Config validation error: "AWS" must be of type object

typeof process.env.AWS devuelve una cadena y Joi no entiende que debe analizarla, tal vez necesito agregar algo en las opciones de validación o me pierdo algo. ¿Cómo puedo resolverlo?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A partir de Joi v16.0.0 , la coerción de cadenas de objetos y matrices ya no está disponible como una opción integrada.

Puede replicar esta funcionalidad extendiendo Joi. Por ejemplo:

 const JoiCustom = Joi.extend({ type: 'object', base: Joi.object(), coerce: { from: 'string', method(value) { if (value[0] !== '{' && !/^\s*{/.test(value)) { return { value }; } try { return { value: JSON.parse(value) }; } catch (error) { return { errors: [error] }; } } } });

Luego use JoiCustom en su esquema:

 const schema = Joi.object({ ... AWS: JoiCustom.object({ ... }).required() });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!