diff options
Diffstat (limited to 'server/helpers/custom-validators')
-rw-r--r-- | server/helpers/custom-validators/follows.ts | 20 | ||||
-rw-r--r-- | server/helpers/custom-validators/misc.ts | 15 | ||||
-rw-r--r-- | server/helpers/custom-validators/servers.ts | 1 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-channels.ts | 11 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-ownership.ts | 2 |
5 files changed, 43 insertions, 6 deletions
diff --git a/server/helpers/custom-validators/follows.ts b/server/helpers/custom-validators/follows.ts index fbef7ad87..8f65552c3 100644 --- a/server/helpers/custom-validators/follows.ts +++ b/server/helpers/custom-validators/follows.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | import { exists } from './misc' | 1 | import { exists, isArray } from './misc' |
2 | import { FollowState } from '@shared/models' | 2 | import { FollowState } from '@shared/models' |
3 | 3 | ||
4 | function isFollowStateValid (value: FollowState) { | 4 | function isFollowStateValid (value: FollowState) { |
@@ -7,8 +7,24 @@ function isFollowStateValid (value: FollowState) { | |||
7 | return value === 'pending' || value === 'accepted' | 7 | return value === 'pending' || value === 'accepted' |
8 | } | 8 | } |
9 | 9 | ||
10 | function isRemoteHandleValid (value: string) { | ||
11 | if (!exists(value)) return false | ||
12 | if (typeof value !== 'string') return false | ||
13 | |||
14 | return value.includes('@') | ||
15 | } | ||
16 | |||
17 | function isEachUniqueHandleValid (handles: string[]) { | ||
18 | return isArray(handles) && | ||
19 | handles.every(handle => { | ||
20 | return isRemoteHandleValid(handle) && handles.indexOf(handle) === handles.lastIndexOf(handle) | ||
21 | }) | ||
22 | } | ||
23 | |||
10 | // --------------------------------------------------------------------------- | 24 | // --------------------------------------------------------------------------- |
11 | 25 | ||
12 | export { | 26 | export { |
13 | isFollowStateValid | 27 | isFollowStateValid, |
28 | isRemoteHandleValid, | ||
29 | isEachUniqueHandleValid | ||
14 | } | 30 | } |
diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index 528bfcfb8..c19a3e5eb 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts | |||
@@ -23,6 +23,10 @@ function isNotEmptyIntArray (value: any) { | |||
23 | return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0 | 23 | return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0 |
24 | } | 24 | } |
25 | 25 | ||
26 | function isNotEmptyStringArray (value: any) { | ||
27 | return Array.isArray(value) && value.every(v => typeof v === 'string' && v.length !== 0) && value.length !== 0 | ||
28 | } | ||
29 | |||
26 | function isArrayOf (value: any, validator: (value: any) => boolean) { | 30 | function isArrayOf (value: any, validator: (value: any) => boolean) { |
27 | return isArray(value) && value.every(v => validator(v)) | 31 | return isArray(value) && value.every(v => validator(v)) |
28 | } | 32 | } |
@@ -39,6 +43,10 @@ function isUUIDValid (value: string) { | |||
39 | return exists(value) && validator.isUUID('' + value, 4) | 43 | return exists(value) && validator.isUUID('' + value, 4) |
40 | } | 44 | } |
41 | 45 | ||
46 | function areUUIDsValid (values: string[]) { | ||
47 | return isArray(values) && values.every(v => isUUIDValid(v)) | ||
48 | } | ||
49 | |||
42 | function isIdOrUUIDValid (value: string) { | 50 | function isIdOrUUIDValid (value: string) { |
43 | return isIdValid(value) || isUUIDValid(value) | 51 | return isIdValid(value) || isUUIDValid(value) |
44 | } | 52 | } |
@@ -132,6 +140,10 @@ function toCompleteUUID (value: string) { | |||
132 | return value | 140 | return value |
133 | } | 141 | } |
134 | 142 | ||
143 | function toCompleteUUIDs (values: string[]) { | ||
144 | return values.map(v => toCompleteUUID(v)) | ||
145 | } | ||
146 | |||
135 | function toIntOrNull (value: string) { | 147 | function toIntOrNull (value: string) { |
136 | const v = toValueOrNull(value) | 148 | const v = toValueOrNull(value) |
137 | 149 | ||
@@ -179,7 +191,9 @@ export { | |||
179 | isIntOrNull, | 191 | isIntOrNull, |
180 | isIdValid, | 192 | isIdValid, |
181 | isSafePath, | 193 | isSafePath, |
194 | isNotEmptyStringArray, | ||
182 | isUUIDValid, | 195 | isUUIDValid, |
196 | toCompleteUUIDs, | ||
183 | toCompleteUUID, | 197 | toCompleteUUID, |
184 | isIdOrUUIDValid, | 198 | isIdOrUUIDValid, |
185 | isDateValid, | 199 | isDateValid, |
@@ -187,6 +201,7 @@ export { | |||
187 | toBooleanOrNull, | 201 | toBooleanOrNull, |
188 | isBooleanValid, | 202 | isBooleanValid, |
189 | toIntOrNull, | 203 | toIntOrNull, |
204 | areUUIDsValid, | ||
190 | toArray, | 205 | toArray, |
191 | toIntArray, | 206 | toIntArray, |
192 | isFileFieldValid, | 207 | isFileFieldValid, |
diff --git a/server/helpers/custom-validators/servers.ts b/server/helpers/custom-validators/servers.ts index adf1ea497..c0f8b6aeb 100644 --- a/server/helpers/custom-validators/servers.ts +++ b/server/helpers/custom-validators/servers.ts | |||
@@ -19,7 +19,6 @@ function isHostValid (host: string) { | |||
19 | 19 | ||
20 | function isEachUniqueHostValid (hosts: string[]) { | 20 | function isEachUniqueHostValid (hosts: string[]) { |
21 | return isArray(hosts) && | 21 | return isArray(hosts) && |
22 | hosts.length !== 0 && | ||
23 | hosts.every(host => { | 22 | hosts.every(host => { |
24 | return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host) | 23 | return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host) |
25 | }) | 24 | }) |
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index ded5d5171..249083f39 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts | |||
@@ -1,14 +1,20 @@ | |||
1 | import validator from 'validator' | 1 | import validator from 'validator' |
2 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' | 2 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' |
3 | import { exists } from './misc' | 3 | import { exists } from './misc' |
4 | import { isUserUsernameValid } from './users' | ||
4 | 5 | ||
5 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS | 6 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS |
6 | 7 | ||
8 | function isVideoChannelUsernameValid (value: string) { | ||
9 | // Use the same constraints than user username | ||
10 | return isUserUsernameValid(value) | ||
11 | } | ||
12 | |||
7 | function isVideoChannelDescriptionValid (value: string) { | 13 | function isVideoChannelDescriptionValid (value: string) { |
8 | return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) | 14 | return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) |
9 | } | 15 | } |
10 | 16 | ||
11 | function isVideoChannelNameValid (value: string) { | 17 | function isVideoChannelDisplayNameValid (value: string) { |
12 | return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) | 18 | return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) |
13 | } | 19 | } |
14 | 20 | ||
@@ -19,7 +25,8 @@ function isVideoChannelSupportValid (value: string) { | |||
19 | // --------------------------------------------------------------------------- | 25 | // --------------------------------------------------------------------------- |
20 | 26 | ||
21 | export { | 27 | export { |
28 | isVideoChannelUsernameValid, | ||
22 | isVideoChannelDescriptionValid, | 29 | isVideoChannelDescriptionValid, |
23 | isVideoChannelNameValid, | 30 | isVideoChannelDisplayNameValid, |
24 | isVideoChannelSupportValid | 31 | isVideoChannelSupportValid |
25 | } | 32 | } |
diff --git a/server/helpers/custom-validators/video-ownership.ts b/server/helpers/custom-validators/video-ownership.ts index 0e1c63bad..cf15b385a 100644 --- a/server/helpers/custom-validators/video-ownership.ts +++ b/server/helpers/custom-validators/video-ownership.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import { Response } from 'express' | 1 | import { Response } from 'express' |
2 | import { MUserId } from '@server/types/models' | 2 | import { MUserId } from '@server/types/models' |
3 | import { MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership' | 3 | import { MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership' |
4 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | 4 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
5 | 5 | ||
6 | function checkUserCanTerminateOwnershipChange (user: MUserId, videoChangeOwnership: MVideoChangeOwnershipFull, res: Response) { | 6 | function checkUserCanTerminateOwnershipChange (user: MUserId, videoChangeOwnership: MVideoChangeOwnershipFull, res: Response) { |
7 | if (videoChangeOwnership.NextOwner.userId === user.id) { | 7 | if (videoChangeOwnership.NextOwner.userId === user.id) { |