From aaf5ce5aea4711da321a5569f2465a5d5ca19e62 Mon Sep 17 00:00:00 2001 From: Evgenii Fedoseev Date: Mon, 8 Apr 2024 18:30:38 +0300 Subject: [PATCH] fix: refactoring --- .../combiners/allOf.test.ts.snap | 2 +- .../combiners/complex.test.ts.snap | 4 +- src/__snapshots__/length.test.ts.snap | 6 +- src/includer/traverse/tables.ts | 106 +++++++----------- 4 files changed, 48 insertions(+), 70 deletions(-) diff --git a/src/__snapshots__/combiners/allOf.test.ts.snap b/src/__snapshots__/combiners/allOf.test.ts.snap index f18df07..43ee2b2 100644 --- a/src/__snapshots__/combiners/allOf.test.ts.snap +++ b/src/__snapshots__/combiners/allOf.test.ts.snap @@ -119,7 +119,7 @@ Base 200 response | **Type:** string -Default: \`a\` +Default: \`a\` || || diff --git a/src/__snapshots__/combiners/complex.test.ts.snap b/src/__snapshots__/combiners/complex.test.ts.snap index f89c0cc..c6d9017 100644 --- a/src/__snapshots__/combiners/complex.test.ts.snap +++ b/src/__snapshots__/combiners/complex.test.ts.snap @@ -61,7 +61,7 @@ Generated server url | **Type:** string -Default: \`b\` +Default: \`b\` || || @@ -196,7 +196,7 @@ Base 200 response | **Type:** string -Default: \`b\` +Default: \`b\` || || diff --git a/src/__snapshots__/length.test.ts.snap b/src/__snapshots__/length.test.ts.snap index d5ff1ed..06d5045 100644 --- a/src/__snapshots__/length.test.ts.snap +++ b/src/__snapshots__/length.test.ts.snap @@ -123,7 +123,7 @@ Cat class | **Type:** string -Min length: \`3\` +Min length: \`3\` |||# @@ -146,7 +146,7 @@ Dog class **Type:** string Pet name -
Max length: \`100\` +
Max length: \`100\` || || @@ -154,7 +154,7 @@ Pet name | **Type:** string -Min length: \`1\`
Max length: \`99\` +Min length: \`1\`
Max length: \`99\` |||# diff --git a/src/includer/traverse/tables.ts b/src/includer/traverse/tables.ts index d63fc8d..0b29888 100644 --- a/src/includer/traverse/tables.ts +++ b/src/includer/traverse/tables.ts @@ -187,87 +187,65 @@ function prepareComplexDescription(baseDescription: string, value: OpenJSONSchem const enumValues = value.enum?.map((s) => `\`${s}\``).join(', '); - if (typeof enumValues !== 'undefined') { - description = concatNewLine( - description, - `Enum: ${enumValues}`, - ); - } + description = concatConstraint(description, enumValues, 'Enum:', false); - if (typeof value.default !== 'undefined') { - description = concatNewLine( - description, - `Default: \`${value.default}\``, - ); - } + description = concatConstraint(description, value.default, 'Default:'); - if (typeof value.example !== 'undefined') { - description = concatNewLine( - description, - `Example: \`${value.example}\``, - ); - } + description = concatConstraint(description, value.example, 'Example:'); - if (typeof value.minLength !== 'undefined') { - description = concatNewLine( - description, - `Min length: \`${value.minLength}\``, - ); - } + description = concatConstraint(description, value.minLength, 'Min length:'); - if (typeof value.maxLength !== 'undefined') { - description = concatNewLine( - description, - `Max length: \`${value.maxLength}\``, - ); - } + description = concatConstraint(description, value.maxLength, 'Max length:'); - if (typeof value.maxItems !== 'undefined') { - description = concatNewLine( - description, - `Max items: \`${value.maxItems}\``, - ); - } + description = concatConstraint(description, value.maxItems, 'Max items:'); - if (typeof value.minItems !== 'undefined') { - description = concatNewLine( - description, - `Min items: \`${value.minItems}\``, - ); - } + description = concatConstraint(description, value.minItems, 'Min items:'); - if (typeof value.pattern !== 'undefined') { - description = concatNewLine( - description, - `Pattern: \`${value.pattern}\``, - ); - } + description = concatConstraint(description, value.pattern, 'Pattern:'); - if (typeof value.uniqueItems !== 'undefined') { - description = concatNewLine(description, `Unique items`); - } + description = concatConstraint(description, value.uniqueItems, 'Unique items'); - if (typeof value.minimum !== 'undefined') { - description = concatNewLine( - description, - `Min value${ - value.exclusiveMinimum ? ' (exclusive)' : '' - }: \`${value.minimum}\``, - ); - } + description = concatConstraint( + description, + value.minimum, + `Min value${value.exclusiveMinimum ? ' (exclusive)' : ''}: `, + ); + + description = concatConstraint( + description, + value.maximum, + `Max value${value.exclusiveMaximum ? ' (exclusive)' : ''}: `, + ); + + return description; +} - if (typeof value.maximum !== 'undefined') { - description = concatNewLine( +function concatConstraint( + description: string, + constraint: unknown, + constraintLabel: string, + wrapValueIntoCode = true, +) { + if (typeof constraint !== 'undefined') { + return concatNewLine( description, - `Max value${ - value.exclusiveMaximum ? ' (exclusive)' : '' - }: \`${value.maximum}\``, + `${constraintLabel} ${prepareConstraintValue( + 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') {