Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

validate-records.js 1.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  1. const assert = require('assert')
  2. const { isArray, isString, inRange } = require('lodash')
  3. const isURL = require('is-url')
  4. const countArrayValues = require('count-array-values')
  5. const { maxRecordLength } = require('../../lib/search/config')
  6. module.exports = function validateRecords (name, records) {
  7. assert(isString(name) && name.length, '`name` is required')
  8. assert(isArray(records) && records.length, '`records` must be a non-empty array')
  9. // each ID is unique
  10. const objectIDs = records.map(record => record.objectID)
  11. const dupes = countArrayValues(objectIDs)
  12. .filter(({ value, count }) => count > 1)
  13. .map(({ value }) => value)
  14. assert(!dupes.length, `every objectID must be unique. dupes: ${dupes.join('; ')}`)
  15. records.forEach(record => {
  16. assert(
  17. isString(record.objectID) && record.objectID.length,
  18. `objectID must be a string. received: ${record.objectID}, ${JSON.stringify(record)}`
  19. )
  20. assert(
  21. isString(record.title) && record.title.length,
  22. `title must be a string. received: ${record.title}, ${JSON.stringify(record)}`
  23. )
  24. assert(
  25. isURL(record.url),
  26. `url must be a fully qualified URL. received: ${record.url}, ${JSON.stringify(record)}`
  27. )
  28. assert(
  29. inRange(record.customRanking, 0, 4),
  30. `customRanking must be an in-range number. received: ${record.customRanking}, (record: ${record.url})`
  31. )
  32. const recordLength = JSON.stringify(record).length
  33. assert(
  34. recordLength <= maxRecordLength,
  35. `record ${record.url} is too long! ${recordLength} (max: ${maxRecordLength})`
  36. )
  37. })
  38. return true
  39. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...