1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-04 02:09:37 +02:00

Refractor validators

This commit is contained in:
Chocobozzz 2017-11-27 17:30:46 +01:00
parent fcaf1e0aa8
commit a2431b7dcb
No known key found for this signature in database
GPG key ID: 583A612D890159BE
33 changed files with 478 additions and 643 deletions

View file

@ -1,17 +1,16 @@
import * as Bluebird from 'bluebird'
import * as express from 'express'
import { Response } from 'express'
import 'express-validator'
import * as validator from 'validator'
import { database as db } from '../../initializers'
import { AccountInstance } from '../../models'
import { logger } from '../logger'
import { isUserUsernameValid } from './users'
function isAccountNameValid (value: string) {
return isUserUsernameValid(value)
}
function checkAccountIdExists (id: number | string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) {
function isAccountIdExist (id: number | string, res: Response) {
let promise: Bluebird<AccountInstance>
if (validator.isInt('' + id)) {
@ -20,36 +19,35 @@ function checkAccountIdExists (id: number | string, res: express.Response, callb
promise = db.Account.loadByUUID('' + id)
}
return checkAccountExists(promise, res, callback)
return isAccountExist(promise, res)
}
function checkLocalAccountNameExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) {
const p = db.Account.loadLocalByName(name)
function isLocalAccountNameExist (name: string, res: Response) {
const promise = db.Account.loadLocalByName(name)
return checkAccountExists(p, res, callback)
return isAccountExist(promise, res)
}
function checkAccountExists (p: Bluebird<AccountInstance>, res: express.Response, callback: (err: Error, account: AccountInstance) => any) {
p.then(account => {
if (!account) {
return res.status(404)
.send({ error: 'Account not found' })
.end()
}
async function isAccountExist (p: Bluebird<AccountInstance>, res: Response) {
const account = await p
res.locals.account = account
return callback(null, account)
})
.catch(err => {
logger.error('Error in account request validator.', err)
return res.sendStatus(500)
})
if (!account) {
res.status(404)
.send({ error: 'Account not found' })
.end()
return false
}
res.locals.account = account
return true
}
// ---------------------------------------------------------------------------
export {
checkAccountIdExists,
checkLocalAccountNameExists,
isAccountIdExist,
isLocalAccountNameExist,
isAccountNameValid
}