Skip to content
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

Add --strict mode #319

Merged
merged 2 commits into from
Jul 29, 2024
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
4 changes: 3 additions & 1 deletion cli/src/commands/validate/spectral.result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { ValidationOutput } from './validation.output';

export class SpectralResult {
public errors: boolean;
public warnings: boolean;
public spectralIssues: ValidationOutput[];

constructor(errors: boolean, spectralIssues: ValidationOutput[]) {
constructor(warnings: boolean, errors: boolean, spectralIssues: ValidationOutput[]) {
this.warnings = warnings;
this.errors = errors;
this.spectralIssues = spectralIssues;
}
Expand Down
61 changes: 59 additions & 2 deletions cli/src/commands/validate/validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe('validate', () => {
code: 'no-empty-properties',
message: 'Must not contain string properties set to the empty string or numerical properties set to zero',
severity: 0,
path: JSON.parse(JSON.stringify('$..*')),
path: ['/nodes'],
range: { start: { line: 1, character: 1 }, end: { line: 2, character: 1 } }
}
];
Expand All @@ -173,7 +173,7 @@ describe('validate', () => {
code: 'no-empty-properties',
message: 'Must not contain string properties set to the empty string or numerical properties set to zero',
severity: 0,
path: JSON.parse(JSON.stringify('$..*')),
path: ['/nodes'],
range: { start: { line: 1, character: 1 }, end: { line: 2, character: 1 } }
}
];
Expand All @@ -190,6 +190,63 @@ describe('validate', () => {
.rejects
.toThrow();
});

it('exits with error when spectral returns warnings, but failOnWarnings is set', async () => {
const expectedSpectralOutput: ISpectralDiagnostic[] = [
{
code: 'no-empty-properties',
message: 'Must not contain string properties set to the empty string or numerical properties set to zero',
severity: 0,
path: ['/nodes'],
range: { start: { line: 1, character: 1 }, end: { line: 2, character: 1 } }
}
];

mockRunFunction.mockReturnValue(expectedSpectralOutput);

const apiGateway = readFileSync(path.resolve(__dirname, '../../../test_fixtures/api-gateway-with-no-relationships.json'), 'utf8');
fetchMock.mock('http://exist/api-gateway.json', apiGateway);

const apiGatewayInstantiation = readFileSync(path.resolve(__dirname, '../../../test_fixtures/api-gateway-implementation.json'), 'utf8');
fetchMock.mock('https://exist/api-gateway-implementation.json', apiGatewayInstantiation);

await expect(validate('https://exist/api-gateway-implementation.json', 'http://exist/api-gateway.json', metaSchemaLocation, debugDisabled, jsonFormat, undefined, true))
.rejects
.toThrow();
});

it('completes successfully when the spectral validation returns warnings only and failOnWarnings is not set', async () => {
const mockExit = jest.spyOn(process, 'exit')
.mockImplementation((code) => {
console.log(code);
if (code != 0) {
throw new Error();
}
return undefined as never;
});

const expectedSpectralOutput: ISpectralDiagnostic[] = [
{
code: 'warning-test',
message: 'Test warning',
severity: 1,
path: [ 'nodes' ],
range: { start: { line: 1, character: 1 }, end: { line: 2, character: 1 } }
}
];

mockRunFunction.mockReturnValue(expectedSpectralOutput);

const apiGateway = readFileSync(path.resolve(__dirname, '../../../test_fixtures/api-gateway.json'), 'utf8');
fetchMock.mock('http://exist/api-gateway.json', apiGateway);

const apiGatewayInstantiation = readFileSync(path.resolve(__dirname, '../../../test_fixtures/api-gateway-implementation.json'), 'utf8');
fetchMock.mock('https://exist/api-gateway-implementation.json', apiGatewayInstantiation);

await validate('https://exist/api-gateway-implementation.json', 'http://exist/api-gateway.json', metaSchemaLocation, debugDisabled, jsonFormat, undefined, false);

expect(mockExit).toHaveBeenCalledWith(0);
});

it('exits with error when the meta schema location is not a directory', async () => {
await expect(validate('https://url/with/non/json/response', 'http://exist/api-gateway.json', 'test_fixtures/api-gateway.json', debugDisabled, jsonFormat))
Expand Down
17 changes: 15 additions & 2 deletions cli/src/commands/validate/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ export default async function validate(
metaSchemaPath: string,
debug: boolean = false,
format: string,
output?: string
output?: string,
failOnWarnings: boolean = false
) {
logger = initLogger(debug);
let errors = false;
let warnings = false;
let validations: ValidationOutput[] = [];
try {
const ajv = buildAjv2020(debug);
Expand All @@ -45,6 +47,7 @@ export default async function validate(
const spectralResult: SpectralResult = await runSpectralValidations(jsonSchemaInstantiation, stripRefs(jsonSchema), spectralRulesetForInstantiation, spectralRulesetForPattern);

errors = spectralResult.errors;
warnings = spectralResult.warnings;
validations = validations.concat(spectralResult.spectralIssues);

let jsonSchemaValidations = [];
Expand All @@ -63,6 +66,11 @@ export default async function validate(
logger.error(`The following issues have been found on the JSON Schema instantiation ${validationsOutput}`);
process.exit(1);
}

if (warnings && failOnWarnings) {
logger.error(`No errors were reported. However, there were warnings, and --strict was provided. The following warnings were encountered: ${validationsOutput}`);
process.exit(1);
}

logger.info('The JSON Schema instantiation is valid');
if(validationsOutput != '[]'){
Expand Down Expand Up @@ -139,6 +147,7 @@ async function runSpectralValidations(
): Promise<SpectralResult> {

let errors = false;
let warnings = false;
let spectralIssues: ValidationOutput[] = [];
const spectral = new Spectral();

Expand All @@ -154,8 +163,12 @@ async function runSpectralValidations(
logger.debug('Spectral output contains errors');
errors = true;
}
if (issues.filter(issue => issue.severity === 1).length > 0) {
logger.debug('Spectral output contains warnings');
warnings = true;
}
}
return new SpectralResult(errors, spectralIssues);
return new SpectralResult(warnings, errors, spectralIssues);
}

function formatJsonSchemaOutput(jsonSchemaIssues: ErrorObject[]): ValidationOutput[]{
Expand Down
3 changes: 2 additions & 1 deletion cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ program
.requiredOption('-p, --pattern <pattern>', 'Path to the pattern file to use. May be a file path or a URL.')
.requiredOption('-i, --instantiation <instantiation>', 'Path to the pattern instantiation file to use. May be a file path or a URL.')
.option('-m, --metaSchemasLocation <metaSchemaLocation>', 'The location of the directory of the meta schemas to be loaded', CALM_META_SCHEMA_DIRECTORY)
.option('--strict', 'When run in strict mode, the CLI will fail if any warnings are reported.', false)
.addOption(
new Option('-f, --format <format>', 'The format of the output')
.choices(['json', 'junit'])
Expand All @@ -54,7 +55,7 @@ program
.option('-o, --output <output>', 'Path location at which to output the generated file.')
.option('-v, --verbose', 'Enable verbose logging.', false)
.action(async (options) =>
await validate(options.instantiation, options.pattern, options.metaSchemasLocation, options.verbose, options.format, options.output)
await validate(options.instantiation, options.pattern, options.metaSchemasLocation, options.verbose, options.format, options.output, options.strict)
);

program.parse(process.argv);