Update validate.js

This commit is contained in:
Aleksandr Statciuk 2022-11-05 05:19:49 +03:00
parent ffd35e395c
commit 3b3f500c10

View file

@ -76,6 +76,7 @@ async function main() {
let fileErrors = [] let fileErrors = []
if (filename === 'channels') { if (filename === 'channels') {
fileErrors = fileErrors.concat(findDuplicatesById(rowsCopy)) fileErrors = fileErrors.concat(findDuplicatesById(rowsCopy))
// fileErrors = fileErrors.concat(findDuplicatesByName(rowsCopy))
for (const [i, row] of rowsCopy.entries()) { for (const [i, row] of rowsCopy.entries()) {
fileErrors = fileErrors.concat(validateChannelId(row, i)) fileErrors = fileErrors.concat(validateChannelId(row, i))
fileErrors = fileErrors.concat(validateChannelBroadcastArea(row, i)) fileErrors = fileErrors.concat(validateChannelBroadcastArea(row, i))
@ -129,48 +130,40 @@ async function main() {
main() main()
function findDuplicatesById(rows) { function findDuplicatesById(rows) {
rows = rows.map(row => {
row.normId = row.id.toLowerCase()
return row
})
const errors = [] const errors = []
const schema = Joi.array().unique((a, b) => a.normId === b.normId) const buffer = {}
const { error } = schema.validate(rows, { abortEarly: false }) rows.forEach((row, i) => {
if (error) { const normId = row.id.toLowerCase()
error.details.forEach(detail => { if (buffer[normId]) {
errors.push({ errors.push({
line: detail.context.pos + 2, line: i + 2,
message: `entry with the id "${detail.context.value.id}" already exists` message: `entry with the id "${row.id}" already exists`
})
}) })
} }
buffer[normId] = true
})
return errors return errors
} }
// function findDuplicatesByName(rows) { function findDuplicatesByName(rows) {
// rows = rows.map(row => { const errors = []
// row.name = row.name.toLowerCase() const buffer = {}
rows.forEach((row, i) => {
const normName = row.name.toLowerCase()
if (buffer[normName]) {
errors.push({
line: i + 2,
message: `entry with the name "${row.name}" already exists`
})
}
// return row buffer[normName] = true
// }) })
// const errors = [] return errors
// const schema = Joi.array().unique((a, b) => a.name === b.name) }
// const { error } = schema.validate(rows, { abortEarly: false })
// if (error) {
// error.details.forEach(detail => {
// errors.push({
// line: detail.context.pos + 2,
// message: `entry with the name "${detail.context.value.name}" already exists`
// })
// })
// }
// return errors
// }
function validateChannelId(row, i) { function validateChannelId(row, i) {
const errors = [] const errors = []