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,13 +115,8 @@ 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) {
categories = _.keyBy(categories, 'id')
rows.forEach((row, i) => {
row.categories.forEach(category => { row.categories.forEach(category => {
if (!categories[category]) { if (!categories[category]) {
errors.push({ errors.push({
@ -109,19 +125,24 @@ async function validateChannelCategories(rows) {
}) })
} }
}) })
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) {
languages = _.keyBy(languages, 'code')
rows.forEach((row, i) => {
row.languages.forEach(language => { row.languages.forEach(language => {
if (!languages[language]) { if (!languages[language]) {
errors.push({ errors.push({
@ -130,27 +151,18 @@ async function validateChannelLanguages(rows) {
}) })
} }
}) })
})
}
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) {
channels = _.keyBy(channels, 'id')
rows.forEach((row, i) => {
if (!channels[row.channel]) { if (!channels[row.channel]) {
errors.push({ errors.push({
line: i + 2, line: i + 2,
message: `"${row.channel}" is missing in the channels.csv` message: `"${row.channel}" is missing in the channels.csv`
}) })
} }
})
}
return errors return errors
} }