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..23bbc74 100644 --- a/src/includer/traverse/tables.ts +++ b/src/includer/traverse/tables.ts @@ -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, - `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') {