Fix backend channel name validator consistency

pull/4317/head
Chocobozzz 2021-08-05 13:54:35 +02:00
parent 352819ef92
commit 27db78400c
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
5 changed files with 29 additions and 22 deletions

View File

@ -1,13 +1,11 @@
import { Validators } from '@angular/forms'
import { BuildFormValidator } from './form-validator.model'
import { USER_USERNAME_VALIDATOR } from './user-validators'
export const VIDEO_CHANNEL_NAME_VALIDATOR: BuildFormValidator = {
VALIDATORS: [
Validators.required,
Validators.minLength(1),
Validators.maxLength(50),
Validators.pattern(/^[a-z0-9][a-z0-9._]*$/)
],
// Use the same constraints than user usernmae
VALIDATORS: USER_USERNAME_VALIDATOR.VALIDATORS,
MESSAGES: {
'required': $localize`Name is required.`,
'minlength': $localize`Name must be at least 1 character long.`,

View File

@ -1,14 +1,20 @@
import validator from 'validator'
import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
import { exists } from './misc'
import { isUserUsernameValid } from './users'
const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
function isVideoChannelUsernameValid (value: string) {
// Use the same constraints than user username
return isUserUsernameValid(value)
}
function isVideoChannelDescriptionValid (value: string) {
return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION)
}
function isVideoChannelNameValid (value: string) {
function isVideoChannelDisplayNameValid (value: string) {
return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
}
@ -19,7 +25,8 @@ function isVideoChannelSupportValid (value: string) {
// ---------------------------------------------------------------------------
export {
isVideoChannelUsernameValid,
isVideoChannelDescriptionValid,
isVideoChannelNameValid,
isVideoChannelDisplayNameValid,
isVideoChannelSupportValid
}

View File

@ -6,7 +6,6 @@ import { MUserDefault } from '@server/types/models'
import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
import { UserRole } from '../../../shared/models/users'
import { UserRegister } from '../../../shared/models/users/user-register.model'
import { isActorPreferredUsernameValid } from '../../helpers/custom-validators/activitypub/actor'
import { toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
import {
@ -28,7 +27,7 @@ import {
isUserVideoQuotaValid,
isUserVideosHistoryEnabledValid
} from '../../helpers/custom-validators/users'
import { isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels'
import { isVideoChannelDisplayNameValid, isVideoChannelUsernameValid } from '../../helpers/custom-validators/video-channels'
import { logger } from '../../helpers/logger'
import { isThemeRegistered } from '../../lib/plugins/theme-utils'
import { Redis } from '../../lib/redis'
@ -56,9 +55,12 @@ const usersAddValidator = [
body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
body('password').custom(isUserPasswordValidOrEmpty).withMessage('Should have a valid password'),
body('email').isEmail().withMessage('Should have a valid email'),
body('channelName').optional().custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
body('channelName').optional().custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'),
body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
body('videoQuotaDaily').custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'),
body('role')
.customSanitizer(toIntOrNull)
.custom(isUserRoleValid).withMessage('Should have a valid role'),
@ -106,10 +108,10 @@ const usersRegisterValidator = [
body('channel.name')
.optional()
.custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
.custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'),
body('channel.displayName')
.optional()
.custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
.custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') })

View File

@ -4,12 +4,12 @@ import { VIDEO_CHANNELS } from '@server/initializers/constants'
import { MChannelAccountDefault, MUser } from '@server/types/models'
import { UserRight } from '../../../../shared'
import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
import {
isVideoChannelDescriptionValid,
isVideoChannelNameValid,
isVideoChannelSupportValid
isVideoChannelDisplayNameValid,
isVideoChannelSupportValid,
isVideoChannelUsernameValid
} from '../../../helpers/custom-validators/video-channels'
import { logger } from '../../../helpers/logger'
import { ActorModel } from '../../../models/actor/actor'
@ -17,8 +17,8 @@ import { VideoChannelModel } from '../../../models/video/video-channel'
import { areValidationErrors, doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../shared'
const videoChannelsAddValidator = [
body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
body('name').custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'),
body('displayName').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
@ -50,7 +50,7 @@ const videoChannelsUpdateValidator = [
param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
body('displayName')
.optional()
.custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
.custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
body('description')
.optional()
.custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
@ -117,7 +117,7 @@ const videoChannelsNameWithHostValidator = [
]
const localVideoChannelValidator = [
param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
param('name').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid video channel name'),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })

View File

@ -23,7 +23,7 @@ import { ActivityPubActor } from '../../../shared/models/activitypub'
import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos'
import {
isVideoChannelDescriptionValid,
isVideoChannelNameValid,
isVideoChannelDisplayNameValid,
isVideoChannelSupportValid
} from '../../helpers/custom-validators/video-channels'
import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
@ -308,7 +308,7 @@ export type SummaryOptions = {
export class VideoChannelModel extends Model<Partial<AttributesOnly<VideoChannelModel>>> {
@AllowNull(false)
@Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
@Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'name'))
@Column
name: string