Update validate.js

This commit is contained in:
Aleksandr Statciuk 2022-04-16 14:28:06 +03:00
parent c32202fec8
commit 321404efc8

View file

@ -18,6 +18,7 @@ const allFiles = [
] ]
let db = {} let db = {}
let files = {}
async function main() { async function main() {
let globalErrors = [] let globalErrors = []
@ -26,39 +27,42 @@ async function main() {
if (!filepath.endsWith('.csv')) continue if (!filepath.endsWith('.csv')) continue
const eol = await file.eol(filepath) const eol = await file.eol(filepath)
if (eol !== 'CRLF') return handleError(`file must have line endings with CRLF (${filepath})`) if (eol !== 'CRLF')
return handleError(`Error: file must have line endings with CRLF (${filepath})`)
const csvString = await file.read(filepath) const csvString = await file.read(filepath)
if (/\s+$/.test(csvString)) if (/\s+$/.test(csvString))
return handleError(`empty lines at the end of file not allowed (${filepath})`) return handleError(`Error: empty lines at the end of file not allowed (${filepath})`)
const filename = file.getFilename(filepath) const filename = file.getFilename(filepath)
let data = await csv let data = await csv
.fromString(csvString) .fromString(csvString)
.catch(err => handleError(`${err.message} (${filepath})`)) .catch(err => handleError(`${err.message} (${filepath})`))
let grouped
switch (filename) { switch (filename) {
case 'blocklist': case 'blocklist':
data = _.keyBy(data, 'channel') grouped = _.keyBy(data, 'channel')
break break
case 'categories': case 'categories':
case 'channels': case 'channels':
data = _.keyBy(data, 'id') grouped = _.keyBy(data, 'id')
break break
default: default:
data = _.keyBy(data, 'code') grouped = _.keyBy(data, 'code')
break break
} }
db[filename] = data db[filename] = grouped
files[filename] = data
} }
const toCheck = program.args.length ? program.args : allFiles const toCheck = program.args.length ? program.args : allFiles
for (const filepath of toCheck) { for (const filepath of toCheck) {
const filename = file.getFilename(filepath) const filename = file.getFilename(filepath)
if (!schemes[filename]) return handleError(`"${filename}" scheme is missing`) if (!schemes[filename]) return handleError(`Error: "${filename}" scheme is missing`)
const rows = Object.values(db[filename]) const rows = files[filename]
let fileErrors = [] let fileErrors = []
if (filename === 'channels') { if (filename === 'channels') {
@ -113,20 +117,21 @@ async function main() {
main() main()
function findDuplicatesById(data) { function findDuplicatesById(rows) {
data = data.map(i => { rows = rows.map(row => {
i.id = i.id.toLowerCase() row.id = row.id.toLowerCase()
return i
return row
}) })
const errors = [] const errors = []
const schema = Joi.array().unique((a, b) => a.id === b.id) const schema = Joi.array().unique((a, b) => a.id === b.id)
const { error } = schema.validate(data, { abortEarly: false }) const { error } = schema.validate(rows, { abortEarly: false })
if (error) { if (error) {
error.details.forEach(detail => { error.details.forEach(detail => {
errors.push({ errors.push({
line: detail.context.pos + 2, line: detail.context.pos + 2,
message: `Entry with the id "${detail.context.value.id}" already exists` message: `entry with the id "${detail.context.value.id}" already exists`
}) })
}) })
} }