feat: better validation for post
This commit is contained in:
parent
806dba19c3
commit
9f737560fb
|
|
@ -0,0 +1,28 @@
|
|||
import {
|
||||
ValidationArguments,
|
||||
ValidatorConstraintInterface,
|
||||
ValidatorConstraint,
|
||||
} from 'class-validator';
|
||||
import striptags from 'striptags';
|
||||
|
||||
@ValidatorConstraint({ name: 'validateContent', async: false })
|
||||
export class ValidContent implements ValidatorConstraintInterface {
|
||||
validate(contentRaw: string, args: ValidationArguments) {
|
||||
console.log(args.object);
|
||||
const content = striptags(contentRaw || '');
|
||||
if (
|
||||
// @ts-ignore
|
||||
(!args?.object?.image || !Array.isArray(args?.object?.image) || !args?.object?.image.length) &&
|
||||
(!content || typeof content !== 'string' || content?.trim() === '')
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
defaultMessage(args: ValidationArguments) {
|
||||
// here you can provide default error message if validation failed
|
||||
return ' If images do not exist, content must be a non-empty string.';
|
||||
}
|
||||
}
|
||||
|
|
@ -1,20 +1,10 @@
|
|||
import {
|
||||
ArrayMinSize,
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
IsDateString,
|
||||
IsDefined,
|
||||
IsIn,
|
||||
IsNumber,
|
||||
IsOptional,
|
||||
IsString,
|
||||
MinLength,
|
||||
ValidateIf,
|
||||
ValidateNested,
|
||||
ArrayMinSize, IsArray, IsBoolean, IsDateString, IsDefined, IsIn, IsNumber, IsOptional, IsString, MinLength, Validate, ValidateIf, ValidateNested
|
||||
} from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
import { MediaDto } from '@gitroom/nestjs-libraries/dtos/media/media.dto';
|
||||
import { allProviders, type AllProvidersSettings, EmptySettings } from '@gitroom/nestjs-libraries/dtos/posts/providers-settings/all.providers.settings';
|
||||
import { ValidContent } from '@gitroom/helpers/utils/valid.images';
|
||||
|
||||
export class Integration {
|
||||
@IsDefined()
|
||||
|
|
@ -25,6 +15,7 @@ export class Integration {
|
|||
export class PostContent {
|
||||
@IsDefined()
|
||||
@IsString()
|
||||
@Validate(ValidContent)
|
||||
content: string;
|
||||
|
||||
@IsOptional()
|
||||
|
|
@ -32,7 +23,6 @@ export class PostContent {
|
|||
id: string;
|
||||
|
||||
@IsArray()
|
||||
@IsOptional()
|
||||
@Type(() => MediaDto)
|
||||
@ValidateNested({ each: true })
|
||||
image: MediaDto[];
|
||||
|
|
|
|||
Loading…
Reference in New Issue