Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add --json option #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 16 additions & 2 deletions bin/swagger-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ function parseArgs () {
.option("h", {
alias: "help",
type: "boolean",
})
.option("j", {
alias: "json",
type: "boolean",
});

// Show the version number on "--version" or "-v"
Expand Down Expand Up @@ -123,6 +127,7 @@ function parseArgs () {
wrap: args.wrap || Infinity,
debug: args.debug,
help: args.help,
json: args.json,
}
};

Expand All @@ -145,10 +150,19 @@ function parseArgs () {
async function validate (file, options) {
try {
await api.validate(file, options);
console.log(file, "is valid");
if (options.json) {
console.log(JSON.stringify({ valid: true, error: [] }));
} else {
console.log(file, "is valid");
}
}
catch (error) {
errorHandler(error);
if (options.json) {
console.error(JSON.stringify({ valid: false, error }));
process.exit(1);
} else {
errorHandler(error);
}
}
}

Expand Down
34 changes: 34 additions & 0 deletions test/specs/validate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ describe("swagger-cli validate", () => {
expect(output.stdout).to.equal("test/files/valid/single-file/api.yaml is valid\n");
});

it("should validate a single-file API (JSON output)", () => {
let output = helper.run(
"--json",
"validate",
"test/files/valid/single-file/api.yaml"
);

expect(output.stderr).to.have.lengthOf(0);
expect(output.status).to.equal(0);
expect(JSON.parse(output.stdout)).to.deep.equal({
valid: true,
error: [],
});
});

it("should validate a multi-file API", () => {
let output = helper.run("validate", "test/files/valid/multi-file/api.yaml");

Expand Down Expand Up @@ -53,6 +68,25 @@ describe("swagger-cli validate", () => {
expect(output.stderr).to.equal("Validation failed. /paths/people/{name}/get is missing path parameter(s) for {name}\n");
});

it("should fail validation against the Swagger 2.0 specification (JSON output)", () => {
let output = helper.run(
"--json",
"validate",
"test/files/invalid/spec/api.yaml"
);

expect(output.stdout).to.have.lengthOf(0);
expect(output.status).to.equal(1);

const parsed = JSON.parse(output.stderr);
expect(parsed.valid).to.be.false;
expect(parsed.error.message).to.equal(
"Validation failed. /paths/people/{name}/get is missing path parameter(s) for {name}"
);
expect(parsed.error.name).to.equal("SyntaxError");
expect(parsed.error.inner).to.be.undefined;
});

it("should skip validation against the Swagger 2.0 specification", () => {
let output = helper.run("validate", "--no-spec", "test/files/invalid/spec/api.yaml");

Expand Down