PeerTube/server/core/helpers/custom-validators/search.ts

38 lines
992 B
TypeScript
Raw Normal View History

2020-01-07 14:56:07 +01:00
import validator from 'validator'
import { SearchTargetType } from '@peertube/peertube-models'
import { CONFIG } from '@server/initializers/config.js'
import { exists, isArray } from './misc.js'
2018-07-20 14:35:18 +02:00
function isNumberArray (value: any) {
return isArray(value) && value.every(v => validator.default.isInt('' + v))
2018-07-20 14:35:18 +02:00
}
function isStringArray (value: any) {
return isArray(value) && value.every(v => typeof v === 'string')
}
function isBooleanBothQueryValid (value: any) {
2018-07-20 18:31:49 +02:00
return value === 'true' || value === 'false' || value === 'both'
}
2020-06-09 16:39:45 +02:00
function isSearchTargetValid (value: SearchTargetType) {
if (!exists(value)) return true
const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX
if (value === 'local') return true
2020-06-09 16:39:45 +02:00
if (value === 'search-index' && searchIndexConfig.ENABLED) return true
return false
}
2018-07-20 14:35:18 +02:00
// ---------------------------------------------------------------------------
export {
isNumberArray,
2018-07-20 18:31:49 +02:00
isStringArray,
isBooleanBothQueryValid,
2020-06-09 16:39:45 +02:00
isSearchTargetValid
2018-07-20 14:35:18 +02:00
}