aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/accounts.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-27 17:30:46 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:43:01 +0100
commita2431b7dcbc72c05101dcdbe631ff84a823aeb51 (patch)
tree09278a822905622a70ff976a75e09d99bc45639a /server/helpers/custom-validators/accounts.ts
parentfcaf1e0aa84213a1b1f1b1a44a3276eae35ebe70 (diff)
downloadPeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.tar.gz
PeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.tar.zst
PeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.zip
Refractor validators
Diffstat (limited to 'server/helpers/custom-validators/accounts.ts')
-rw-r--r--server/helpers/custom-validators/accounts.ts46
1 files changed, 22 insertions, 24 deletions
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts
index a6d7f2b82..e3c477414 100644
--- a/server/helpers/custom-validators/accounts.ts
+++ b/server/helpers/custom-validators/accounts.ts
@@ -1,17 +1,16 @@
1import * as Bluebird from 'bluebird' 1import * as Bluebird from 'bluebird'
2import * as express from 'express' 2import { Response } from 'express'
3import 'express-validator' 3import 'express-validator'
4import * as validator from 'validator' 4import * as validator from 'validator'
5import { database as db } from '../../initializers' 5import { database as db } from '../../initializers'
6import { AccountInstance } from '../../models' 6import { AccountInstance } from '../../models'
7import { logger } from '../logger'
8import { isUserUsernameValid } from './users' 7import { isUserUsernameValid } from './users'
9 8
10function isAccountNameValid (value: string) { 9function isAccountNameValid (value: string) {
11 return isUserUsernameValid(value) 10 return isUserUsernameValid(value)
12} 11}
13 12
14function checkAccountIdExists (id: number | string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { 13function isAccountIdExist (id: number | string, res: Response) {
15 let promise: Bluebird<AccountInstance> 14 let promise: Bluebird<AccountInstance>
16 15
17 if (validator.isInt('' + id)) { 16 if (validator.isInt('' + id)) {
@@ -20,36 +19,35 @@ function checkAccountIdExists (id: number | string, res: express.Response, callb
20 promise = db.Account.loadByUUID('' + id) 19 promise = db.Account.loadByUUID('' + id)
21 } 20 }
22 21
23 return checkAccountExists(promise, res, callback) 22 return isAccountExist(promise, res)
24} 23}
25 24
26function checkLocalAccountNameExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { 25function isLocalAccountNameExist (name: string, res: Response) {
27 const p = db.Account.loadLocalByName(name) 26 const promise = db.Account.loadLocalByName(name)
28 27
29 return checkAccountExists(p, res, callback) 28 return isAccountExist(promise, res)
30} 29}
31 30
32function checkAccountExists (p: Bluebird<AccountInstance>, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { 31async function isAccountExist (p: Bluebird<AccountInstance>, res: Response) {
33 p.then(account => { 32 const account = await p
34 if (!account) { 33
35 return res.status(404) 34 if (!account) {
36 .send({ error: 'Account not found' }) 35 res.status(404)
37 .end() 36 .send({ error: 'Account not found' })
38 } 37 .end()
39 38
40 res.locals.account = account 39 return false
41 return callback(null, account) 40 }
42 }) 41
43 .catch(err => { 42 res.locals.account = account
44 logger.error('Error in account request validator.', err) 43
45 return res.sendStatus(500) 44 return true
46 })
47} 45}
48 46
49// --------------------------------------------------------------------------- 47// ---------------------------------------------------------------------------
50 48
51export { 49export {
52 checkAccountIdExists, 50 isAccountIdExist,
53 checkLocalAccountNameExists, 51 isLocalAccountNameExist,
54 isAccountNameValid 52 isAccountNameValid
55} 53}