community controller

This commit is contained in:
hannathkadher
2024-10-15 11:01:56 +04:00
parent a157444897
commit 2292c01220
23 changed files with 822 additions and 259 deletions

View File

@ -0,0 +1,21 @@
import { ValidateBy, ValidationOptions } from 'class-validator';
export function IsPageRequestParam(
validationOptions?: ValidationOptions,
): PropertyDecorator {
return ValidateBy(
{
name: 'IsPageRequestParam',
validator: {
validate(value) {
return IsPageParam(value); // you can return a Promise<boolean> here as well, if you want to make async validation
},
},
},
validationOptions,
);
}
function IsPageParam(value: any): boolean {
return !isNaN(Number(value)) && value > 0;
}

View File

@ -0,0 +1,21 @@
import { ValidateBy, ValidationOptions } from 'class-validator';
export function IsSizeRequestParam(
validationOptions?: ValidationOptions,
): PropertyDecorator {
return ValidateBy(
{
name: 'IsSizeRequestParam',
validator: {
validate(value) {
return IsSizeParam(value); // you can return a Promise<boolean> here as well, if you want to make async validation
},
},
},
validationOptions,
);
}
function IsSizeParam(value: any): boolean {
return !isNaN(Number(value)) && value > -1;
}

View File

@ -0,0 +1,54 @@
import { ValidateBy, ValidationOptions } from 'class-validator';
export function IsSortParam(
validationOptions?: ValidationOptions,
allowedFieldName?: string[],
): PropertyDecorator {
return ValidateBy(
{
name: 'IsSortParam',
validator: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
validate(value) {
return IsValidMultipleSortParam(value, allowedFieldName); // you can return a Promise<boolean> here as well, if you want to make async validation
},
},
},
validationOptions,
);
}
function IsValidMultipleSortParam(
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
value: any,
allowedFieldName?: string[],
): boolean {
if (typeof value !== 'string') {
return false;
}
const conditions: string[] = value.split(',');
const isValid: boolean = conditions.every((condition) => {
const combination: string[] = condition.split(':');
if (combination.length !== 2) {
return false;
}
const field = combination[0].trim();
const direction = combination[1].trim();
if (!field) {
return false;
}
if (allowedFieldName?.length && !allowedFieldName.includes(field)) {
return false;
}
if (!['asc', 'desc'].includes(direction.toLowerCase())) {
return false;
}
return true;
});
return isValid;
}