1 import { AbstractControl, ValidationErrors, ValidatorFn, Validators, FormControl } from '@angular/forms'
2 import { BuildFormValidator } from './form-validator.model'
4 export const trimValidator: ValidatorFn = (control: FormControl) => {
5 if (control.value.startsWith(' ') || control.value.endsWith(' ')) {
6 return { spaces: true }
12 export const VIDEO_NAME_VALIDATOR: BuildFormValidator = {
13 VALIDATORS: [ Validators.required, Validators.minLength(3), Validators.maxLength(120) ],
15 required: $localize`Video name is required.`,
16 minlength: $localize`Video name must be at least 3 characters long.`,
17 maxlength: $localize`Video name cannot be more than 120 characters long.`,
18 spaces: $localize`Video name has leading or trailing whitespace.`
22 export const VIDEO_PRIVACY_VALIDATOR: BuildFormValidator = {
23 VALIDATORS: [ Validators.required ],
25 required: $localize`Video privacy is required.`
29 export const VIDEO_CATEGORY_VALIDATOR: BuildFormValidator = {
34 export const VIDEO_LICENCE_VALIDATOR: BuildFormValidator = {
39 export const VIDEO_LANGUAGE_VALIDATOR: BuildFormValidator = {
44 export const VIDEO_IMAGE_VALIDATOR: BuildFormValidator = {
49 export const VIDEO_CHANNEL_VALIDATOR: BuildFormValidator = {
50 VALIDATORS: [ Validators.required ],
52 required: $localize`Video channel is required.`
56 export const VIDEO_DESCRIPTION_VALIDATOR: BuildFormValidator = {
57 VALIDATORS: [ Validators.minLength(3), Validators.maxLength(10000) ],
59 minlength: $localize`Video description must be at least 3 characters long.`,
60 maxlength: $localize`Video description cannot be more than 10000 characters long.`
64 export const VIDEO_TAG_VALIDATOR: BuildFormValidator = {
65 VALIDATORS: [ Validators.minLength(2), Validators.maxLength(30) ],
67 minlength: $localize`A tag should be more than 2 characters long.`,
68 maxlength: $localize`A tag should be less than 30 characters long.`
72 export const VIDEO_TAGS_ARRAY_VALIDATOR: BuildFormValidator = {
73 VALIDATORS: [ Validators.maxLength(5), arrayTagLengthValidator() ],
75 maxlength: $localize`A maximum of 5 tags can be used on a video.`,
76 arrayTagLength: $localize`A tag should be more than 1 and less than 30 characters long.`
80 export const VIDEO_SUPPORT_VALIDATOR: BuildFormValidator = {
81 VALIDATORS: [ Validators.minLength(3), Validators.maxLength(1000) ],
83 minlength: $localize`Video support must be at least 3 characters long.`,
84 maxlength: $localize`Video support cannot be more than 1000 characters long.`
88 export const VIDEO_SCHEDULE_PUBLICATION_AT_VALIDATOR: BuildFormValidator = {
89 VALIDATORS: [ ], // Required is set dynamically
91 required: $localize`A date is required to schedule video update.`
95 export const VIDEO_ORIGINALLY_PUBLISHED_AT_VALIDATOR: BuildFormValidator = {
100 function arrayTagLengthValidator (min = 2, max = 30): ValidatorFn {
101 return (control: AbstractControl): ValidationErrors => {
102 const array = control.value as Array<string>
104 if (array.every(e => e.length >= min && e.length <= max)) {
108 return { arrayTagLength: true }