Skip to content

style: better readability for schema-downloader tool #974

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

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions tools/schema-downloader/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,34 @@ const BomXsd = Object.freeze({
])
})

const bomSchemaEnumMatch = /("\$id": "(http:\/\/cyclonedx\.org\/schema\/bom.+?\.schema\.json)".*"enum": \[\s+")http:\/\/cyclonedx\.org\/schema\/bom.+?\.schema\.json"/sg
const bomSchemaEnumReplace = '$1$2"'

const bomRequired = `
/* "version" is not required but optional with a default value!
this is wrong in schema<1.5 */
const _bomRequired = `
"required": [
"bomFormat",
"specVersion",
"version"
],`
const bomRequiredReplace = `
const _bomRequiredReplace = `
"required": [
"bomFormat",
"specVersion"
],`

const defaultWithPatternMatch = /\s+"default": "",(?![^}]*?"pattern": "\^\(\.\*\)\$")/gm
const defaultWithPatternReplace = ''

const BomJsonLax = Object.freeze({
versions: ['1.5', '1.4', '1.3', '1.2'],
sourcePattern: `${SOURCE_ROOT}bom-%s.schema.json`,
targetPattern: join(TARGET_ROOT, 'bom-%s.SNAPSHOT.schema.json'),
replace: Object.freeze([
Object.freeze(['spdx.schema.json', 'spdx.SNAPSHOT.schema.json']),
Object.freeze(['jsf-0.82.schema.json', 'jsf-0.82.SNAPSHOT.schema.json']),
Object.freeze([bomSchemaEnumMatch, bomSchemaEnumReplace]),
Object.freeze([bomRequired, bomRequiredReplace])
// with current SchemaValidator this is no longer required: defaultWithPatternMatch -> defaultWithPatternReplace
// Object.freeze([defaultWithPatternMatch, defaultWithPatternReplace])
/* fix "$schema" property to match $id */
Object.freeze([/("\$id": "(http:\/\/cyclonedx\.org\/schema\/bom.+?\.schema\.json)".*"enum": \[\s+")http:\/\/cyclonedx\.org\/schema\/bom.+?\.schema\.json"/sg, '$1$2"']),
Object.freeze([_bomRequired, _bomRequiredReplace])
/* there was a case where the default value did not match the own pattern ...
this is wrong in schema<1.5
with current SchemaValidator this is no longer required, as defaults are not applied */
// Object.freeze([/\s+"default": "",(?![^}]*?"pattern": "\^\(\.\*\)\$")/gm, ''])
])
})

Expand All @@ -91,8 +90,8 @@ for (const dSpec of [BomXsd, BomJsonLax, BomJsonStrict]) {
fetch(source, FetchOptions).then(res => res.text()).then(
/** @param {string} text */
text => new Promise((resolve, reject) => {
for (const [searchValue, replaceValue] of dSpec.replace) {
text = text.replaceAll(searchValue, replaceValue)
for (const [search, replace] of dSpec.replace) {
text = text.replaceAll(search, replace)
}
resolve(text)
})).then(text => writeFile(target, text))
Expand Down