forked from jayalfredprufrock/nestjs-typebox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: BREAKING CHANGE - upgrade to latest typebox
- Loading branch information
1 parent
c658d7b
commit 47426f3
Showing
9 changed files
with
1,128 additions
and
5,459 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"mode": "pre", | ||
"tag": "next", | ||
"initialVersions": { | ||
"nestjs-typebox": "2.6.1" | ||
}, | ||
"changesets": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import type { PipeTransform, Type } from '@nestjs/common'; | ||
import type { TypeCheck } from '@sinclair/typebox/compiler'; | ||
import type { Static, TSchema } from '@sinclair/typebox/type'; | ||
|
||
export type AllKeys<T> = T extends unknown ? Exclude<keyof T, symbol> : never; | ||
|
||
export type Obj<T = unknown> = Record<string, T>; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any | ||
export type MethodDecorator<T extends Function = any> = ( | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
target: Object, | ||
propertyKey: string | symbol, | ||
descriptor: TypedPropertyDescriptor<T> | ||
) => TypedPropertyDescriptor<T> | void; | ||
|
||
export interface SchemaValidator<T extends TSchema = TSchema> { | ||
schema: T; | ||
name: string; | ||
check: TypeCheck<T>['Check']; | ||
validate(data: Obj | Obj[]): Static<T>; | ||
} | ||
export interface ValidatorConfigBase { | ||
schema?: TSchema; | ||
coerceTypes?: boolean; | ||
stripUnknownProps?: boolean; | ||
name?: string; | ||
required?: boolean; | ||
pipes?: (PipeTransform | Type<PipeTransform>)[]; | ||
} | ||
export interface ResponseValidatorConfig<T extends TSchema = TSchema> extends ValidatorConfigBase { | ||
schema: T; | ||
type?: 'response'; | ||
responseCode?: number; | ||
required?: true; | ||
pipes?: never; | ||
} | ||
|
||
export interface ParamValidatorConfig extends ValidatorConfigBase { | ||
schema?: TSchema; | ||
type: 'param'; | ||
name: string; | ||
stripUnknownProps?: never; | ||
} | ||
|
||
export interface QueryValidatorConfig extends ValidatorConfigBase { | ||
schema?: TSchema; | ||
type: 'query'; | ||
name: string; | ||
stripUnknownProps?: never; | ||
} | ||
|
||
export interface BodyValidatorConfig extends ValidatorConfigBase { | ||
schema: TSchema; | ||
type: 'body'; | ||
} | ||
|
||
export type RequestValidatorConfig = ParamValidatorConfig | QueryValidatorConfig | BodyValidatorConfig; | ||
export type SchemaValidatorConfig = RequestValidatorConfig | ResponseValidatorConfig; | ||
|
||
export type ValidatorType = NonNullable<SchemaValidatorConfig['type']>; | ||
|
||
export interface ValidatorConfig< | ||
S extends TSchema, | ||
ResponseConfig extends ResponseValidatorConfig<S>, | ||
RequestConfigs extends RequestValidatorConfig[], | ||
> { | ||
response?: S | ResponseConfig; | ||
request?: [...RequestConfigs]; | ||
} | ||
|
||
export type RequestConfigsToTypes<RequestConfigs extends RequestValidatorConfig[]> = { | ||
[K in keyof RequestConfigs]: RequestConfigs[K]['required'] extends false | ||
? RequestConfigs[K]['schema'] extends TSchema | ||
? Static<RequestConfigs[K]['schema']> | undefined | ||
: string | undefined | ||
: RequestConfigs[K]['schema'] extends TSchema | ||
? Static<RequestConfigs[K]['schema']> | ||
: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters