diff --git a/.changeset/fix-suppress-all-warnings-errors.md b/.changeset/fix-suppress-all-warnings-errors.md new file mode 100644 index 000000000..1a9cafdd3 --- /dev/null +++ b/.changeset/fix-suppress-all-warnings-errors.md @@ -0,0 +1,5 @@ +--- +"@asyncapi/cli": patch +--- + +fix(validate): only suppress warning-severity rules and ignore non-ruleset diagnostic codes in `--suppressAllWarnings` diff --git a/src/domains/services/validation.service.ts b/src/domains/services/validation.service.ts index e87599d5a..334da6608 100644 --- a/src/domains/services/validation.service.ts +++ b/src/domains/services/validation.service.ts @@ -333,11 +333,12 @@ export class ValidationService extends BaseService { const allRuleNames = Array.from( new Set( diagnostics + .filter((d) => d.severity === DiagnosticSeverity.Warning) .map((d) => d.code) .filter((c): c is string => typeof c === 'string'), ), ); - return this.buildCustomParser(allRuleNames); + return this.buildParserWithSpecificWarningsSuppressed(allRuleNames); } /** diff --git a/test/integration/validate.test.ts b/test/integration/validate.test.ts index ea888cafc..96c2768d1 100644 --- a/test/integration/validate.test.ts +++ b/test/integration/validate.test.ts @@ -360,6 +360,24 @@ describe('validate', () => { }); }); + describe('validate command with --suppressAllWarnings flag', () => { + test + .stderr() + .stdout() + .command([ + 'validate', + path.join('test', 'fixtures', 'specification-invalid.yml'), + '--suppressAllWarnings' + ]) + .it('should suppress warnings while still failing on errors', (ctx, done) => { + expect(ctx.stderr).to.include('have governance issues.'); + expect(ctx.stdout).to.include('asyncapi-document-resolved'); + expect(ctx.stdout).to.not.include('asyncapi-id'); + expect(process.exitCode).to.equal(1); + done(); + }); + }); + describe('with --save-output flag', () => { beforeEach(() => { testHelper.createDummyContextFile(); diff --git a/test/unit/services/validation.service.test.ts b/test/unit/services/validation.service.test.ts index efb25eff9..b8423f07c 100644 --- a/test/unit/services/validation.service.test.ts +++ b/test/unit/services/validation.service.test.ts @@ -171,6 +171,50 @@ describe('ValidationService', () => { expect(result.data?.diagnostics).to.be.an('array'); } }); + + it('should suppress only warnings and preserve error diagnostics when suppressAllWarnings is true', async () => { + const specFile = new Specification(invalidAsyncAPI); + + const result = await validationService.validateDocument(specFile, { + suppressAllWarnings: true + }); + + expect(result.success).to.equal(true); + if (result.success) { + expect(result.data?.status).to.equal('invalid'); + expect(result.data?.diagnostics?.some((d: any) => d.severity === 0)).to.equal(true); + expect(result.data?.diagnostics?.some((d: any) => d.severity === 1)).to.equal(false); + } + }); + + it('should not throw on invalid-ref diagnostics when suppressAllWarnings is true', async () => { + const brokenRefSpec = new Specification(JSON.stringify({ + asyncapi: '2.6.0', + info: { + title: 'Test Service', + version: '1.0.0' + }, + channels: { + 'user/signedup': { + subscribe: { + message: { + $ref: './non-existent-file.yaml#/messages/UserSignedUp' + } + } + } + } + }), { filepath: '/tmp/broken-ref-spec.json' }); + + const result = await validationService.validateDocument(brokenRefSpec, { + suppressAllWarnings: true + }); + + expect(result.success).to.equal(true); + if (result.success) { + expect(result.data?.status).to.equal('invalid'); + expect(result.data?.diagnostics?.some((d: any) => d.code === 'invalid-ref')).to.equal(true); + } + }); }); describe('validateDocument() with external URLs', () => {