1
0
mirror of synced 2025-12-20 10:28:40 -05:00
Files
docs/script/search/validate-records.js
2022-02-01 21:27:47 +00:00

38 lines
1.2 KiB
JavaScript

#!/usr/bin/env node
import assert from 'assert'
import { isArray, isString } from 'lodash-es'
function countArrayValues(arr) {
const counter = new Map()
arr.forEach((value) => counter.set(value, (counter.get(value) || 0) + 1))
return [...counter.entries()].map(([value, count]) => {
return { value, count }
})
}
export default function validateRecords(name, records) {
assert(isString(name) && name.length, '`name` is required')
assert(isArray(records) && records.length, '`records` must be a non-empty array')
// each ID is unique
const objectIDs = records.map((record) => record.objectID)
const dupes = countArrayValues(objectIDs)
.filter(({ count }) => count > 1)
.map(({ value }) => value)
assert(!dupes.length, `every objectID must be unique. dupes: ${dupes.join('; ')}`)
records.forEach((record) => {
assert(
isString(record.objectID) && record.objectID.length,
`objectID must be a string. received: ${record.objectID}, ${JSON.stringify(record)}`
)
assert(
isString(record.title) && record.title.length,
`title must be a string. received: ${record.title}, ${JSON.stringify(record)}`
)
})
return true
}