mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 16:14:55 +00:00
community controller
This commit is contained in:
@ -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;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
54
libs/common/src/validators/is-sort-param.validator.ts
Normal file
54
libs/common/src/validators/is-sort-param.validator.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user