From 7ed1edbbe4ffbef28093e4f5630751cb652814e4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 17 Aug 2020 11:47:04 +0200 Subject: We don't need services anymore for validators --- .../app/shared/form-validators/abuse-validators.ts | 29 +++++ .../form-validators/batch-domains-validators.ts | 60 +++++++++ .../form-validators/custom-config-validators.ts | 80 ++++++++++++ .../shared/form-validators/form-validator.model.ts | 14 ++ client/src/app/shared/form-validators/host.ts | 8 ++ client/src/app/shared/form-validators/index.ts | 17 +++ .../shared/form-validators/instance-validators.ts | 49 +++++++ .../app/shared/form-validators/login-validators.ts | 20 +++ .../form-validators/reset-password-validators.ts | 11 ++ .../app/shared/form-validators/user-validators.ts | 144 +++++++++++++++++++++ .../form-validators/video-block-validators.ts | 10 ++ .../form-validators/video-captions-validators.ts | 16 +++ .../form-validators/video-channel-validators.ts | 52 ++++++++ .../form-validators/video-comment-validators.ts | 11 ++ .../video-ownership-change-validators.ts | 25 ++++ .../form-validators/video-playlist-validators.ts | 54 ++++++++ .../app/shared/form-validators/video-validators.ts | 101 +++++++++++++++ 17 files changed, 701 insertions(+) create mode 100644 client/src/app/shared/form-validators/abuse-validators.ts create mode 100644 client/src/app/shared/form-validators/batch-domains-validators.ts create mode 100644 client/src/app/shared/form-validators/custom-config-validators.ts create mode 100644 client/src/app/shared/form-validators/form-validator.model.ts create mode 100644 client/src/app/shared/form-validators/host.ts create mode 100644 client/src/app/shared/form-validators/index.ts create mode 100644 client/src/app/shared/form-validators/instance-validators.ts create mode 100644 client/src/app/shared/form-validators/login-validators.ts create mode 100644 client/src/app/shared/form-validators/reset-password-validators.ts create mode 100644 client/src/app/shared/form-validators/user-validators.ts create mode 100644 client/src/app/shared/form-validators/video-block-validators.ts create mode 100644 client/src/app/shared/form-validators/video-captions-validators.ts create mode 100644 client/src/app/shared/form-validators/video-channel-validators.ts create mode 100644 client/src/app/shared/form-validators/video-comment-validators.ts create mode 100644 client/src/app/shared/form-validators/video-ownership-change-validators.ts create mode 100644 client/src/app/shared/form-validators/video-playlist-validators.ts create mode 100644 client/src/app/shared/form-validators/video-validators.ts (limited to 'client/src/app/shared/form-validators') diff --git a/client/src/app/shared/form-validators/abuse-validators.ts b/client/src/app/shared/form-validators/abuse-validators.ts new file mode 100644 index 000000000..75bfacf01 --- /dev/null +++ b/client/src/app/shared/form-validators/abuse-validators.ts @@ -0,0 +1,29 @@ +import { Validators } from '@angular/forms' +import { BuildFormValidator } from './form-validator.model' + +export const ABUSE_REASON_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.required, Validators.minLength(2), Validators.maxLength(3000)], + MESSAGES: { + 'required': $localize`Report reason is required.`, + 'minlength': $localize`Report reason must be at least 2 characters long.`, + 'maxlength': $localize`Report reason cannot be more than 3000 characters long.` + } +} + +export const ABUSE_MODERATION_COMMENT_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.required, Validators.minLength(2), Validators.maxLength(3000)], + MESSAGES: { + 'required': $localize`Moderation comment is required.`, + 'minlength': $localize`Moderation comment must be at least 2 characters long.`, + 'maxlength': $localize`Moderation comment cannot be more than 3000 characters long.` + } +} + +export const ABUSE_MESSAGE_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.required, Validators.minLength(2), Validators.maxLength(3000)], + MESSAGES: { + 'required': $localize`Abuse message is required.`, + 'minlength': $localize`Abuse message must be at least 2 characters long.`, + 'maxlength': $localize`Abuse message cannot be more than 3000 characters long.` + } +} diff --git a/client/src/app/shared/form-validators/batch-domains-validators.ts b/client/src/app/shared/form-validators/batch-domains-validators.ts new file mode 100644 index 000000000..423d1337f --- /dev/null +++ b/client/src/app/shared/form-validators/batch-domains-validators.ts @@ -0,0 +1,60 @@ +import { AbstractControl, FormControl, ValidatorFn, Validators } from '@angular/forms' +import { BuildFormValidator } from './form-validator.model' +import { validateHost } from './host' + +export function getNotEmptyHosts (hosts: string) { + return hosts + .split('\n') + .filter((host: string) => host && host.length !== 0) // Eject empty hosts +} + +const validDomains: ValidatorFn = (control: FormControl) => { + if (!control.value) return null + + const newHostsErrors = [] + const hosts = getNotEmptyHosts(control.value) + + for (const host of hosts) { + if (validateHost(host) === false) { + newHostsErrors.push($localize`${host} is not valid`) + } + } + + /* Is not valid. */ + if (newHostsErrors.length !== 0) { + return { + 'validDomains': { + reason: 'invalid', + value: newHostsErrors.join('. ') + '.' + } + } + } + + /* Is valid. */ + return null +} + +const isHostsUnique: ValidatorFn = (control: AbstractControl) => { + if (!control.value) return null + + const hosts = getNotEmptyHosts(control.value) + + if (hosts.every((host: string) => hosts.indexOf(host) === hosts.lastIndexOf(host))) { + return null + } else { + return { + 'uniqueDomains': { + reason: 'invalid' + } + } + } +} + +export const DOMAINS_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.required, validDomains, isHostsUnique], + MESSAGES: { + 'required': $localize`Domain is required.`, + 'validDomains': $localize`Domains entered are invalid.`, + 'uniqueDomains': $localize`Domains entered contain duplicates.` + } +} diff --git a/client/src/app/shared/form-validators/custom-config-validators.ts b/client/src/app/shared/form-validators/custom-config-validators.ts new file mode 100644 index 000000000..41b3cbba9 --- /dev/null +++ b/client/src/app/shared/form-validators/custom-config-validators.ts @@ -0,0 +1,80 @@ +import { Validators } from '@angular/forms' +import { BuildFormValidator } from './form-validator.model' + +export const INSTANCE_NAME_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.required], + MESSAGES: { + 'required': $localize`Instance name is required.` + } +} + +export const INSTANCE_SHORT_DESCRIPTION_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.max(250)], + MESSAGES: { + 'max': $localize`Short description should not be longer than 250 characters.` + } +} + +export const SERVICES_TWITTER_USERNAME_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.required], + MESSAGES: { + 'required': $localize`Twitter username is required.` + } +} + +export const CACHE_PREVIEWS_SIZE_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.required, Validators.min(1), Validators.pattern('[0-9]+')], + MESSAGES: { + 'required': $localize`Previews cache size is required.`, + 'min': $localize`Previews cache size must be greater than 1.`, + 'pattern': $localize`Previews cache size must be a number.` + } +} + +export const CACHE_CAPTIONS_SIZE_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.required, Validators.min(1), Validators.pattern('[0-9]+')], + MESSAGES: { + 'required': $localize`Captions cache size is required.`, + 'min': $localize`Captions cache size must be greater than 1.`, + 'pattern': $localize`Captions cache size must be a number.` + } +} + +export const SIGNUP_LIMIT_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.required, Validators.min(-1), Validators.pattern('-?[0-9]+')], + MESSAGES: { + 'required': $localize`Signup limit is required.`, + 'min': $localize`Signup limit must be greater than 1.`, + 'pattern': $localize`Signup limit must be a number.` + } +} + +export const ADMIN_EMAIL_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.required, Validators.email], + MESSAGES: { + 'required': $localize`Admin email is required.`, + 'email': $localize`Admin email must be valid.` + } +} + +export const TRANSCODING_THREADS_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.required, Validators.min(0)], + MESSAGES: { + 'required': $localize`Transcoding threads is required.`, + 'min': $localize`Transcoding threads must be greater or equal to 0.` + } +} + +export const INDEX_URL_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.pattern(/^https:\/\//)], + MESSAGES: { + 'pattern': $localize`Index URL should be a URL` + } +} + +export const SEARCH_INDEX_URL_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.pattern(/^https?:\/\//)], + MESSAGES: { + 'pattern': $localize`Search index URL should be a URL` + } +} diff --git a/client/src/app/shared/form-validators/form-validator.model.ts b/client/src/app/shared/form-validators/form-validator.model.ts new file mode 100644 index 000000000..248a3b1d3 --- /dev/null +++ b/client/src/app/shared/form-validators/form-validator.model.ts @@ -0,0 +1,14 @@ +import { ValidatorFn } from '@angular/forms' + +export type BuildFormValidator = { + VALIDATORS: ValidatorFn[], + MESSAGES: { [ name: string ]: string } +} + +export type BuildFormArgument = { + [ id: string ]: BuildFormValidator | BuildFormArgument +} + +export type BuildFormDefaultValues = { + [ name: string ]: string | string[] | BuildFormDefaultValues +} diff --git a/client/src/app/shared/form-validators/host.ts b/client/src/app/shared/form-validators/host.ts new file mode 100644 index 000000000..c18a35f9b --- /dev/null +++ b/client/src/app/shared/form-validators/host.ts @@ -0,0 +1,8 @@ +export function validateHost (value: string) { + // Thanks to http://stackoverflow.com/a/106223 + const HOST_REGEXP = new RegExp( + '^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$' + ) + + return HOST_REGEXP.test(value) +} diff --git a/client/src/app/shared/form-validators/index.ts b/client/src/app/shared/form-validators/index.ts new file mode 100644 index 000000000..f621f03a4 --- /dev/null +++ b/client/src/app/shared/form-validators/index.ts @@ -0,0 +1,17 @@ +export * from './form-validator.model' +export * from './host' + +// Don't re export const variables because webpack 4 cannot do tree shaking with them +// export * from './abuse-validators' +// export * from './batch-domains-validators' +// export * from './custom-config-validators' +// export * from './instance-validators' +// export * from './login-validators' +// export * from './reset-password-validators' +// export * from './user-validators' +// export * from './video-block-validators' +// export * from './video-captions-validators' +// export * from './video-channel-validators' +// export * from './video-comment-validators' +// export * from './video-playlist-validators' +// export * from './video-validators' diff --git a/client/src/app/shared/form-validators/instance-validators.ts b/client/src/app/shared/form-validators/instance-validators.ts new file mode 100644 index 000000000..a72e28ba0 --- /dev/null +++ b/client/src/app/shared/form-validators/instance-validators.ts @@ -0,0 +1,49 @@ +import { Validators } from '@angular/forms' +import { BuildFormValidator } from './form-validator.model' + +export const FROM_EMAIL_VALIDATOR: BuildFormValidator = { + VALIDATORS: [Validators.required, Validators.email], + MESSAGES: { + 'required': $localize`Email is required.`, + 'email': $localize`Email must be valid.` + } +} + +export const FROM_NAME_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.required, + Validators.minLength(1), + Validators.maxLength(120) + ], + MESSAGES: { + 'required': $localize`Your name is required.`, + 'minlength': $localize`Your name must be at least 1 character long.`, + 'maxlength': $localize`Your name cannot be more than 120 characters long.` + } +} + +export const SUBJECT_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.required, + Validators.minLength(1), + Validators.maxLength(120) + ], + MESSAGES: { + 'required': $localize`A subject is required.`, + 'minlength': $localize`The subject must be at least 1 character long.`, + 'maxlength': $localize`The subject cannot be more than 120 characters long.` + } +} + +export const BODY_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.required, + Validators.minLength(3), + Validators.maxLength(5000) + ], + MESSAGES: { + 'required': $localize`A message is required.`, + 'minlength': $localize`The message must be at least 3 characters long.`, + 'maxlength': $localize`The message cannot be more than 5000 characters long.` + } +} diff --git a/client/src/app/shared/form-validators/login-validators.ts b/client/src/app/shared/form-validators/login-validators.ts new file mode 100644 index 000000000..1ceae1be3 --- /dev/null +++ b/client/src/app/shared/form-validators/login-validators.ts @@ -0,0 +1,20 @@ +import { Validators } from '@angular/forms' +import { BuildFormValidator } from './form-validator.model' + +export const LOGIN_USERNAME_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.required + ], + MESSAGES: { + 'required': $localize`Username is required.` + } +} + +export const LOGIN_PASSWORD_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.required + ], + MESSAGES: { + 'required': $localize`Password is required.` + } +} diff --git a/client/src/app/shared/form-validators/reset-password-validators.ts b/client/src/app/shared/form-validators/reset-password-validators.ts new file mode 100644 index 000000000..b87f2eab9 --- /dev/null +++ b/client/src/app/shared/form-validators/reset-password-validators.ts @@ -0,0 +1,11 @@ +import { Validators } from '@angular/forms' +import { BuildFormValidator } from './form-validator.model' + +export const RESET_PASSWORD_CONFIRM_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.required + ], + MESSAGES: { + 'required': $localize`Confirmation of the password is required.` + } +} diff --git a/client/src/app/shared/form-validators/user-validators.ts b/client/src/app/shared/form-validators/user-validators.ts new file mode 100644 index 000000000..18199505c --- /dev/null +++ b/client/src/app/shared/form-validators/user-validators.ts @@ -0,0 +1,144 @@ +import { Validators } from '@angular/forms' +import { BuildFormValidator } from './form-validator.model' + +export const USER_USERNAME_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.required, + Validators.minLength(1), + Validators.maxLength(50), + Validators.pattern(/^[a-z0-9][a-z0-9._]*$/) + ], + MESSAGES: { + 'required': $localize`Username is required.`, + 'minlength': $localize`Username must be at least 1 character long.`, + 'maxlength': $localize`Username cannot be more than 50 characters long.`, + 'pattern': $localize`Username should be lowercase alphanumeric; dots and underscores are allowed.` + } +} + +export const USER_CHANNEL_NAME_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.required, + Validators.minLength(1), + Validators.maxLength(50), + Validators.pattern(/^[a-z0-9][a-z0-9._]*$/) + ], + MESSAGES: { + 'required': $localize`Channel name is required.`, + 'minlength': $localize`Channel name must be at least 1 character long.`, + 'maxlength': $localize`Channel name cannot be more than 50 characters long.`, + 'pattern': $localize`Channel name should be lowercase alphanumeric; dots and underscores are allowed.` + } +} + +export const USER_EMAIL_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.required, Validators.email ], + MESSAGES: { + 'required': $localize`Email is required.`, + 'email': $localize`Email must be valid.` + } +} + +export const USER_PASSWORD_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.required, + Validators.minLength(6), + Validators.maxLength(255) + ], + MESSAGES: { + 'required': $localize`Password is required.`, + 'minlength': $localize`Password must be at least 6 characters long.`, + 'maxlength': $localize`Password cannot be more than 255 characters long.` + } +} + +export const USER_PASSWORD_OPTIONAL_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.minLength(6), + Validators.maxLength(255) + ], + MESSAGES: { + 'minlength': $localize`Password must be at least 6 characters long.`, + 'maxlength': $localize`Password cannot be more than 255 characters long.` + } +} + +export const USER_CONFIRM_PASSWORD_VALIDATOR: BuildFormValidator = { + VALIDATORS: [], + MESSAGES: { + 'matchPassword': $localize`The new password and the confirmed password do not correspond.` + } +} + +export const USER_VIDEO_QUOTA_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.required, Validators.min(-1) ], + MESSAGES: { + 'required': $localize`Video quota is required.`, + 'min': $localize`Quota must be greater than -1.` + } +} +export const USER_VIDEO_QUOTA_DAILY_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.required, Validators.min(-1) ], + MESSAGES: { + 'required': $localize`Daily upload limit is required.`, + 'min': $localize`Daily upload limit must be greater than -1.` + } +} + +export const USER_ROLE_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.required ], + MESSAGES: { + 'required': $localize`User role is required.` + } +} + +export const USER_DISPLAY_NAME_REQUIRED_VALIDATOR = buildDisplayNameValidator(true) + +export const USER_DESCRIPTION_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.minLength(3), + Validators.maxLength(1000) + ], + MESSAGES: { + 'minlength': $localize`Description must be at least 3 characters long.`, + 'maxlength': $localize`Description cannot be more than 1000 characters long.` + } +} + +export const USER_TERMS_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.requiredTrue + ], + MESSAGES: { + 'required': $localize`You must agree with the instance terms in order to register on it.` + } +} + +export const USER_BAN_REASON_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.minLength(3), + Validators.maxLength(250) + ], + MESSAGES: { + 'minlength': $localize`Ban reason must be at least 3 characters long.`, + 'maxlength': $localize`Ban reason cannot be more than 250 characters long.` + } +} + +function buildDisplayNameValidator (required: boolean) { + const control = { + VALIDATORS: [ + Validators.minLength(1), + Validators.maxLength(120) + ], + MESSAGES: { + 'required': $localize`Display name is required.`, + 'minlength': $localize`Display name must be at least 1 character long.`, + 'maxlength': $localize`Display name cannot be more than 50 characters long.` + } + } + + if (required) control.VALIDATORS.push(Validators.required) + + return control +} diff --git a/client/src/app/shared/form-validators/video-block-validators.ts b/client/src/app/shared/form-validators/video-block-validators.ts new file mode 100644 index 000000000..d3974aefe --- /dev/null +++ b/client/src/app/shared/form-validators/video-block-validators.ts @@ -0,0 +1,10 @@ +import { Validators } from '@angular/forms' +import { BuildFormValidator } from './form-validator.model' + +export const VIDEO_BLOCK_REASON_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.minLength(2), Validators.maxLength(300) ], + MESSAGES: { + 'minlength': $localize`Block reason must be at least 2 characters long.`, + 'maxlength': $localize`Block reason cannot be more than 300 characters long.` + } +} diff --git a/client/src/app/shared/form-validators/video-captions-validators.ts b/client/src/app/shared/form-validators/video-captions-validators.ts new file mode 100644 index 000000000..9742d2925 --- /dev/null +++ b/client/src/app/shared/form-validators/video-captions-validators.ts @@ -0,0 +1,16 @@ +import { Validators } from '@angular/forms' +import { BuildFormValidator } from './form-validator.model' + +export const VIDEO_CAPTION_LANGUAGE_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.required ], + MESSAGES: { + 'required': $localize`Video caption language is required.` + } +} + +export const VIDEO_CAPTION_FILE_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.required ], + MESSAGES: { + 'required': $localize`Video caption file is required.` + } +} diff --git a/client/src/app/shared/form-validators/video-channel-validators.ts b/client/src/app/shared/form-validators/video-channel-validators.ts new file mode 100644 index 000000000..0daab22ce --- /dev/null +++ b/client/src/app/shared/form-validators/video-channel-validators.ts @@ -0,0 +1,52 @@ +import { Validators } from '@angular/forms' +import { BuildFormValidator } from './form-validator.model' + +export const VIDEO_CHANNEL_NAME_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.required, + Validators.minLength(1), + Validators.maxLength(50), + Validators.pattern(/^[a-z0-9][a-z0-9._]*$/) + ], + MESSAGES: { + 'required': $localize`Name is required.`, + 'minlength': $localize`Name must be at least 1 character long.`, + 'maxlength': $localize`Name cannot be more than 50 characters long.`, + 'pattern': $localize`Name should be lowercase alphanumeric; dots and underscores are allowed.` + } +} + +export const VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.required, + Validators.minLength(1), + Validators.maxLength(50) + ], + MESSAGES: { + 'required': $localize`Display name is required.`, + 'minlength': $localize`Display name must be at least 1 character long.`, + 'maxlength': $localize`Display name cannot be more than 50 characters long.` + } +} + +export const VIDEO_CHANNEL_DESCRIPTION_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.minLength(3), + Validators.maxLength(1000) + ], + MESSAGES: { + 'minlength': $localize`Description must be at least 3 characters long.`, + 'maxlength': $localize`Description cannot be more than 1000 characters long.` + } +} + +export const VIDEO_CHANNEL_SUPPORT_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.minLength(3), + Validators.maxLength(1000) + ], + MESSAGES: { + 'minlength': $localize`Support text must be at least 3 characters long.`, + 'maxlength': $localize`Support text cannot be more than 1000 characters long` + } +} diff --git a/client/src/app/shared/form-validators/video-comment-validators.ts b/client/src/app/shared/form-validators/video-comment-validators.ts new file mode 100644 index 000000000..c56564d34 --- /dev/null +++ b/client/src/app/shared/form-validators/video-comment-validators.ts @@ -0,0 +1,11 @@ +import { Validators } from '@angular/forms' +import { BuildFormValidator } from './form-validator.model' + +export const VIDEO_COMMENT_TEXT_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.required, Validators.minLength(1), Validators.maxLength(3000) ], + MESSAGES: { + 'required': $localize`Comment is required.`, + 'minlength': $localize`Comment must be at least 2 characters long.`, + 'maxlength': $localize`Comment cannot be more than 3000 characters long.` + } +} diff --git a/client/src/app/shared/form-validators/video-ownership-change-validators.ts b/client/src/app/shared/form-validators/video-ownership-change-validators.ts new file mode 100644 index 000000000..e1a2df8a6 --- /dev/null +++ b/client/src/app/shared/form-validators/video-ownership-change-validators.ts @@ -0,0 +1,25 @@ +import { AbstractControl, ValidationErrors, Validators } from '@angular/forms' +import { BuildFormValidator } from './form-validator.model' + +export const OWNERSHIP_CHANGE_CHANNEL_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.required ], + MESSAGES: { + 'required': $localize`The channel is required.` + } +} + +export const OWNERSHIP_CHANGE_USERNAME_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.required, localAccountValidator ], + MESSAGES: { + 'required': $localize`The username is required.`, + 'localAccountOnly': $localize`You can only transfer ownership to a local account` + } +} + +function localAccountValidator (control: AbstractControl): ValidationErrors { + if (control.value.includes('@')) { + return { 'localAccountOnly': true } + } + + return null +} diff --git a/client/src/app/shared/form-validators/video-playlist-validators.ts b/client/src/app/shared/form-validators/video-playlist-validators.ts new file mode 100644 index 000000000..7e3d29458 --- /dev/null +++ b/client/src/app/shared/form-validators/video-playlist-validators.ts @@ -0,0 +1,54 @@ +import { Validators, AbstractControl } from '@angular/forms' +import { BuildFormValidator } from './form-validator.model' +import { VideoPlaylistPrivacy } from '@shared/models' + +export const VIDEO_PLAYLIST_DISPLAY_NAME_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.required, + Validators.minLength(1), + Validators.maxLength(120) + ], + MESSAGES: { + 'required': $localize`Display name is required.`, + 'minlength': $localize`Display name must be at least 1 character long.`, + 'maxlength': $localize`Display name cannot be more than 120 characters long.` + } +} + +export const VIDEO_PLAYLIST_PRIVACY_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.required + ], + MESSAGES: { + 'required': $localize`Privacy is required.` + } +} + +export const VIDEO_PLAYLIST_DESCRIPTION_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ + Validators.minLength(3), + Validators.maxLength(1000) + ], + MESSAGES: { + 'minlength': $localize`Description must be at least 3 characters long.`, + 'maxlength': $localize`Description cannot be more than 1000 characters long.` + } +} + +export const VIDEO_PLAYLIST_CHANNEL_ID_VALIDATOR: BuildFormValidator = { + VALIDATORS: [], + MESSAGES: { + 'required': $localize`The channel is required when the playlist is public.` + } +} + +export function setPlaylistChannelValidator (channelControl: AbstractControl, privacy: VideoPlaylistPrivacy) { + if (privacy.toString() === VideoPlaylistPrivacy.PUBLIC.toString()) { + channelControl.setValidators([Validators.required]) + } else { + channelControl.setValidators(null) + } + + channelControl.markAsDirty() + channelControl.updateValueAndValidity() +} diff --git a/client/src/app/shared/form-validators/video-validators.ts b/client/src/app/shared/form-validators/video-validators.ts new file mode 100644 index 000000000..23f2391b2 --- /dev/null +++ b/client/src/app/shared/form-validators/video-validators.ts @@ -0,0 +1,101 @@ +import { AbstractControl, ValidationErrors, ValidatorFn, Validators } from '@angular/forms' +import { BuildFormValidator } from './form-validator.model' + +export const VIDEO_NAME_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.required, Validators.minLength(3), Validators.maxLength(120) ], + MESSAGES: { + 'required': $localize`Video name is required.`, + 'minlength': $localize`Video name must be at least 3 characters long.`, + 'maxlength': $localize`Video name cannot be more than 120 characters long.` + } +} + +export const VIDEO_PRIVACY_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.required ], + MESSAGES: { + 'required': $localize`Video privacy is required.` + } +} + +export const VIDEO_CATEGORY_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ ], + MESSAGES: {} +} + +export const VIDEO_LICENCE_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ ], + MESSAGES: {} +} + +export const VIDEO_LANGUAGE_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ ], + MESSAGES: {} +} + +export const VIDEO_IMAGE_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ ], + MESSAGES: {} +} + +export const VIDEO_CHANNEL_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.required ], + MESSAGES: { + 'required': $localize`Video channel is required.` + } +} + +export const VIDEO_DESCRIPTION_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.minLength(3), Validators.maxLength(10000) ], + MESSAGES: { + 'minlength': $localize`Video description must be at least 3 characters long.`, + 'maxlength': $localize`Video description cannot be more than 10000 characters long.` + } +} + +export const VIDEO_TAG_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.minLength(2), Validators.maxLength(30) ], + MESSAGES: { + 'minlength': $localize`A tag should be more than 2 characters long.`, + 'maxlength': $localize`A tag should be less than 30 characters long.` + } +} + +export const VIDEO_TAGS_ARRAY_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.maxLength(5), arrayTagLengthValidator() ], + MESSAGES: { + 'maxlength': $localize`A maximum of 5 tags can be used on a video.`, + 'arrayTagLength': $localize`A tag should be more than 2, and less than 30 characters long.` + } +} + +export const VIDEO_SUPPORT_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ Validators.minLength(3), Validators.maxLength(1000) ], + MESSAGES: { + 'minlength': $localize`Video support must be at least 3 characters long.`, + 'maxlength': $localize`Video support cannot be more than 1000 characters long.` + } +} + +export const VIDEO_SCHEDULE_PUBLICATION_AT_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ ], + MESSAGES: { + 'required': $localize`A date is required to schedule video update.` + } +} + +export const VIDEO_ORIGINALLY_PUBLISHED_AT_VALIDATOR: BuildFormValidator = { + VALIDATORS: [ ], + MESSAGES: {} +} + +function arrayTagLengthValidator (min = 2, max = 30): ValidatorFn { + return (control: AbstractControl): ValidationErrors => { + const array = control.value as Array + + if (array.every(e => e.length > min && e.length < max)) { + return null + } + + return { 'arrayTagLength': true } + } +} -- cgit v1.2.3