123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- import 'express-validator'
- import * as validator from 'validator'
- import { UserRole } from '../../../shared'
- import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES } from '../../initializers/constants'
- import { exists, isArray, isBooleanValid, isFileValid } from './misc'
- import { values } from 'lodash'
- const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
- function isUserPasswordValid (value: string) {
- return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
- }
- function isUserVideoQuotaValid (value: string) {
- return exists(value) && validator.isInt(value + '', USERS_CONSTRAINTS_FIELDS.VIDEO_QUOTA)
- }
- function isUserVideoQuotaDailyValid (value: string) {
- return exists(value) && validator.isInt(value + '', USERS_CONSTRAINTS_FIELDS.VIDEO_QUOTA_DAILY)
- }
- function isUserUsernameValid (value: string) {
- const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
- const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
- return exists(value) && validator.matches(value, new RegExp(`^[a-z0-9._]{${min},${max}}$`))
- }
- function isUserDisplayNameValid (value: string) {
- return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.NAME))
- }
- function isUserDescriptionValid (value: string) {
- return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.DESCRIPTION))
- }
- function isUserEmailVerifiedValid (value: any) {
- return isBooleanValid(value)
- }
- const nsfwPolicies = values(NSFW_POLICY_TYPES)
- function isUserNSFWPolicyValid (value: any) {
- return exists(value) && nsfwPolicies.indexOf(value) !== -1
- }
- function isUserWebTorrentEnabledValid (value: any) {
- return isBooleanValid(value)
- }
- function isUserVideosHistoryEnabledValid (value: any) {
- return isBooleanValid(value)
- }
- function isUserAutoPlayVideoValid (value: any) {
- return isBooleanValid(value)
- }
- function isUserVideoLanguages (value: any) {
- return value === null || (isArray(value) && value.length < CONSTRAINTS_FIELDS.USERS.VIDEO_LANGUAGES.max)
- }
- function isUserAdminFlagsValid (value: any) {
- return exists(value) && validator.isInt('' + value)
- }
- function isUserBlockedValid (value: any) {
- return isBooleanValid(value)
- }
- function isUserBlockedReasonValid (value: any) {
- return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.BLOCKED_REASON))
- }
- function isUserRoleValid (value: any) {
- return exists(value) && validator.isInt('' + value) && UserRole[value] !== undefined
- }
- const avatarMimeTypes = CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
- .map(v => v.replace('.', ''))
- .join('|')
- const avatarMimeTypesRegex = `image/(${avatarMimeTypes})`
- function isAvatarFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
- return isFileValid(files, avatarMimeTypesRegex, 'avatarfile', CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max)
- }
- // ---------------------------------------------------------------------------
- export {
- isUserVideosHistoryEnabledValid,
- isUserBlockedValid,
- isUserPasswordValid,
- isUserVideoLanguages,
- isUserBlockedReasonValid,
- isUserRoleValid,
- isUserVideoQuotaValid,
- isUserVideoQuotaDailyValid,
- isUserUsernameValid,
- isUserAdminFlagsValid,
- isUserEmailVerifiedValid,
- isUserNSFWPolicyValid,
- isUserWebTorrentEnabledValid,
- isUserAutoPlayVideoValid,
- isUserDisplayNameValid,
- isUserDescriptionValid,
- isAvatarFile
- }
|