Update validate.js

This commit is contained in:
Aleksandr Statciuk 2022-04-09 15:23:56 +03:00
parent c569cc2e6e
commit b211bf904d

View file

@ -42,10 +42,31 @@ async function main() {
if (/\"/.test(csvString)) return handleError(`\" character is not allowed (${filepath})`) if (/\"/.test(csvString)) return handleError(`\" character is not allowed (${filepath})`)
fileErrors = fileErrors.concat(findDuplicatesById(rows)) fileErrors = fileErrors.concat(findDuplicatesById(rows))
fileErrors = fileErrors.concat(await validateChannelCategories(rows)) let categories = await csv
fileErrors = fileErrors.concat(await validateChannelLanguages(rows)) .fromFile('data/categories.csv')
.catch(err => handleError(err.message))
categories = _.keyBy(categories, 'id')
let languages = await csv
.fromFile('data/languages.csv')
.catch(err => handleError(err.message))
languages = _.keyBy(languages, 'code')
let countries = await csv
.fromFile('data/countries.csv')
.catch(err => handleError(err.message))
countries = _.keyBy(countries, 'code')
for (const [i, row] of rows.entries()) {
fileErrors = fileErrors.concat(await validateChannelCategories(row, i, categories))
fileErrors = fileErrors.concat(await validateChannelLanguages(row, i, languages))
fileErrors = fileErrors.concat(await validateChannelCountry(row, i, countries))
}
} else if (filename === 'blocklist') { } else if (filename === 'blocklist') {
fileErrors = fileErrors.concat(await validateChannelId(rows)) let channels = await csv.fromFile('data/channels.csv').catch(err => handleError(err.message))
channels = _.keyBy(channels, 'id')
for (const [i, row] of rows.entries()) {
fileErrors = fileErrors.concat(await validateChannelId(row, i, channels))
}
} }
const schema = Joi.object(schemes[filename]) const schema = Joi.object(schemes[filename])
@ -94,61 +115,52 @@ function findDuplicatesById(data) {
return errors return errors
} }
async function validateChannelCategories(rows) { async function validateChannelCategories(row, i, categories) {
let categories = await csv.fromFile('data/categories.csv').catch(err => handleError(err.message))
const errors = [] const errors = []
if (categories.length) { row.categories.forEach(category => {
categories = _.keyBy(categories, 'id') if (!categories[category]) {
rows.forEach((row, i) => { errors.push({
row.categories.forEach(category => { line: i + 2,
if (!categories[category]) { message: `"${row.id}" has the wrong category "${category}"`
errors.push({
line: i + 2,
message: `"${row.id}" has the wrong category "${category}"`
})
}
}) })
}
})
return errors
}
async function validateChannelCountry(row, i, countries) {
const errors = []
if (!countries[row.country]) {
errors.push({
line: i + 2,
message: `"${row.id}" has the wrong country "${row.country}"`
}) })
} }
return errors return errors
} }
async function validateChannelLanguages(rows) { async function validateChannelLanguages(row, i, languages) {
let languages = await csv.fromFile('data/languages.csv').catch(err => handleError(err.message))
const errors = [] const errors = []
if (languages.length) { row.languages.forEach(language => {
languages = _.keyBy(languages, 'code') if (!languages[language]) {
rows.forEach((row, i) => { errors.push({
row.languages.forEach(language => { line: i + 2,
if (!languages[language]) { message: `"${row.id}" has the wrong language "${language}"`
errors.push({
line: i + 2,
message: `"${row.id}" has the wrong language "${language}"`
})
}
}) })
}) }
} })
return errors return errors
} }
async function validateChannelId(rows) { async function validateChannelId(row, i, channels) {
let channels = await csv.fromFile('data/channels.csv').catch(err => handleError(err.message))
const errors = [] const errors = []
if (channels.length) { if (!channels[row.channel]) {
channels = _.keyBy(channels, 'id') errors.push({
rows.forEach((row, i) => { line: i + 2,
if (!channels[row.channel]) { message: `"${row.channel}" is missing in the channels.csv`
errors.push({
line: i + 2,
message: `"${row.channel}" is missing in the channels.csv`
})
}
}) })
} }