Skip to content
Merged
Show file tree
Hide file tree
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
46 changes: 37 additions & 9 deletions tools/src/main/js/linter/checks/schema-comment.check.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* CycloneDX Schema Linter - Schema Comment Check
*
*
* Validates that the root $comment property contains the required
* OWASP CycloneDX standard notice.
*
*
* @license Apache-2.0
*/

Expand All @@ -29,9 +29,10 @@ class SchemaCommentCheck extends LintCheck {

async run(schema, rawContent, config = {}) {
const issues = [];

const requiredComment = config.requiredComment ?? REQUIRED_COMMENT;

const requiredCommentPattern = config.requiredCommentPattern;

// Check if $comment exists at root level
if (!('$comment' in schema)) {
issues.push(this.createIssue(
Expand All @@ -41,19 +42,46 @@ class SchemaCommentCheck extends LintCheck {
));
return issues;
}

// Check if $comment matches required value
if (schema.$comment !== requiredComment) {

const comment = schema.$comment;
if (typeof comment !== 'string') {
issues.push(this.createIssue(
'Schema $comment is not string.',
'$.$comment',
{
actual: comment,
expected: 'any string'
}
));
return issues;
}

if (typeof requiredCommentPattern === 'string') {
const requiredCommentRE = new RegExp(requiredCommentPattern);
// Check if $comment matches required pattern
if (!requiredCommentRE.test(comment)) {
issues.push(this.createIssue(
'$comment does not match the required standard notice pattern.',
'$.$comment',
{
actual: comment,
expected: `matches ${requiredCommentRE.toString()}`
}
));
}
}
// Check if $comment exactly matches required value
else if (comment !== requiredComment) {
issues.push(this.createIssue(
'$comment does not match the required standard notice.',
'$.$comment',
{
actual: schema.$comment,
actual: comment,
expected: requiredComment
}
));
}

return issues;
}
}
Expand Down
8 changes: 6 additions & 2 deletions tools/src/main/js/linter/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Configuration:
{
"checks": {
"schema-id-pattern": {
"pattern": "^https://cyclonedx\\\\.org/schema/.*\\\\.schema\\\\.json$"
"pattern": "^https://cyclonedx\\\\.org/schema/.+\\\\.schema\\\\.json$"
},
"formatting-indent": {
"spaces": 2
Expand Down Expand Up @@ -184,7 +184,11 @@ function loadConfig(configPath) {
}
}

if (configPath && existsSync(configPath)) {
if (configPath) {
if (!existsSync(configPath)) {
console.error(`Missing config file: ${configPath}`);
process.exit(1);
}
try {
return JSON.parse(readFileSync(configPath, 'utf-8'));
} catch (err) {
Expand Down
Loading