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

38 lines
1.0 KiB
TypeScript
Raw Normal View History

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