aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/form-validators/video-validators.ts
blob: ab6f21a350fe99e298165a96fb921d4042a7b4ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { AbstractControl, ValidationErrors, ValidatorFn, Validators, FormControl } from '@angular/forms'
import { BuildFormValidator } from './form-validator.model'

export const trimValidator: ValidatorFn = (control: FormControl) => {
  if (control.value.startsWith(' ') || control.value.endsWith(' ')) {
    return { spaces: true }
  }

  return null
}

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.`,
    spaces: $localize`Video name has leading or trailing whitespace.`
  }
}

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 1 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: [ ], // Required is set dynamically
  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<string>

    if (array.every(e => e.length >= min && e.length <= max)) {
      return null
    }

    return { arrayTagLength: true }
  }
}