1
0
mirror of synced 2026-01-05 21:04:17 -05:00
Files
docs/tests/linting/lint-secret-scanning-data.js
Sarah Schneider 5ee7bfe4ac Secret scanning structured data (#33117)
Co-authored-by: Rachael Sewell <rachmari@github.com>
2023-03-03 18:20:50 +00:00

40 lines
1.3 KiB
JavaScript

import fs from 'fs'
import yaml from 'js-yaml'
import { get } from 'lodash-es'
import Ajv from 'ajv'
import addErrors from 'ajv-errors'
import semver from 'semver'
import schema from '../helpers/schemas/secret-scanning-schema.js'
const data = yaml.load(fs.readFileSync('data/secret-scanning.yml', 'utf8'))
const ajv = new Ajv({ allErrors: true, allowUnionTypes: true })
addErrors(ajv)
// *** TODO: We can drop this override once the frontmatter schema has been updated to work with AJV. ***
ajv.addFormat('semver', {
validate: (x) => semver.validRange(x),
})
// *** End TODO ***
const validate = ajv.compile(schema)
test('make sure secret scanning data matches the schema', () => {
validate(data)
const errors = (validate.errors || []).map((errorObj) => {
// We have to use AJV's instancePath, which is an index number, to find out which entries are invalid.
const split = errorObj.instancePath.split('/')
split.shift()
const index = split.shift()
const entry = data[index]
const path = split.length ? split.join('.') : null
return path
? `The entry with provider '${entry.provider}' (at '${path}: ${get(entry, path)}') ${
errorObj.message
}`
: `The entry with provider '${entry.provider}' ${errorObj.message}`
})
expect(errors.length, errors.join('\n ')).toBe(0)
})