omnivore/packages/web/utils/csvValidator.ts

106 lines
2 KiB
TypeScript
Raw Normal View History

2023-08-14 10:14:08 +00:00
import CSVFileValidator, { ValidatorConfig } from 'csv-file-validator'
import dayjs from 'dayjs'
2023-08-14 10:14:08 +00:00
const isUrlValid = (url: string | number | boolean) => {
if (typeof url !== 'string') {
return false
}
try {
new URL(url)
return true
} catch (e) {
return false
}
}
const isStateValid = (state: string | number | boolean) => {
if (typeof state !== 'string') {
return false
}
const validStates = ['SUCCEEDED', 'ARCHIVED']
return validStates.includes(state.toUpperCase())
}
const isDateValid = (date: string | number | boolean) => {
const dateString = date.toString()
// date is unix timestamp in milliseconds
if (dateString.length !== 13) {
return false
}
const timestamp = parseInt(dateString, 10)
if (isNaN(timestamp)) {
return false
}
return dayjs(timestamp).isValid()
}
2023-08-14 10:14:08 +00:00
const csvConfig: ValidatorConfig = {
headers: [
{
name: 'url',
inputName: 'url',
required: true,
unique: true,
validate: function (url) {
return isUrlValid(url)
},
},
{
name: 'state',
inputName: 'state',
required: false,
optional: true,
validate: function (state) {
2023-08-15 10:09:16 +00:00
if (!state) {
return true
}
2023-08-14 10:14:08 +00:00
return isStateValid(state)
},
},
{
name: 'labels',
inputName: 'labels',
required: false,
optional: true,
isArray: true,
},
{
name: 'saved_at',
inputName: 'saved_at',
required: false,
optional: true,
validate: function (date) {
if (!date) {
return true
}
return isDateValid(date)
},
2023-08-14 10:14:08 +00:00
},
{
name: 'published_at',
inputName: 'published_at',
required: false,
optional: true,
validate: function (date) {
if (!date) {
return true
}
return isDateValid(date)
},
2023-08-14 10:14:08 +00:00
},
],
}
export const validateCsvFile = async (
file: string | File | NodeJS.ReadableStream
) => {
// validate csv file
return CSVFileValidator(file, csvConfig)
}