aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/abuses.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/custom-validators/abuses.ts')
-rw-r--r--server/helpers/custom-validators/abuses.ts54
1 files changed, 54 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/abuses.ts b/server/helpers/custom-validators/abuses.ts
new file mode 100644
index 000000000..a6a895c65
--- /dev/null
+++ b/server/helpers/custom-validators/abuses.ts
@@ -0,0 +1,54 @@
1import validator from 'validator'
2import { abusePredefinedReasonsMap, AbusePredefinedReasonsString, AbuseVideoIs } from '@shared/models'
3import { CONSTRAINTS_FIELDS, ABUSE_STATES } from '../../initializers/constants'
4import { exists, isArray } from './misc'
5
6const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSES
7
8function isAbuseReasonValid (value: string) {
9 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
10}
11
12function isAbusePredefinedReasonValid (value: AbusePredefinedReasonsString) {
13 return exists(value) && value in abusePredefinedReasonsMap
14}
15
16function isAbusePredefinedReasonsValid (value: AbusePredefinedReasonsString[]) {
17 return exists(value) && isArray(value) && value.every(v => v in abusePredefinedReasonsMap)
18}
19
20function isAbuseTimestampValid (value: number) {
21 return value === null || (exists(value) && validator.isInt('' + value, { min: 0 }))
22}
23
24function isAbuseTimestampCoherent (endAt: number, { req }) {
25 return exists(req.body.startAt) && endAt > req.body.startAt
26}
27
28function isAbuseModerationCommentValid (value: string) {
29 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.MODERATION_COMMENT)
30}
31
32function isAbuseStateValid (value: string) {
33 return exists(value) && ABUSE_STATES[value] !== undefined
34}
35
36function isAbuseVideoIsValid (value: AbuseVideoIs) {
37 return exists(value) && (
38 value === 'deleted' ||
39 value === 'blacklisted'
40 )
41}
42
43// ---------------------------------------------------------------------------
44
45export {
46 isAbuseReasonValid,
47 isAbusePredefinedReasonValid,
48 isAbusePredefinedReasonsValid,
49 isAbuseTimestampValid,
50 isAbuseTimestampCoherent,
51 isAbuseModerationCommentValid,
52 isAbuseStateValid,
53 isAbuseVideoIsValid
54}