Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/common/pipes/file/parse-file.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ParseFileOptions } from './parse-file-options.interface';
* @publicApi
*/
@Injectable()
export class ParseFilePipe implements PipeTransform<any> {
export class ParseFilePipe implements PipeTransform {
protected exceptionFactory: (error: string) => any;
private readonly validators: FileValidator[];
private readonly fileIsRequired: boolean;
Expand All @@ -38,7 +38,7 @@ export class ParseFilePipe implements PipeTransform<any> {
this.fileIsRequired = fileIsRequired ?? true;
}

async transform(value: any): Promise<any> {
async transform(value: unknown): Promise<any> {
const areThereAnyFilesIn = this.thereAreNoFilesIn(value);

if (areThereAnyFilesIn && this.fileIsRequired) {
Expand All @@ -51,29 +51,29 @@ export class ParseFilePipe implements PipeTransform<any> {
return value;
}

private async validateFilesOrFile(value: any): Promise<void> {
private async validateFilesOrFile(value: unknown): Promise<void> {
if (Array.isArray(value)) {
await Promise.all(value.map(f => this.validate(f)));
} else {
await this.validate(value);
}
}

private thereAreNoFilesIn(value: any): boolean {
private thereAreNoFilesIn(value: unknown): boolean {
const isEmptyArray = Array.isArray(value) && isEmpty(value);
const isEmptyObject = isObject(value) && isEmpty(Object.keys(value));
return isUndefined(value) || isEmptyArray || isEmptyObject;
}

protected async validate(file: any): Promise<any> {
protected async validate(file: unknown): Promise<any> {
for (const validator of this.validators) {
await this.validateOrThrow(file, validator);
}
return file;
}

private async validateOrThrow(file: any, validator: FileValidator) {
const isValid = await validator.isValid(file);
private async validateOrThrow(file: unknown, validator: FileValidator) {
const isValid = await validator.isValid(file as any);

if (!isValid) {
const errorMessage = validator.buildErrorMessage(file);
Expand Down
6 changes: 4 additions & 2 deletions packages/common/pipes/parse-array.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class ParseArrayPipe implements PipeTransform {
* @param value currently processed route argument
* @param metadata contains metadata about the currently processed route argument
*/
async transform(value: any, metadata: ArgumentMetadata): Promise<any> {
async transform(value: unknown, metadata: ArgumentMetadata): Promise<any> {
if (!value && !this.options.optional) {
throw this.exceptionFactory(VALIDATION_ERROR_MESSAGE);
} else if (isNil(value) && this.options.optional) {
Expand Down Expand Up @@ -147,7 +147,9 @@ export class ParseArrayPipe implements PipeTransform {
}
return targetArray;
} else {
value = await Promise.all(value.map(toClassInstance));
value = await Promise.all(
(value as Array<unknown>).map(toClassInstance),
);
}
}
return value;
Expand Down
13 changes: 5 additions & 8 deletions packages/common/pipes/parse-bool.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ export interface ParseBoolPipeOptions {
* @publicApi
*/
@Injectable()
export class ParseBoolPipe implements PipeTransform<
string | boolean,
Promise<boolean>
> {
export class ParseBoolPipe implements PipeTransform {
protected exceptionFactory: (error: string) => any;

constructor(@Optional() protected readonly options?: ParseBoolPipeOptions) {
Expand All @@ -64,9 +61,9 @@ export class ParseBoolPipe implements PipeTransform<
* @param metadata contains metadata about the currently processed route argument
*/
async transform(
value: string | boolean,
value: unknown,
metadata: ArgumentMetadata,
): Promise<boolean> {
): Promise<boolean | undefined | null> {
if (isNil(value) && this.options?.optional) {
return value;
}
Expand All @@ -86,7 +83,7 @@ export class ParseBoolPipe implements PipeTransform<
* @returns `true` if `value` is said 'true', ie., if it is equal to the boolean
* `true` or the string `"true"`
*/
protected isTrue(value: string | boolean): boolean {
protected isTrue(value: unknown): boolean {
return value === true || value === 'true';
}

Expand All @@ -95,7 +92,7 @@ export class ParseBoolPipe implements PipeTransform<
* @returns `true` if `value` is said 'false', ie., if it is equal to the boolean
* `false` or the string `"false"`
*/
protected isFalse(value: string | boolean): boolean {
protected isFalse(value: unknown): boolean {
return value === false || value === 'false';
}
}
15 changes: 7 additions & 8 deletions packages/common/pipes/parse-date.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ErrorHttpStatusCode,
HttpErrorByCode,
} from '../utils/http-error-by-code.util';
import { isNil } from '../utils/shared.utils';
import { isNil, isNumber, isString } from '../utils/shared.utils';

export interface ParseDatePipeOptions {
/**
Expand All @@ -31,9 +31,7 @@ export interface ParseDatePipeOptions {
}

@Injectable()
export class ParseDatePipe implements PipeTransform<
string | number | undefined | null
> {
export class ParseDatePipe implements PipeTransform {
protected exceptionFactory: (error: string) => any;

constructor(private readonly options: ParseDatePipeOptions = {}) {
Expand All @@ -52,9 +50,7 @@ export class ParseDatePipe implements PipeTransform<
* @param value currently processed route argument
* @param metadata contains metadata about the currently processed route argument
*/
transform(
value: string | number | undefined | null,
): Date | null | undefined {
transform(value: unknown): Date | null | undefined {
if (this.options.optional && isNil(value)) {
return this.options.default ? this.options.default() : value;
}
Expand All @@ -63,7 +59,10 @@ export class ParseDatePipe implements PipeTransform<
throw this.exceptionFactory('Validation failed (no Date provided)');
}

const transformedValue = new Date(value);
const transformedValue =
isString(value) || isNumber(value) || value instanceof Date
? new Date(value)
: new Date(NaN);

if (isNaN(transformedValue.getTime())) {
throw this.exceptionFactory('Validation failed (invalid date format)');
Expand Down
9 changes: 6 additions & 3 deletions packages/common/pipes/parse-enum.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ export class ParseEnumPipe<T = any> implements PipeTransform<T> {
* @param value currently processed route argument
* @param metadata contains metadata about the currently processed route argument
*/
async transform(value: T, metadata: ArgumentMetadata): Promise<T> {
async transform(
value: unknown,
metadata: ArgumentMetadata,
): Promise<T | undefined | null> {
if (isNil(value) && this.options?.optional) {
return value;
}
Expand All @@ -73,10 +76,10 @@ export class ParseEnumPipe<T = any> implements PipeTransform<T> {
'Validation failed (enum string is expected)',
);
}
return value;
return value as T;
}

protected isEnum(value: T): boolean {
protected isEnum(value: unknown): boolean {
const enumValues = Object.keys(this.enumType as object).map(
item => this.enumType[item],
);
Expand Down
13 changes: 8 additions & 5 deletions packages/common/pipes/parse-float.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface ParseFloatPipeOptions {
* @publicApi
*/
@Injectable()
export class ParseFloatPipe implements PipeTransform<string> {
export class ParseFloatPipe implements PipeTransform {
protected exceptionFactory: (error: string) => any;

constructor(@Optional() protected readonly options?: ParseFloatPipeOptions) {
Expand All @@ -57,7 +57,10 @@ export class ParseFloatPipe implements PipeTransform<string> {
* @param value currently processed route argument
* @param metadata contains metadata about the currently processed route argument
*/
async transform(value: string, metadata: ArgumentMetadata): Promise<number> {
async transform(
value: unknown,
metadata: ArgumentMetadata,
): Promise<number | undefined | null> {
if (isNil(value) && this.options?.optional) {
return value;
}
Expand All @@ -66,17 +69,17 @@ export class ParseFloatPipe implements PipeTransform<string> {
'Validation failed (numeric string is expected)',
);
}
return parseFloat(value);
return parseFloat(String(value));
}

/**
* @param value currently processed route argument
* @returns `true` if `value` is a valid float number
*/
protected isNumeric(value: string): boolean {
protected isNumeric(value: unknown): boolean {
return (
['string', 'number'].includes(typeof value) &&
!isNaN(parseFloat(value)) &&
!isNaN(parseFloat(String(value))) &&
isFinite(value as any)
);
}
Expand Down
13 changes: 8 additions & 5 deletions packages/common/pipes/parse-int.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface ParseIntPipeOptions {
* @publicApi
*/
@Injectable()
export class ParseIntPipe implements PipeTransform<string> {
export class ParseIntPipe implements PipeTransform {
protected exceptionFactory: (error: string) => any;

constructor(@Optional() protected readonly options?: ParseIntPipeOptions) {
Expand All @@ -61,7 +61,10 @@ export class ParseIntPipe implements PipeTransform<string> {
* @param value currently processed route argument
* @param metadata contains metadata about the currently processed route argument
*/
async transform(value: string, metadata: ArgumentMetadata): Promise<number> {
async transform(
value: unknown,
metadata: ArgumentMetadata,
): Promise<number | undefined | null> {
if (isNil(value) && this.options?.optional) {
return value;
}
Expand All @@ -70,17 +73,17 @@ export class ParseIntPipe implements PipeTransform<string> {
'Validation failed (numeric string is expected)',
);
}
return parseInt(value, 10);
return parseInt(String(value), 10);
}

/**
* @param value currently processed route argument
* @returns `true` if `value` is a valid integer number
*/
protected isNumeric(value: string): boolean {
protected isNumeric(value: unknown): boolean {
return (
['string', 'number'].includes(typeof value) &&
/^-?\d+$/.test(value) &&
/^-?\d+$/.test(String(value)) &&
isFinite(value as any)
);
}
Expand Down
9 changes: 6 additions & 3 deletions packages/common/pipes/parse-uuid.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface ParseUUIDPipeOptions {
* @publicApi
*/
@Injectable()
export class ParseUUIDPipe implements PipeTransform<string> {
export class ParseUUIDPipe implements PipeTransform {
protected static uuidRegExps = {
3: /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
4: /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
Expand All @@ -70,7 +70,10 @@ export class ParseUUIDPipe implements PipeTransform<string> {
(error => new HttpErrorByCode[errorHttpStatusCode](error));
}

async transform(value: string, metadata: ArgumentMetadata): Promise<string> {
async transform(
value: unknown,
metadata: ArgumentMetadata,
): Promise<string | undefined | null> {
if (isNil(value) && this.options?.optional) {
return value;
}
Expand All @@ -81,7 +84,7 @@ export class ParseUUIDPipe implements PipeTransform<string> {
} is expected)`,
);
}
return value;
return value as string;
}

protected isUUID(str: unknown, version = 'all') {
Expand Down
10 changes: 5 additions & 5 deletions packages/common/pipes/validation.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let classTransformer: TransformerPackage = {} as any;
* @publicApi
*/
@Injectable()
export class ValidationPipe implements PipeTransform<any> {
export class ValidationPipe implements PipeTransform {
protected isTransformEnabled: boolean;
protected isDetailedOutputDisabled?: boolean;
protected validatorOptions: ValidatorOptions;
Expand Down Expand Up @@ -104,7 +104,7 @@ export class ValidationPipe implements PipeTransform<any> {
);
}

public async transform(value: any, metadata: ArgumentMetadata) {
public async transform(value: unknown, metadata: ArgumentMetadata) {
if (this.expectedType) {
metadata = { ...metadata, metatype: this.expectedType };
}
Expand Down Expand Up @@ -191,7 +191,7 @@ export class ValidationPipe implements PipeTransform<any> {
return !types.some(t => metatype === t) && !isNil(metatype);
}

protected transformPrimitive(value: any, metadata: ArgumentMetadata) {
protected transformPrimitive(value: unknown, metadata: ArgumentMetadata) {
if (!metadata.data) {
// leave top-level query/param objects unmodified
return value;
Expand All @@ -217,7 +217,7 @@ export class ValidationPipe implements PipeTransform<any> {
// they were not defined
return undefined;
}
return +value;
return +(value as any);
}
if (metatype === String && !isUndefined(value)) {
return String(value);
Expand All @@ -226,7 +226,7 @@ export class ValidationPipe implements PipeTransform<any> {
}

protected toEmptyIfNil<T = any, R = T>(
value: T,
value: unknown,
metatype: Type<unknown> | object,
): R | object | string {
if (!isNil(value)) {
Expand Down
Loading