Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the disableDescription flag, which can be used to NOT print a description for a schema, even if it has been defined previously. #403

Merged
merged 6 commits into from
Dec 11, 2023
34 changes: 34 additions & 0 deletions src/__tests__/description/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,40 @@ export interface ExampleLong {
another: string;
}

/**
* IgnoreDescription
*/
export interface IgnoreDescription {
/**
* thing
*/
thing: string;
}

/**
* IgnoreDescriptionObject
*/
export interface IgnoreDescriptionObject {
/**
* withDescription
*/
withDescription?: {
/**
* A simple description
*/
[x: string]: Example;
};
/**
* withoutDescription
*/
withoutDescription?: {
/**
* [x: string]
*/
[x: string]: Example;
};
}

/**
* NoComment
*/
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/description/schemas/OneSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ And more here!
export const noCommentSchema = Joi.object({
more: Joi.string().required()
}).meta({ className: 'NoComment' });

export const ignoreDescriptionSchema = exampleSchema.meta({ className: 'IgnoreDescription', ignoreDescription: true });
export const ignoreDescriptionObjectSchema = Joi.object({
withDescription: Joi.object().pattern(Joi.string(), exampleSchema).meta({ unknownType: exampleSchema }),
withoutDescription: Joi.object()
.pattern(Joi.string(), exampleSchema)
.meta({ unknownType: exampleSchema.meta({ ignoreDescription: true }) })
}).meta({ className: 'IgnoreDescriptionObject' });
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 getIgnoreDescription(details: Describe): boolean | undefined {
const ignoreDescriptionItems = getMetadataFromDetails('ignoreDescription', details);
if (ignoreDescriptionItems.length !== 0) {
const ignoreDescription = ignoreDescriptionItems.pop();
return Boolean(ignoreDescription);
}

return undefined;
}

/**
* Get the interface name from the Joi
* @returns a string if it can find one
Expand Down
11 changes: 9 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,
getIgnoreDescription,
getInterfaceOrTypeName,
getIsReadonly,
getMetadataFromDetails
} from './joiUtils';
import { getIndentStr, getJsDocString } from './write';

// see __tests__/joiTypes.ts for more information
Expand All @@ -25,7 +31,8 @@ function getCommonDetails(
): { interfaceOrTypeName?: string; jsDoc: JsDoc; required: boolean; value?: unknown; isReadonly?: boolean } {
const interfaceOrTypeName = getInterfaceOrTypeName(settings, details);

const description = details.flags?.description;
const ignoreDescription = getIgnoreDescription(details);
const description = ignoreDescription ? undefined : details.flags?.description;
const presence = details.flags?.presence;
const value = details.flags?.default;
const example = details.examples?.[0];
Expand Down