diff --git a/src/analyseSchemaFile.ts b/src/analyseSchemaFile.ts index aa9bf5d2..edee10e8 100644 --- a/src/analyseSchemaFile.ts +++ b/src/analyseSchemaFile.ts @@ -11,12 +11,17 @@ export function convertSchemaInternal( exportedName?: string ): ConvertedType | undefined { const details = joi.describe() as Describe; - const name = details?.flags?.label || exportedName; + const defaultName = details?.flags?.label || exportedName; - if (!name) { + if (!defaultName) { throw new Error(`At least one "object" does not have a .label(). Details: ${JSON.stringify(details)}`); } + const name = settings.mapTypeName(defaultName); + if (!name) { + throw new Error(`Resulting type name cannot be empty. Default name was: ${defaultName}`); + } + if (settings.debug && name.toLowerCase().endsWith('schema')) { console.debug( `It is recommended you update the Joi Schema '${name}' similar to: ${name} = Joi.object().label('${name.replace( diff --git a/src/index.ts b/src/index.ts index 895da709..22714398 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,7 +28,9 @@ function defaultSettings(settings: Partial): Settings { sortPropertiesByName: true, commentEverything: false, ignoreFiles: [], - indentationChacters: ' ' + indentationChacters: ' ', + mapTypeFileName: (name: string) => name, + mapTypeName: (name: string) => name }, settings ) as Settings; @@ -46,9 +48,11 @@ export function convertSchema( } export function getTypeFileNameFromSchema(schemaFileName: string, settings: Settings): string { - return schemaFileName.endsWith(`${settings.schemaFileSuffix}.ts`) + return settings.mapTypeFileName( + schemaFileName.endsWith(`${settings.schemaFileSuffix}.ts`) ? schemaFileName.substring(0, schemaFileName.length - `${settings.schemaFileSuffix}.ts`.length) - : schemaFileName.replace('.ts', ''); + : schemaFileName.replace('.ts', '') + ); } /** diff --git a/src/types.ts b/src/types.ts index 23c9ee04..10428ac9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -65,6 +65,18 @@ export interface Settings { * @default ' ' (two spaces) */ readonly indentationChacters: string; + + /** + * Can be used to customize the name of the generated file. + * @default (name: string) => name + */ + readonly mapTypeFileName: (name: string) => string; + + /** + * Can be used to customize resulting type name. + * @default (name: string) => name + */ + readonly mapTypeName: (name: string) => string; } export interface ConvertedType {