refactor(server): guards, decorators, and utils (#3060)

This commit is contained in:
Jason Rasmussen
2023-07-01 14:27:34 -04:00
committed by GitHub
parent f55b3add80
commit d69fa3ceae
54 changed files with 243 additions and 255 deletions

View File

@@ -1,4 +1,39 @@
import { applyDecorators } from '@nestjs/common';
import { ApiProperty } from '@nestjs/swagger';
import { IsArray, IsNotEmpty, IsOptional, IsString, IsUUID } from 'class-validator';
import { basename, extname } from 'node:path';
import sanitize from 'sanitize-filename';
export type Options = {
optional?: boolean;
each?: boolean;
};
export function ValidateUUID({ optional, each }: Options = { optional: false, each: false }) {
return applyDecorators(
IsUUID('4', { each }),
ApiProperty({ format: 'uuid' }),
optional ? IsOptional() : IsNotEmpty(),
each ? IsArray() : IsString(),
);
}
interface IValue {
value?: string;
}
export const toBoolean = ({ value }: IValue) => {
if (value == 'true') {
return true;
} else if (value == 'false') {
return false;
}
return value;
};
export const toEmail = ({ value }: IValue) => value?.toLowerCase();
export const toSanitized = ({ value }: IValue) => sanitize((value || '').replace(/\./g, ''));
export function getFileNameWithoutExtension(path: string): string {
return basename(path, extname(path));