diff options
author | Chocobozzz <me@florianbigard.com> | 2021-08-05 13:54:35 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-08-05 13:54:35 +0200 |
commit | 27db78400c558e19bfac0da885fe0b7d0a3e6a0c (patch) | |
tree | e50e031f4b0989e438f3c1610c0a16086dd9455f | |
parent | 352819ef921e45381b3fbb17072926103b320e73 (diff) | |
download | PeerTube-27db78400c558e19bfac0da885fe0b7d0a3e6a0c.tar.gz PeerTube-27db78400c558e19bfac0da885fe0b7d0a3e6a0c.tar.zst PeerTube-27db78400c558e19bfac0da885fe0b7d0a3e6a0c.zip |
Fix backend channel name validator consistency
-rw-r--r-- | client/src/app/shared/form-validators/video-channel-validators.ts | 10 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-channels.ts | 11 | ||||
-rw-r--r-- | server/middlewares/validators/users.ts | 12 | ||||
-rw-r--r-- | server/middlewares/validators/videos/video-channels.ts | 14 | ||||
-rw-r--r-- | server/models/video/video-channel.ts | 4 |
5 files changed, 29 insertions, 22 deletions
diff --git a/client/src/app/shared/form-validators/video-channel-validators.ts b/client/src/app/shared/form-validators/video-channel-validators.ts index 0daab22ce..ba502ed01 100644 --- a/client/src/app/shared/form-validators/video-channel-validators.ts +++ b/client/src/app/shared/form-validators/video-channel-validators.ts | |||
@@ -1,13 +1,11 @@ | |||
1 | import { Validators } from '@angular/forms' | 1 | import { Validators } from '@angular/forms' |
2 | import { BuildFormValidator } from './form-validator.model' | 2 | import { BuildFormValidator } from './form-validator.model' |
3 | import { USER_USERNAME_VALIDATOR } from './user-validators' | ||
3 | 4 | ||
4 | export const VIDEO_CHANNEL_NAME_VALIDATOR: BuildFormValidator = { | 5 | export const VIDEO_CHANNEL_NAME_VALIDATOR: BuildFormValidator = { |
5 | VALIDATORS: [ | 6 | // Use the same constraints than user usernmae |
6 | Validators.required, | 7 | VALIDATORS: USER_USERNAME_VALIDATOR.VALIDATORS, |
7 | Validators.minLength(1), | 8 | |
8 | Validators.maxLength(50), | ||
9 | Validators.pattern(/^[a-z0-9][a-z0-9._]*$/) | ||
10 | ], | ||
11 | MESSAGES: { | 9 | MESSAGES: { |
12 | 'required': $localize`Name is required.`, | 10 | 'required': $localize`Name is required.`, |
13 | 'minlength': $localize`Name must be at least 1 character long.`, | 11 | 'minlength': $localize`Name must be at least 1 character long.`, |
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index ded5d5171..249083f39 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts | |||
@@ -1,14 +1,20 @@ | |||
1 | import validator from 'validator' | 1 | import validator from 'validator' |
2 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' | 2 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' |
3 | import { exists } from './misc' | 3 | import { exists } from './misc' |
4 | import { isUserUsernameValid } from './users' | ||
4 | 5 | ||
5 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS | 6 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS |
6 | 7 | ||
8 | function isVideoChannelUsernameValid (value: string) { | ||
9 | // Use the same constraints than user username | ||
10 | return isUserUsernameValid(value) | ||
11 | } | ||
12 | |||
7 | function isVideoChannelDescriptionValid (value: string) { | 13 | function isVideoChannelDescriptionValid (value: string) { |
8 | return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) | 14 | return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) |
9 | } | 15 | } |
10 | 16 | ||
11 | function isVideoChannelNameValid (value: string) { | 17 | function isVideoChannelDisplayNameValid (value: string) { |
12 | return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) | 18 | return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) |
13 | } | 19 | } |
14 | 20 | ||
@@ -19,7 +25,8 @@ function isVideoChannelSupportValid (value: string) { | |||
19 | // --------------------------------------------------------------------------- | 25 | // --------------------------------------------------------------------------- |
20 | 26 | ||
21 | export { | 27 | export { |
28 | isVideoChannelUsernameValid, | ||
22 | isVideoChannelDescriptionValid, | 29 | isVideoChannelDescriptionValid, |
23 | isVideoChannelNameValid, | 30 | isVideoChannelDisplayNameValid, |
24 | isVideoChannelSupportValid | 31 | isVideoChannelSupportValid |
25 | } | 32 | } |
diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 748b89f8f..bc8607523 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts | |||
@@ -6,7 +6,6 @@ import { MUserDefault } from '@server/types/models' | |||
6 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' | 6 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
7 | import { UserRole } from '../../../shared/models/users' | 7 | import { UserRole } from '../../../shared/models/users' |
8 | import { UserRegister } from '../../../shared/models/users/user-register.model' | 8 | import { UserRegister } from '../../../shared/models/users/user-register.model' |
9 | import { isActorPreferredUsernameValid } from '../../helpers/custom-validators/activitypub/actor' | ||
10 | import { toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' | 9 | import { toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' |
11 | import { isThemeNameValid } from '../../helpers/custom-validators/plugins' | 10 | import { isThemeNameValid } from '../../helpers/custom-validators/plugins' |
12 | import { | 11 | import { |
@@ -28,7 +27,7 @@ import { | |||
28 | isUserVideoQuotaValid, | 27 | isUserVideoQuotaValid, |
29 | isUserVideosHistoryEnabledValid | 28 | isUserVideosHistoryEnabledValid |
30 | } from '../../helpers/custom-validators/users' | 29 | } from '../../helpers/custom-validators/users' |
31 | import { isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels' | 30 | import { isVideoChannelDisplayNameValid, isVideoChannelUsernameValid } from '../../helpers/custom-validators/video-channels' |
32 | import { logger } from '../../helpers/logger' | 31 | import { logger } from '../../helpers/logger' |
33 | import { isThemeRegistered } from '../../lib/plugins/theme-utils' | 32 | import { isThemeRegistered } from '../../lib/plugins/theme-utils' |
34 | import { Redis } from '../../lib/redis' | 33 | import { Redis } from '../../lib/redis' |
@@ -56,9 +55,12 @@ const usersAddValidator = [ | |||
56 | body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'), | 55 | body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'), |
57 | body('password').custom(isUserPasswordValidOrEmpty).withMessage('Should have a valid password'), | 56 | body('password').custom(isUserPasswordValidOrEmpty).withMessage('Should have a valid password'), |
58 | body('email').isEmail().withMessage('Should have a valid email'), | 57 | body('email').isEmail().withMessage('Should have a valid email'), |
59 | body('channelName').optional().custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'), | 58 | |
59 | body('channelName').optional().custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'), | ||
60 | |||
60 | body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'), | 61 | body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'), |
61 | body('videoQuotaDaily').custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'), | 62 | body('videoQuotaDaily').custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'), |
63 | |||
62 | body('role') | 64 | body('role') |
63 | .customSanitizer(toIntOrNull) | 65 | .customSanitizer(toIntOrNull) |
64 | .custom(isUserRoleValid).withMessage('Should have a valid role'), | 66 | .custom(isUserRoleValid).withMessage('Should have a valid role'), |
@@ -106,10 +108,10 @@ const usersRegisterValidator = [ | |||
106 | 108 | ||
107 | body('channel.name') | 109 | body('channel.name') |
108 | .optional() | 110 | .optional() |
109 | .custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'), | 111 | .custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'), |
110 | body('channel.displayName') | 112 | body('channel.displayName') |
111 | .optional() | 113 | .optional() |
112 | .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), | 114 | .custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'), |
113 | 115 | ||
114 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | 116 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
115 | logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') }) | 117 | logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') }) |
diff --git a/server/middlewares/validators/videos/video-channels.ts b/server/middlewares/validators/videos/video-channels.ts index ea10fe425..3b5693d23 100644 --- a/server/middlewares/validators/videos/video-channels.ts +++ b/server/middlewares/validators/videos/video-channels.ts | |||
@@ -4,12 +4,12 @@ import { VIDEO_CHANNELS } from '@server/initializers/constants' | |||
4 | import { MChannelAccountDefault, MUser } from '@server/types/models' | 4 | import { MChannelAccountDefault, MUser } from '@server/types/models' |
5 | import { UserRight } from '../../../../shared' | 5 | import { UserRight } from '../../../../shared' |
6 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' | 6 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' |
7 | import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor' | ||
8 | import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc' | 7 | import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc' |
9 | import { | 8 | import { |
10 | isVideoChannelDescriptionValid, | 9 | isVideoChannelDescriptionValid, |
11 | isVideoChannelNameValid, | 10 | isVideoChannelDisplayNameValid, |
12 | isVideoChannelSupportValid | 11 | isVideoChannelSupportValid, |
12 | isVideoChannelUsernameValid | ||
13 | } from '../../../helpers/custom-validators/video-channels' | 13 | } from '../../../helpers/custom-validators/video-channels' |
14 | import { logger } from '../../../helpers/logger' | 14 | import { logger } from '../../../helpers/logger' |
15 | import { ActorModel } from '../../../models/actor/actor' | 15 | import { ActorModel } from '../../../models/actor/actor' |
@@ -17,8 +17,8 @@ import { VideoChannelModel } from '../../../models/video/video-channel' | |||
17 | import { areValidationErrors, doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../shared' | 17 | import { areValidationErrors, doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../shared' |
18 | 18 | ||
19 | const videoChannelsAddValidator = [ | 19 | const videoChannelsAddValidator = [ |
20 | body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'), | 20 | body('name').custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'), |
21 | body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), | 21 | body('displayName').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'), |
22 | body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), | 22 | body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), |
23 | body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'), | 23 | body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'), |
24 | 24 | ||
@@ -50,7 +50,7 @@ const videoChannelsUpdateValidator = [ | |||
50 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), | 50 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), |
51 | body('displayName') | 51 | body('displayName') |
52 | .optional() | 52 | .optional() |
53 | .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), | 53 | .custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'), |
54 | body('description') | 54 | body('description') |
55 | .optional() | 55 | .optional() |
56 | .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), | 56 | .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), |
@@ -117,7 +117,7 @@ const videoChannelsNameWithHostValidator = [ | |||
117 | ] | 117 | ] |
118 | 118 | ||
119 | const localVideoChannelValidator = [ | 119 | const localVideoChannelValidator = [ |
120 | param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'), | 120 | param('name').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid video channel name'), |
121 | 121 | ||
122 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | 122 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
123 | logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params }) | 123 | logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params }) |
diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index 9f04a57c6..278149d60 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts | |||
@@ -23,7 +23,7 @@ import { ActivityPubActor } from '../../../shared/models/activitypub' | |||
23 | import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos' | 23 | import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos' |
24 | import { | 24 | import { |
25 | isVideoChannelDescriptionValid, | 25 | isVideoChannelDescriptionValid, |
26 | isVideoChannelNameValid, | 26 | isVideoChannelDisplayNameValid, |
27 | isVideoChannelSupportValid | 27 | isVideoChannelSupportValid |
28 | } from '../../helpers/custom-validators/video-channels' | 28 | } from '../../helpers/custom-validators/video-channels' |
29 | import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' | 29 | import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' |
@@ -308,7 +308,7 @@ export type SummaryOptions = { | |||
308 | export class VideoChannelModel extends Model<Partial<AttributesOnly<VideoChannelModel>>> { | 308 | export class VideoChannelModel extends Model<Partial<AttributesOnly<VideoChannelModel>>> { |
309 | 309 | ||
310 | @AllowNull(false) | 310 | @AllowNull(false) |
311 | @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name')) | 311 | @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'name')) |
312 | @Column | 312 | @Column |
313 | name: string | 313 | name: string |
314 | 314 | ||