-
Notifications
You must be signed in to change notification settings - Fork 2.4k
enhance(build): validate built data.json against schemas
#27685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
05e52bf
81d7b1d
fae5c18
fabb4f2
019e2e6
5622a61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import fs from 'node:fs/promises'; | |
| import { relative } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| import betterAjvErrors from 'better-ajv-errors'; | ||
| import esMain from 'es-main'; | ||
| import stringify from 'fast-json-stable-stringify'; | ||
| import { compareVersions } from 'compare-versions'; | ||
|
|
@@ -13,6 +14,9 @@ import { marked } from 'marked'; | |
| import { InternalSupportStatement } from '../../types/index.js'; | ||
| import { BrowserName, CompatData, VersionValue } from '../../types/types.js'; | ||
| import compileTS from '../generate-types.js'; | ||
| import compatDataSchema from '../../schemas/compat-data.schema.json' with { type: 'json' }; | ||
| import browserDataSchema from '../../schemas/browsers.schema.json' with { type: 'json' }; | ||
| import { createAjv } from '../lib/ajv.js'; | ||
| import { walk } from '../../utils/index.js'; | ||
| import { WalkOutput } from '../../utils/walk.js'; | ||
| import bcd from '../../index.js'; | ||
|
|
@@ -219,6 +223,35 @@ export const createDataBundle = async (): Promise<CompatData> => { | |
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Validates the given data against the schema. | ||
| * @param data - The data to validate. | ||
| */ | ||
| const validate = (data: CompatData) => { | ||
| const ajv = createAjv(); | ||
|
|
||
| for (const [key, value] of Object.entries(data)) { | ||
| if (key === '__meta') { | ||
| // Not covered by the schema. | ||
| continue; | ||
| } | ||
|
|
||
| const schema = key === 'browsers' ? browserDataSchema : compatDataSchema; | ||
| const data = { [key]: value }; | ||
| if (!ajv.validate(schema, data)) { | ||
| const errors = ajv.errors || []; | ||
| if (!errors.length) { | ||
| console.error(`${key} data failed validation with unknown errors!`); | ||
| } | ||
| // Output messages by one since better-ajv-errors wrongly joins messages | ||
| // (see https://github.com/atlassian/better-ajv-errors/pull/21) | ||
| errors.forEach((e) => { | ||
| console.error(betterAjvErrors(schema, data, [e], { indent: 2 })); | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /* c8 ignore start */ | ||
|
|
||
| /** | ||
|
|
@@ -227,6 +260,7 @@ export const createDataBundle = async (): Promise<CompatData> => { | |
| const writeData = async () => { | ||
| const dest = new URL('data.json', targetdir); | ||
| const data = await createDataBundle(); | ||
| validate(data); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note If validation fails, the validation errors are logged via |
||
| await fs.writeFile(dest, stringify(data)); | ||
| logWrite(dest, 'data'); | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { Ajv } from 'ajv'; | ||
| import ajvErrors from 'ajv-errors'; | ||
| import ajvFormats from 'ajv-formats'; | ||
|
|
||
| /** | ||
| * Returns a new pre-configured Ajv instance. | ||
| * @returns the Ajv instance. | ||
| */ | ||
| export const createAjv = (): Ajv => { | ||
| const ajv = new Ajv({ allErrors: true }); | ||
| // We use 'fast' because as a side effect that makes the "uri" format more lax. | ||
| // By default the "uri" format rejects ① and similar in URLs. | ||
| ajvFormats.default(ajv, { mode: 'fast' }); | ||
| // Allow for custom error messages to provide better directions for contributors | ||
| ajvErrors.default(ajv); | ||
|
|
||
| // Define keywords for schema->TS converter | ||
| ajv.addKeyword('tsEnumNames'); | ||
| ajv.addKeyword('tsName'); | ||
| ajv.addKeyword('tsType'); | ||
|
|
||
| return ajv; | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.