Skip to content

Commit

Permalink
Merge pull request #403 from cmaster11/ignore-description
Browse files Browse the repository at this point in the history
Add the `disableDescription` flag, which can be used to NOT print a description for a schema, even if it has been defined previously.
  • Loading branch information
mrjono1 authored Dec 11, 2023
2 parents 1a9476b + e4e8a49 commit 1ee278f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 6 deletions.
28 changes: 28 additions & 0 deletions src/__tests__/description/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@ describe('description', () => {
* Do not modify this file manually
*/
export interface DisableDescription {
/**
* thing
*/
thing: string;
}
/**
* DisableDescriptionObject
*/
export interface DisableDescriptionObject {
/**
* withDescription
*/
withDescription?: {
/**
* A simple description
*/
[x: string]: Example;
};
/**
* withoutDescription
*/
withoutDescription?: {
[x: string]: Example;
};
}
/**
* A simple description
*/
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/description/schemas/OneSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ And more here!
export const noCommentSchema = Joi.object({
more: Joi.string().required()
}).meta({ className: 'NoComment' });

export const disableDescriptionSchema = exampleSchema.meta({
className: 'DisableDescription',
disableDescription: true
});
export const disableDescriptionObjectSchema = Joi.object({
withDescription: Joi.object().pattern(Joi.string(), exampleSchema).meta({ unknownType: exampleSchema }),
withoutDescription: Joi.object()
.pattern(Joi.string(), exampleSchema)
.meta({ unknownType: exampleSchema.meta({ disableDescription: true }) })
}).meta({ className: 'DisableDescriptionObject' });
10 changes: 10 additions & 0 deletions src/joiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export function getIsReadonly(details: Describe): boolean | undefined {
return undefined;
}

export function getDisableDescription(details: Describe): boolean | undefined {
const disableDescriptionItems = getMetadataFromDetails('disableDescription', details);
if (disableDescriptionItems.length !== 0) {
const disableDescription = disableDescriptionItems.pop();
return Boolean(disableDescription);
}

return undefined;
}

/**
* Get the interface name from the Joi
* @returns a string if it can find one
Expand Down
17 changes: 15 additions & 2 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import {
ObjectDescribe,
StringDescribe
} from './joiDescribeTypes';
import { getAllowValues, getInterfaceOrTypeName, getIsReadonly, getMetadataFromDetails } from './joiUtils';
import {
getAllowValues,
getDisableDescription,
getInterfaceOrTypeName,
getIsReadonly,
getMetadataFromDetails
} from './joiUtils';
import { getIndentStr, getJsDocString } from './write';

// see __tests__/joiTypes.ts for more information
Expand All @@ -30,6 +36,7 @@ function getCommonDetails(
const value = details.flags?.default;
const example = details.examples?.[0];
const isReadonly = getIsReadonly(details);
const disableJsDoc = getDisableDescription(details);

let required;
if (
Expand All @@ -42,7 +49,13 @@ function getCommonDetails(
} else {
required = settings.defaultToRequired;
}
return { interfaceOrTypeName, jsDoc: { description, example }, required, value, isReadonly };
return {
interfaceOrTypeName,
jsDoc: { description, example, disable: disableJsDoc },
required,
value,
isReadonly
};
}

export function getAllCustomTypes(parsedSchema: TypeContent): string[] {
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,8 @@ export interface JsDoc {
* @example example value
*/
example?: string;
/**
* If true, completely disables printing JsDoc
*/
disable?: boolean;
}
14 changes: 10 additions & 4 deletions src/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { writeFileSync } from 'fs';
import Path from 'path';

import { Settings, JsDoc } from './types';
import { JsDoc, Settings } from './types';

/**
* Write index.ts file
Expand All @@ -23,9 +23,11 @@ export function writeIndexFile(settings: Settings, fileNamesToExport: string[]):
}

export function getTypeFileNameFromSchema(schemaFileName: string, settings: Settings): string {
return (schemaFileName.endsWith(`${settings.schemaFileSuffix}.ts`)
? schemaFileName.substring(0, schemaFileName.length - `${settings.schemaFileSuffix}.ts`.length)
: schemaFileName.replace('.ts', '')) + settings.interfaceFileSuffix;
return (
(schemaFileName.endsWith(`${settings.schemaFileSuffix}.ts`)
? schemaFileName.substring(0, schemaFileName.length - `${settings.schemaFileSuffix}.ts`.length)
: schemaFileName.replace('.ts', '')) + settings.interfaceFileSuffix
);
}

/**
Expand All @@ -41,6 +43,10 @@ export function getIndentStr(settings: Settings, indentLevel: number): string {
* Get Interface jsDoc
*/
export function getJsDocString(settings: Settings, name: string, jsDoc?: JsDoc, indentLevel = 0): string {
if (jsDoc?.disable == true) {
return '';
}

if (!settings.commentEverything && !jsDoc?.description && !jsDoc?.example) {
return '';
}
Expand Down

0 comments on commit 1ee278f

Please sign in to comment.