Skip to content

Commit

Permalink
Hide required labels on response schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Nov 22, 2022
1 parent 57c0c3b commit faeff35
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,25 @@ function resolveAllOf(allOf: SchemaObject[]) {
return { properties, required };
}

interface RowOptions {
showRequiredLabel?: boolean;
}

const defaultRowOptions: RowOptions = { showRequiredLabel: true };

interface RowProps {
name: string;
schema: SchemaObject;
required: boolean;
options?: RowOptions;
}

function createRow({ name, schema, required }: RowProps) {
function createRow({
name,
schema,
required,
options = defaultRowOptions,
}: RowProps) {
return create("tr", {
children: create("td", {
children: [
Expand All @@ -49,7 +61,7 @@ function createRow({ name, schema, required }: RowProps) {
style: { opacity: "0.6" },
children: ` ${getSchemaName(schema, true)}`,
}),
guard(required, () => [
guard(required && options.showRequiredLabel, () => [
create("span", {
style: { opacity: "0.6" },
children: " — ",
Expand All @@ -74,17 +86,21 @@ function createRow({ name, schema, required }: RowProps) {
children: createDescription(description),
})
),
createRows({ schema: schema }),
createRows({ schema: schema, options }),
],
}),
});
}

interface RowsProps {
schema: SchemaObject;
options?: RowOptions;
}

function createRows({ schema }: RowsProps): string | undefined {
function createRows({
schema,
options = defaultRowOptions,
}: RowsProps): string | undefined {
// object
if (schema.properties !== undefined) {
return createFullWidthTable({
Expand All @@ -100,6 +116,7 @@ function createRows({ schema }: RowsProps): string | undefined {
required: Array.isArray(schema.required)
? schema.required.includes(key)
: false,
options,
})
),
}),
Expand All @@ -120,6 +137,7 @@ function createRows({ schema }: RowsProps): string | undefined {
name: key,
schema: val,
required: Array.isArray(required) ? required.includes(key) : false,
options,
})
),
}),
Expand All @@ -128,7 +146,7 @@ function createRows({ schema }: RowsProps): string | undefined {

// array
if (schema.items !== undefined) {
return createRows({ schema: schema.items });
return createRows({ schema: schema.items, options });
}

// primitive
Expand All @@ -137,9 +155,13 @@ function createRows({ schema }: RowsProps): string | undefined {

interface RowsRootProps {
schema: SchemaObject;
options?: RowOptions;
}

function createRowsRoot({ schema }: RowsRootProps) {
function createRowsRoot({
schema,
options = defaultRowOptions,
}: RowsRootProps) {
// object
if (schema.properties !== undefined) {
return Object.entries(schema.properties).map(([key, val]) =>
Expand All @@ -149,6 +171,7 @@ function createRowsRoot({ schema }: RowsRootProps) {
required: Array.isArray(schema.required)
? schema.required.includes(key)
: false,
options,
})
);
}
Expand All @@ -161,6 +184,7 @@ function createRowsRoot({ schema }: RowsRootProps) {
name: key,
schema: val,
required: Array.isArray(required) ? required.includes(key) : false,
options,
})
);
}
Expand All @@ -174,7 +198,7 @@ function createRowsRoot({ schema }: RowsRootProps) {
style: { opacity: "0.6" },
children: ` ${getSchemaName(schema, true)}`,
}),
createRows({ schema: schema.items }),
createRows({ schema: schema.items, options }),
],
}),
});
Expand Down Expand Up @@ -215,9 +239,15 @@ interface Props {
description?: string;
required?: boolean;
};
options?: RowOptions;
}

export function createSchemaTable({ title, body, ...rest }: Props) {
export function createSchemaTable({
title,
body,
options = defaultRowOptions,
...rest
}: Props) {
if (body === undefined || body.content === undefined) {
return undefined;
}
Expand Down Expand Up @@ -249,7 +279,7 @@ export function createSchemaTable({ title, body, ...rest }: Props) {
style: { textAlign: "left" },
children: [
`${title} `,
guard(body.required, () => [
guard(body.required && options.showRequiredLabel, () => [
create("span", {
style: { opacity: "0.6" },
children: " — ",
Expand All @@ -270,7 +300,7 @@ export function createSchemaTable({ title, body, ...rest }: Props) {
}),
}),
create("tbody", {
children: createRowsRoot({ schema: firstBody }),
children: createRowsRoot({ schema: firstBody, options }),
}),
],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export function createStatusCodesTable({ responses }: Props) {
body: {
content: responses[code].content,
},
options: {
showRequiredLabel: false,
},
}),
}),
],
Expand Down

0 comments on commit faeff35

Please sign in to comment.