diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index a2d6b10..811d3a3 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -305,10 +305,14 @@ describe('action', () => { await main.run(); expect(runSpy).toHaveReturned(); - expect(process.exitCode).not.toBeDefined(); + expect(process.exitCode).toEqual(1); - expect(core.setFailed).toHaveBeenLastCalledWith( - 'JSON schema missing $schema key' + expect(core.error).toHaveBeenLastCalledWith( + 'JSON schema missing $schema key', + { + title: 'JSON Schema Validation Error', + file: '/foo/bar' + } ); }); @@ -359,6 +363,29 @@ describe('action', () => { expect(runSpy).toHaveReturned(); expect(process.exitCode).toEqual(1); + expect(core.error).toHaveBeenCalledWith( + 'Error while validating file: /foo/bar/baz/config.yml' + ); + expect(core.error).toHaveBeenLastCalledWith( + JSON.stringify( + { + instancePath: '', + schemaPath: '#/oneOf', + keyword: 'oneOf', + params: { + passingSchemas: [0, 1] + }, + message: 'must match exactly one schema in oneOf' + }, + null, + 4 + ), + { + title: 'JSON Schema Validation Error', + file: '/foo/bar/baz/config.yml' + } + ); + expect(core.setOutput).toHaveBeenCalledTimes(1); expect(core.setOutput).toHaveBeenLastCalledWith('valid', false); }); @@ -447,14 +474,36 @@ describe('action', () => { }); it('which are invalid', async () => { - mockGetBooleanInput({ 'fail-on-invalid': false }); + mockGetBooleanInput({ 'fail-on-invalid': true }); vi.mocked(fs.readFile).mockResolvedValueOnce(invalidSchemaContents); await main.run(); expect(runSpy).toHaveReturned(); - expect(process.exitCode).not.toBeDefined(); + expect(process.exitCode).toEqual(1); + expect(core.error).toHaveBeenCalledWith( + 'Error while validating file: /foo/bar/baz/config.yml' + ); + expect(core.error).toHaveBeenLastCalledWith( + JSON.stringify( + { + instancePath: '/properties/foobar/minLength', + schemaPath: '#/definitions/nonNegativeInteger/type', + keyword: 'type', + params: { + type: 'integer' + }, + message: 'must be integer' + }, + null, + 4 + ), + { + title: 'JSON Schema Validation Error', + file: '/foo/bar/baz/config.yml' + } + ); expect(core.setOutput).toHaveBeenCalledTimes(1); expect(core.setOutput).toHaveBeenLastCalledWith('valid', false); }); @@ -482,10 +531,14 @@ describe('action', () => { await main.run(); expect(runSpy).toHaveReturned(); - expect(process.exitCode).not.toBeDefined(); - - expect(core.setFailed).toHaveBeenLastCalledWith( - 'JSON schema missing $schema key' + expect(process.exitCode).toEqual(1); + + expect(core.error).toHaveBeenLastCalledWith( + 'JSON schema missing $schema key', + { + title: 'JSON Schema Validation Error', + file: '/foo/bar/baz/config.yml' + } ); }); }); diff --git a/dist/index.js b/dist/index.js index 1ded1bc..0d288a6 100644 --- a/dist/index.js +++ b/dist/index.js @@ -89913,7 +89913,12 @@ async function run() { // Load and compile the schema const schema = JSON.parse(await fs.readFile(schemaPath, 'utf-8')); if (typeof schema.$schema !== 'string') { - core.setFailed('JSON schema missing $schema key'); + core.error(`Error while validating schema: ${schemaPath}`); + core.error('JSON schema missing $schema key', { + title: 'JSON Schema Validation Error', + file: schemaPath + }); + process.exitCode = 1; return; } const ajv = newAjv(schema); @@ -89929,7 +89934,12 @@ async function run() { filesValidated = true; const instance = yaml.parse(await fs.readFile(file, 'utf-8')); if (validatingSchema && typeof instance.$schema !== 'string') { - core.setFailed('JSON schema missing $schema key'); + core.error(`Error while validating schema: ${file}`); + core.error('JSON schema missing $schema key', { + title: 'JSON Schema Validation Error', + file + }); + process.exitCode = 1; return; } const errors = await validate(instance); @@ -89937,7 +89947,11 @@ async function run() { valid = false; core.debug(`𐄂 ${file} is not valid`); for (const error of errors) { - core.error(JSON.stringify(error, null, 4)); + core.error(`Error while validating file: ${file}`); + core.error(JSON.stringify(error, null, 4), { + title: 'JSON Schema Validation Error', + file + }); } } else { diff --git a/src/main.ts b/src/main.ts index 3bb6f32..94a0b8a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -111,7 +111,12 @@ export async function run(): Promise { ); if (typeof schema.$schema !== 'string') { - core.setFailed('JSON schema missing $schema key'); + core.error(`Error while validating schema: ${schemaPath}`); + core.error('JSON schema missing $schema key', { + title: 'JSON Schema Validation Error', + file: schemaPath + }); + process.exitCode = 1; return; } @@ -134,7 +139,12 @@ export async function run(): Promise { const instance = yaml.parse(await fs.readFile(file, 'utf-8')); if (validatingSchema && typeof instance.$schema !== 'string') { - core.setFailed('JSON schema missing $schema key'); + core.error(`Error while validating schema: ${file}`); + core.error('JSON schema missing $schema key', { + title: 'JSON Schema Validation Error', + file + }); + process.exitCode = 1; return; } @@ -145,7 +155,11 @@ export async function run(): Promise { core.debug(`𐄂 ${file} is not valid`); for (const error of errors) { - core.error(JSON.stringify(error, null, 4)); + core.error(`Error while validating file: ${file}`); + core.error(JSON.stringify(error, null, 4), { + title: 'JSON Schema Validation Error', + file + }); } } else { core.debug(`✓ ${file} is valid`);