fix(middleware-joi): fixed type inference on `Joi.Object`
MODIFY
packages/middleware-joi/src/validator.interface.ts
packages/middleware-joi/src/validator.interface.ts
import * as Joi from 'joi';
import { Schema } from './validator.schema';
-export type ExtractedSchema<SchemaToExtract extends Schema> = {
- [K1 in keyof SchemaToExtract]:
- SchemaToExtract[K1] extends undefined
- ? unknown
- : { [K2 in keyof SchemaToExtract[K1]]: Joi.ExtractType<SchemaToExtract[K1][K2]> }
-};
+export type ExtractedBody<T extends Schema> = ExtractObject<T['body']>;
+export type ExtractedParams<T extends Schema> = ExtractObject<T['params']>;
+export type ExtractedQuery<T extends Schema> = ExtractObject<T['query']>;
-export type ExtractedBody<T extends Schema> = ExtractedSchema<T>['body'];
-export type ExtractedParams<T extends Schema> = ExtractedSchema<T>['params'];
-export type ExtractedQuery<T extends Schema> = ExtractedSchema<T>['query'];
+export type ExtractObject<T> =
+ T extends Joi.ObjectSchema
+ ? ExtractType<T>
+ : { [K in keyof T]: ExtractType<T[K]> };
+
+export type ExtractType<T> =
+ T extends Joi.NumberSchema ? number :
+ T extends Joi.BooleanSchema ? boolean :
+ T extends Joi.StringSchema ? string :
+ T extends Joi.DateSchema ? Date :
+ T extends Joi.ObjectSchema<infer U> ?
+ U extends null ? Record<string, any> : { [K in keyof U]: ExtractType<U[K]> } :
+ T extends Joi.ArraySchema<infer U> ?
+ U extends null ? any[] : U[] :
+ unknown;
declare module 'joi' {
// Object Schema
@@ -24,16 +33,4 @@ declare module 'joi' {
export interface ArraySchema<T = null> {
items<T>(schema: T): ArraySchema<ExtractType<T>>;
}
-
- // ExtractType
- export type ExtractType<T> =
- T extends NumberSchema ? number :
- T extends BooleanSchema ? boolean :
- T extends StringSchema ? string :
- T extends DateSchema ? Date :
- T extends ObjectSchema<infer U> ?
- U extends null ? Record<string, any> : { [K in keyof U]: ExtractType<U[K]> } :
- T extends ArraySchema<infer U> ?
- U extends null ? any[] : U[] :
- unknown;
}

MODIFY
packages/middleware-joi/test/helpers/api.spec-util.ts
packages/middleware-joi/test/helpers/api.spec-util.ts
@@ -9,7 +9,7 @@ const getPost$ = EffectFactory
.use(req$ => req$
.pipe(
use(validator$({
- query: { page: Joi.number().integer().greater(1) }
+ query: Joi.object({ page: Joi.number().integer().greater(1) })
})),
map(req => req.query),
map(query => ({ status: 200, body: { ...query } }))
@@ -23,7 +23,7 @@ const getUser$ = EffectFactory
use(validator$({
params: {
id: Joi.number().integer().min(1).max(10).required(),
- }
+ },
})),
map(req => req.params),
map(params => ({ status: 200, body: { id: params.id } }))
@@ -35,8 +35,8 @@ const storePost$ = EffectFactory
.use(req$ => req$
.pipe(
use(validator$({
- query: { timestamp: Joi.date().required() },
- body: { title: Joi.string().required() },
+ query: Joi.object({ timestamp: Joi.date().required() }),
+ body: Joi.object({ title: Joi.string().required() }),
})),
map(resp => ({ body: { ...resp.body, ...resp.query } }))
));
@@ -47,10 +47,10 @@ const postUser$ = EffectFactory
.use(req$ => req$
.pipe(
use(validator$({
- body: {
+ body: Joi.object({
name: Joi.string().required(),
passport: Joi.string().default('marble.js')
- }
+ })
})),
map(req => req.body),
map(response => ({ body: response }))

fix(middleware-joi): fixed type inference on `Joi.Object`