Skip to content

Commit

Permalink
fix: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgenii Fedoseev committed Apr 8, 2024
1 parent 0749a24 commit 787f338
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/__snapshots__/combiners/allOf.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Base 200 response
|
**Type:** string
<span style="color:gray;">Default</span>: \`a\`
<span style="color:gray;">Default:</span> \`a\`
||
||
Expand Down
4 changes: 2 additions & 2 deletions src/__snapshots__/combiners/complex.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Generated server url
|
**Type:** string
<span style="color:gray;">Default</span>: \`b\`
<span style="color:gray;">Default:</span> \`b\`
||
||
Expand Down Expand Up @@ -196,7 +196,7 @@ Base 200 response
|
**Type:** string
<span style="color:gray;">Default</span>: \`b\`
<span style="color:gray;">Default:</span> \`b\`
||
||
Expand Down
6 changes: 3 additions & 3 deletions src/__snapshots__/length.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Cat class
|
**Type:** string
<span style="color:gray;">Min length</span>: \`3\`
<span style="color:gray;">Min length:</span> \`3\`
|||#
</div>
Expand All @@ -146,15 +146,15 @@ Dog class
**Type:** string
Pet name
<br><span style="color:gray;">Max length</span>: \`100\`
<br><span style="color:gray;">Max length:</span> \`100\`
||
||
bar
|
**Type:** string
<span style="color:gray;">Min length</span>: \`1\`<br><span style="color:gray;">Max length</span>: \`99\`
<span style="color:gray;">Min length:</span> \`1\`<br><span style="color:gray;">Max length:</span> \`99\`
|||#
</div>
Expand Down
98 changes: 34 additions & 64 deletions src/includer/traverse/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,87 +187,57 @@ function prepareComplexDescription(baseDescription: string, value: OpenJSONSchem

const enumValues = value.enum?.map((s) => `\`${s}\``).join(', ');

if (typeof enumValues !== 'undefined') {
description = concatNewLine(
description,
`<span style="color:gray;">Enum</span>: ${enumValues}`,
);
}
description = concatConstraint(description, enumValues, 'Enum:', false);

if (typeof value.default !== 'undefined') {
description = concatNewLine(
description,
`<span style="color:gray;">Default</span>: \`${value.default}\``,
);
}
description = concatConstraint(description, value.default, 'Default:');

if (typeof value.example !== 'undefined') {
description = concatNewLine(
description,
`<span style="color:gray;">Example</span>: \`${value.example}\``,
);
}
description = concatConstraint(description, value.example, 'Example:');

if (typeof value.minLength !== 'undefined') {
description = concatNewLine(
description,
`<span style="color:gray;">Min length</span>: \`${value.minLength}\``,
);
}
description = concatConstraint(description, value.minLength, 'Min length:');

if (typeof value.maxLength !== 'undefined') {
description = concatNewLine(
description,
`<span style="color:gray;">Max length</span>: \`${value.maxLength}\``,
);
}
description = concatConstraint(description, value.maxLength, 'Max length:');

if (typeof value.maxItems !== 'undefined') {
description = concatNewLine(
description,
`<span style="color:gray;">Max items</span>: \`${value.maxItems}\``,
);
}
description = concatConstraint(description, value.maxItems, 'Max items:');

if (typeof value.minItems !== 'undefined') {
description = concatNewLine(
description,
`<span style="color:gray;">Min items</span>: \`${value.minItems}\``,
);
}
description = concatConstraint(description, value.minItems, 'Min items:');

if (typeof value.pattern !== 'undefined') {
description = concatNewLine(
description,
`<span style="color:gray;">Pattern</span>: \`${value.pattern}\``,
);
}
description = concatConstraint(description, value.pattern, 'Pattern:');

if (typeof value.uniqueItems !== 'undefined') {
description = concatNewLine(description, `<span style="color:gray;">Unique items</span>`);
}
description = concatConstraint(description, value.uniqueItems, 'Unique items');

if (typeof value.minimum !== 'undefined') {
description = concatNewLine(
description,
`<span style="color:gray;">Min value${
value.exclusiveMinimum ? ' (exclusive)' : ''
}</span>: \`${value.minimum}\``,
);
}
description = concatConstraint(description, value.minimum,

Check failure on line 208 in src/includer/traverse/tables.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `description,` with `⏎········description,⏎·······`
`Min value${

Check failure on line 209 in src/includer/traverse/tables.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `⏎············value.exclusiveMinimum·?·'·(exclusive)'·:·''⏎········` with `value.exclusiveMinimum·?·'·(exclusive)'·:·''`
value.exclusiveMinimum ? ' (exclusive)' : ''
}: `,
);

description = concatConstraint(description, value.maximum,

Check failure on line 214 in src/includer/traverse/tables.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `description,` with `⏎········description,⏎·······`
`Max value${

Check failure on line 215 in src/includer/traverse/tables.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `⏎············value.exclusiveMaximum·?·'·(exclusive)'·:·''⏎········` with `value.exclusiveMaximum·?·'·(exclusive)'·:·''`
value.exclusiveMaximum ? ' (exclusive)' : ''
}: `,
);

return description;
}

if (typeof value.maximum !== 'undefined') {
description = concatNewLine(
function concatConstraint(description: string, constraint: unknown, constraintLabel: string, wrapValueIntoCode = true) {

Check failure on line 223 in src/includer/traverse/tables.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `description:·string,·constraint:·unknown,·constraintLabel:·string,·wrapValueIntoCode·=·true` with `⏎····description:·string,⏎····constraint:·unknown,⏎····constraintLabel:·string,⏎····wrapValueIntoCode·=·true,⏎`
if (typeof constraint !== 'undefined') {
return concatNewLine(
description,
`<span style="color:gray;">Max value${
value.exclusiveMaximum ? ' (exclusive)' : ''
}</span>: \`${value.maximum}\``,
`<span style="color:gray;">${constraintLabel}</span> ${prepareConstraintValue(constraint, wrapValueIntoCode)}`,

Check failure on line 227 in src/includer/traverse/tables.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `constraint,·wrapValueIntoCode` with `⏎················constraint,⏎················wrapValueIntoCode,⏎············`
);
}

return description;
}

function prepareConstraintValue(value: unknown, wrapValueIntoCode: boolean) {
if (typeof value === 'boolean') {
return '';
}
return wrapValueIntoCode ? `\`${value}\`` : value;
}

function findNonNullOneOfElement(schema: OpenJSONSchema): OpenJSONSchema {
const isValid = (v: OpenJSONSchema) => {
if (typeof inferType(v) === 'string') {
Expand Down

0 comments on commit 787f338

Please sign in to comment.