Skip to content

Commit

Permalink
fix: Guard only undefined and empty strings (#725)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImFlog committed Mar 12, 2024
1 parent f01a0d8 commit cc5067d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 12 deletions.
1 change: 1 addition & 0 deletions demo/examples/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@ components:
description: Average amount of honey produced per day in ounces
example: 3.14
multipleOf: .01
default: 0
required:
- honeyPerDay
Id:
Expand Down
48 changes: 48 additions & 0 deletions packages/docusaurus-theme-openapi-docs/src/markdown/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* ============================================================================
* Copyright (c) Palo Alto Networks
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* ========================================================================== */

import { guard } from "./utils";

describe("guard", () => {
it("should guard empty strings", () => {
const actual = guard("", (_) => {
throw new Error("Should not be called");
});
expect(actual).toBe("");
});

it("should guard undefined", () => {
const actual = guard(undefined, (value) => {
throw new Error("Should not be called");
});
expect(actual).toBe("");
});

it("should not guard strings", () => {
const actual = guard("hello", (value) => value);
expect(actual).toBe("hello");
});

it("should not guard numbers", () => {
const actual = guard(1, (value) => `${value}`);
expect(actual).toBe("1");
});

it("should not guard numbers equals to 0", () => {
const actual = guard(0, (value) => `${value}`);
expect(actual).toBe("0");
});

it("should not guard false booleans", () => {
const actual = guard(false, (value) => `${value}`);
expect(actual).toBe("false");
});
it("should not guard true booleans", () => {
const actual = guard(true, (value) => `${value}`);
expect(actual).toBe("true");
});
});
8 changes: 4 additions & 4 deletions packages/docusaurus-theme-openapi-docs/src/markdown/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export function guard<T>(
value: T | undefined | string,
cb: (value: T) => Children
): string {
if (!!value) {
const children = cb(value as T);
return render(children);
if (value === undefined || value === "") {
return "";
}
return "";
const children = cb(value as T);
return render(children);
}

export function render(children: Children): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,11 @@ function SchemaItem({
</div>
));

const renderDefaultValue = guard(
typeof defaultValue === "boolean" ? defaultValue.toString() : defaultValue,
(value) => (
<div className="">
<ReactMarkdown children={`**Default value:** \`${value}\``} />
</div>
)
);
const renderDefaultValue = guard(defaultValue, (value) => (
<div className="">
<ReactMarkdown children={`**Default value:** \`${value}\``} />
</div>
));

const schemaContent = (
<div>
Expand Down

0 comments on commit cc5067d

Please sign in to comment.