iptv-database/scripts/db/validate.js

162 lines
4.3 KiB
JavaScript
Raw Normal View History

2022-02-11 21:55:50 -05:00
const { logger, file, csv } = require('../core')
const { program } = require('commander')
const schemes = require('./schemes')
const chalk = require('chalk')
const Joi = require('joi')
2022-04-09 07:42:41 -04:00
const _ = require('lodash')
2022-02-11 21:55:50 -05:00
program.argument('[filepath]', 'Path to file to validate').parse(process.argv)
async function main() {
2022-04-09 08:11:13 -04:00
let globalErrors = []
2022-02-11 21:55:50 -05:00
const files = program.args.length
? program.args
: [
2022-02-13 22:11:47 -05:00
'data/blocklist.csv',
2022-02-11 21:55:50 -05:00
'data/categories.csv',
'data/channels.csv',
'data/countries.csv',
'data/languages.csv',
'data/regions.csv',
'data/subdivisions.csv'
]
for (const filepath of files) {
if (!filepath.endsWith('.csv')) continue
2022-02-17 09:33:35 -05:00
const eol = await file.eol(filepath)
2022-04-09 08:11:13 -04:00
if (eol !== 'CRLF') return handleError(`file must have line endings with CRLF (${filepath})`)
2022-02-17 09:33:35 -05:00
const csvString = await file.read(filepath)
2022-04-09 08:11:13 -04:00
if (/\s+$/.test(csvString))
return handleError(`empty lines at the end of file not allowed (${filepath})`)
2022-02-17 09:33:35 -05:00
2022-02-11 21:55:50 -05:00
const filename = file.getFilename(filepath)
2022-04-09 08:11:13 -04:00
if (!schemes[filename]) return handleError(`"${filename}" scheme is missing`)
2022-02-11 21:55:50 -05:00
2022-04-09 08:11:13 -04:00
const rows = await csv
.fromString(csvString)
.catch(err => handleError(`${err.message} (${filepath})`))
2022-02-21 06:07:37 -05:00
2022-02-11 21:55:50 -05:00
let fileErrors = []
if (filename === 'channels') {
2022-04-09 08:11:13 -04:00
if (/\"/.test(csvString)) return handleError(`\" character is not allowed (${filepath})`)
2022-04-08 21:02:02 -04:00
2022-04-09 08:11:13 -04:00
fileErrors = fileErrors.concat(findDuplicatesById(rows))
fileErrors = fileErrors.concat(await validateChannelCategories(rows))
fileErrors = fileErrors.concat(await validateChannelLanguages(rows))
2022-04-08 20:44:51 -04:00
} else if (filename === 'blocklist') {
2022-04-09 08:11:13 -04:00
fileErrors = fileErrors.concat(await validateChannelId(rows))
2022-02-11 21:55:50 -05:00
}
const schema = Joi.object(schemes[filename])
2022-04-09 08:11:13 -04:00
rows.forEach((row, i) => {
2022-02-11 21:55:50 -05:00
const { error } = schema.validate(row, { abortEarly: false })
if (error) {
error.details.forEach(detail => {
fileErrors.push({ line: i + 2, message: detail.message })
})
}
})
if (fileErrors.length) {
logger.info(`\n${chalk.underline(filepath)}`)
fileErrors.forEach(err => {
const position = err.line.toString().padEnd(6, ' ')
2022-04-09 08:11:13 -04:00
logger.info(` ${chalk.gray(position)} ${err.message}`)
2022-02-11 21:55:50 -05:00
})
2022-04-09 08:11:13 -04:00
globalErrors = globalErrors.concat(fileErrors)
2022-02-11 21:55:50 -05:00
}
}
2022-04-09 08:11:13 -04:00
if (globalErrors.length) return handleError(`${globalErrors.length} error(s)`)
2022-02-11 21:55:50 -05:00
}
main()
function findDuplicatesById(data) {
data = data.map(i => {
i.id = i.id.toLowerCase()
return i
})
const errors = []
const schema = Joi.array().unique((a, b) => a.id === b.id)
const { error } = schema.validate(data, { abortEarly: false })
if (error) {
error.details.forEach(detail => {
errors.push({
line: detail.context.pos + 2,
message: `Entry with the id "${detail.context.value.id}" already exists`
})
})
}
return errors
}
2022-04-08 21:02:02 -04:00
2022-04-09 08:11:13 -04:00
async function validateChannelCategories(rows) {
let categories = await csv.fromFile('data/categories.csv').catch(err => handleError(err.message))
const errors = []
if (categories.length) {
categories = _.keyBy(categories, 'id')
rows.forEach((row, i) => {
row.categories.forEach(category => {
if (!categories[category]) {
errors.push({
line: i + 2,
message: `"${row.id}" has the wrong category "${category}"`
})
}
})
})
}
return errors
}
async function validateChannelLanguages(rows) {
let languages = await csv.fromFile('data/languages.csv').catch(err => handleError(err.message))
const errors = []
if (languages.length) {
languages = _.keyBy(languages, 'code')
rows.forEach((row, i) => {
row.languages.forEach(language => {
if (!languages[language]) {
errors.push({
line: i + 2,
message: `"${row.id}" has the wrong language "${language}"`
})
}
})
})
}
return errors
}
async function validateChannelId(rows) {
let channels = await csv.fromFile('data/channels.csv').catch(err => handleError(err.message))
const errors = []
if (channels.length) {
channels = _.keyBy(channels, 'id')
rows.forEach((row, i) => {
if (!channels[row.channel]) {
errors.push({
line: i + 2,
message: `"${row.channel}" is missing in the channels.csv`
})
}
})
}
return errors
}
function handleError(message) {
logger.error(chalk.red(`\n${message}`))
process.exit(1)
2022-04-08 21:02:02 -04:00
}