Skip to content

Commit 66a9579

Browse files
limit links
1 parent 40016a8 commit 66a9579

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

redisinsight/api/src/common/decorators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './client-metadata';
77
export * from './object-as-map.decorator';
88
export * from './is-multi-number.decorator';
99
export * from './is-bigger-than.decorator';
10+
export * from './is-github-link.decorator';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {
2+
registerDecorator,
3+
ValidationOptions,
4+
} from 'class-validator';
5+
import { GitHubLink } from 'src/common/validators';
6+
7+
export function IsGitHubLink(validationOptions?: ValidationOptions) {
8+
return (object: any, propertyName: string) => {
9+
registerDecorator({
10+
name: 'IsGitHubLink',
11+
target: object.constructor,
12+
propertyName,
13+
constraints: [],
14+
options: validationOptions,
15+
validator: GitHubLink,
16+
});
17+
};
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {
2+
ValidatorConstraint,
3+
ValidatorConstraintInterface,
4+
} from 'class-validator';
5+
import config from 'src/utils/config';
6+
7+
const SERVER_CONFIG = config.get('server');
8+
9+
@ValidatorConstraint({ name: 'GitHubLink', async: false })
10+
export class GitHubLink implements ValidatorConstraintInterface {
11+
validate(value: any) {
12+
// TODO: temporary solution for integration tests
13+
if (SERVER_CONFIG.env === 'test') return true;
14+
15+
// Regular expression to match any GitHub URL
16+
const githubUrlRegex = /^https:\/\/github\.com(?:\/[^\s/]+(?:\/[^\s/]+)*)?\/?$/;
17+
return typeof value === 'string' && githubUrlRegex.test(value);
18+
}
19+
20+
defaultMessage() {
21+
return 'Enter a full GitHub link';
22+
}
23+
}

redisinsight/api/src/common/validators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './redis-string.validator';
22
export * from './zset-score.validator';
33
export * from './multi-number.validator';
44
export * from './bigger-than.validator';
5+
export * from './github-link.validator';

redisinsight/api/src/modules/custom-tutorial/dto/upload.custom-tutorial.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
33
import {
44
HasMimeType, IsFile, MaxFileSize, MemoryStoredFile,
55
} from 'nestjs-form-data';
6+
import { IsGitHubLink } from 'src/common/decorators';
67

78
export class UploadCustomTutorialDto {
89
@ApiPropertyOptional({
@@ -23,5 +24,6 @@ export class UploadCustomTutorialDto {
2324
@IsOptional()
2425
@IsString()
2526
@IsNotEmpty()
27+
@IsGitHubLink()
2628
link?: string;
2729
}

0 commit comments

Comments
 (0)