Skip to content

Commit

Permalink
Merge pull request #376 from cmaster11/default-interface-suffix
Browse files Browse the repository at this point in the history
Introduce the defaultInterfaceSuffix setting
  • Loading branch information
mrjono1 authored Oct 18, 2023
2 parents 6011c9e + 028ea7a commit 5f0ec59
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ export interface Settings {
* Use .label('InterfaceName') instead of .meta({className:'InterfaceName'}) for interface names
*/
useLabelAsInterfaceName: boolean;
/**
* If defined, when a schema name ends with "schema", replaces the ending in the generated type by default
* with this string.
* E.g. when this setting is "Interface", a `TestSchema` object generates a `TestInterface` type
*/
defaultInterfaceSuffix?: string;
/**
* Should interface properties be defaulted to optional or required
* @default false
Expand Down
43 changes: 43 additions & 0 deletions src/__tests__/defaultInterfaceSuffix/defaultInterfaceSuffix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { existsSync, readFileSync, rmdirSync } from 'fs';

import { convertFromDirectory } from '../../index';

const typeOutputDirectory = './src/__tests__/defaultInterfaceSuffix/interfaces';

describe('Create interfaces from schema files and applies a default interface suffix', () => {
beforeAll(() => {
if (existsSync(typeOutputDirectory)) {
rmdirSync(typeOutputDirectory, { recursive: true });
}
});

test('generates interfaces', async () => {
const result = await convertFromDirectory({
schemaDirectory: './src/__tests__/defaultInterfaceSuffix/schemas',
typeOutputDirectory,
defaultInterfaceSuffix: 'Interface'
});

expect(result).toBe(true);

const oneContent = readFileSync(`${typeOutputDirectory}/One.ts`).toString();
expect(oneContent).toBe(
`/**
* This file was automatically generated by joi-to-typescript
* Do not modify this file manually
*/
/**
* a test schema definition
*/
export interface TestInterface {
name?: string;
}
export interface TestWithMetaInterface {
name?: string;
}
`
);
});
});
11 changes: 11 additions & 0 deletions src/__tests__/defaultInterfaceSuffix/schemas/OneSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Joi from 'joi';

export const TestSchema = Joi.object({
name: Joi.string().optional()
}).description('a test schema definition');

export const TestWithMetaSchema = Joi.object({
name: Joi.string().optional()
}).meta({
myMeta: 'Hello'
});
15 changes: 10 additions & 5 deletions src/joiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ export function ensureInterfaceorTypeName(settings: Settings, details: Describe,
details.flags.label = interfaceOrTypeName;
}
} else {
// Set the meta[].className from the exportedName if missing
if (!details.metas || details.metas.length === 0) {
details.metas = [{ className: interfaceOrTypeName }];
} else {
const className = details.metas.find(meta => meta.className)?.className;
details.metas = [];
}

if (!className) {
const className = details.metas.find(meta => meta.className)?.className;

// Set the meta[].className from the exportedName if missing
if (!className) {
if (settings.defaultInterfaceSuffix && interfaceOrTypeName.toLowerCase().endsWith('schema')) {
const nameWithNewSuffix = interfaceOrTypeName.slice(0, -6) + settings.defaultInterfaceSuffix;
details.metas.push({ className: nameWithNewSuffix });
} else {
details.metas.push({ className: interfaceOrTypeName });
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export interface Settings {
* Use .label('InterfaceName') instead of .meta({className:'InterfaceName'}) for interface names
*/
readonly useLabelAsInterfaceName: boolean;
/**
* If defined, when a schema name ends with "schema", replaces the ending in the generated type by default
* with this string.
* E.g. when this setting is "Interface", a `TestSchema` object generates a `TestInterface` type
*/
readonly defaultInterfaceSuffix?: string;
/**
* Should interface properties be defaulted to optional or required
* @default false
Expand Down

0 comments on commit 5f0ec59

Please sign in to comment.