Skip to content

Commit

Permalink
replaced schema types with generics
Browse files Browse the repository at this point in the history
  • Loading branch information
Omri-Levy committed Jun 3, 2024
1 parent 396c613 commit 612d4a4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
Binary file added ballerine-nestjs-typebox.tgz
Binary file not shown.
13 changes: 9 additions & 4 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ export function isSchemaValidator(type: any): type is SchemaValidator {
return type && typeof type === 'object' && typeof type.validate === 'function';
}

export function buildSchemaValidator(config: SchemaValidatorConfig): SchemaValidator {
const { type, schema, coerceTypes, stripUnknownProps, name, required } = config;

export function buildSchemaValidator<TTSchema extends TSchema>({
type,
schema,
coerceTypes,
stripUnknownProps,
name,
required,
}: SchemaValidatorConfig<TTSchema>): SchemaValidator<TTSchema> {
if (!type) {
throw new Error('Validator missing "type".');
}
Expand All @@ -44,7 +49,7 @@ export function buildSchemaValidator(config: SchemaValidatorConfig): SchemaValid
throw new Error(`Validator "${name}" expects a TypeBox schema.`);
}

const check = ajv.compile(schema);
const check = ajv.compile<Static<TTSchema>>(schema);

return {
schema,
Expand Down
53 changes: 30 additions & 23 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,68 +16,73 @@ export type MethodDecorator<T extends Function = any> = (
) => TypedPropertyDescriptor<T> | void;

export interface HttpEndpointDecoratorConfig<
S extends TSchema = TSchema,
ResponseConfig extends ResponseValidatorConfig<S> = ResponseValidatorConfig<S>,
TTSchema extends TSchema = TSchema,
ResponseConfig extends ResponseValidatorConfig<TTSchema> = ResponseValidatorConfig<TTSchema>,
RequestConfigs extends RequestValidatorConfig[] = RequestValidatorConfig[],
> extends Omit<ApiOperationOptions, 'requestBody' | 'parameters'> {
method: 'GET' | 'POST' | 'PATCH' | 'DELETE' | 'PUT';
responseCode?: number;
path?: string;
validate?: ValidatorConfig<S, ResponseConfig, RequestConfigs>;
validate?: ValidatorConfig<TTSchema, ResponseConfig, RequestConfigs>;
}

export interface SchemaValidator<T extends TSchema = TSchema> {
schema: T;
export interface SchemaValidator<TTSchema extends TSchema = TSchema> {
schema: TTSchema;
name: string;
check: ValidateFunction;
validate(data: Obj | Obj[]): Static<T>;
check: ValidateFunction<Static<TTSchema>>;
validate(data: Obj | Obj[]): Static<TTSchema>;
}
export interface ValidatorConfigBase {
schema?: TSchema;
export interface ValidatorConfigBase<TTSchema extends TSchema> {
schema?: TTSchema;
coerceTypes?: boolean;
stripUnknownProps?: boolean;
name?: string;
required?: boolean;
pipes?: (PipeTransform | Type<PipeTransform>)[];
}
export interface ResponseValidatorConfig<T extends TSchema = TSchema> extends ValidatorConfigBase {
schema: T;
export interface ResponseValidatorConfig<TTSchema extends TSchema = TSchema> extends ValidatorConfigBase<TTSchema> {
schema: TTSchema;
type?: 'response';
responseCode?: number;
required?: true;
pipes?: never;
}

export interface ParamValidatorConfig extends ValidatorConfigBase {
schema?: TSchema;
export interface ParamValidatorConfig<TTSchema extends TSchema = TSchema> extends ValidatorConfigBase<TTSchema> {
schema?: TTSchema;
type: 'param';
name: string;
stripUnknownProps?: never;
}

export interface QueryValidatorConfig extends ValidatorConfigBase {
schema?: TSchema;
export interface QueryValidatorConfig<TTSchema extends TSchema = TSchema> extends ValidatorConfigBase<TTSchema> {
schema?: TTSchema;
type: 'query';
name: string;
stripUnknownProps?: never;
}

export interface BodyValidatorConfig extends ValidatorConfigBase {
schema: TSchema;
export interface BodyValidatorConfig<TTSchema extends TSchema = TSchema> extends ValidatorConfigBase<TTSchema> {
schema: TTSchema;
type: 'body';
}

export type RequestValidatorConfig = ParamValidatorConfig | QueryValidatorConfig | BodyValidatorConfig;
export type SchemaValidatorConfig = RequestValidatorConfig | ResponseValidatorConfig;
export type RequestValidatorConfig<TTSchema extends TSchema = TSchema> =
| ParamValidatorConfig<TTSchema>
| QueryValidatorConfig<TTSchema>
| BodyValidatorConfig<TTSchema>;
export type SchemaValidatorConfig<TTSchema extends TSchema = TSchema> =
| RequestValidatorConfig<TTSchema>
| ResponseValidatorConfig<TTSchema>;

export type ValidatorType = NonNullable<SchemaValidatorConfig['type']>;

export interface ValidatorConfig<
S extends TSchema,
ResponseConfig extends ResponseValidatorConfig<S>,
TTSchema extends TSchema,
ResponseConfig extends ResponseValidatorConfig<TTSchema>,
RequestConfigs extends RequestValidatorConfig[],
> {
response?: S | ResponseConfig;
response?: TTSchema | ResponseConfig;
request?: [...RequestConfigs];
}

Expand All @@ -91,4 +96,6 @@ export type RequestConfigsToTypes<RequestConfigs extends RequestValidatorConfig[
: string;
};

export type TPartialSome<T extends TSchema, K extends PropertyKey[]> = TComposite<[TOmit<T, K>, TPartial<TPick<T, K>>]>;
export type TPartialSome<TTSchema extends TSchema, K extends PropertyKey[]> = TComposite<
[TOmit<TTSchema, K>, TPartial<TPick<TTSchema, K>>]
>;

0 comments on commit 612d4a4

Please sign in to comment.