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

Wraps function types in additional brackets to prevent broken union types #400

Merged
merged 2 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/__tests__/alternatives/alternatives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ describe('alternative types', () => {
* Do not modify this file manually
*/

export type AlternativesWithFunctionInterface = ((...args: any[]) => any) | {
json: any;
} | {
raw: string;
};

/**
* a description for basic
*/
Expand Down Expand Up @@ -73,7 +79,8 @@ export interface Thing {
});

test('allowed value in alternatives', () => {
const schema = Joi.alternatives(Joi.string(), Joi.number()).allow(null)
const schema = Joi.alternatives(Joi.string(), Joi.number())
.allow(null)
.meta({ className: 'Test' })
.description('Test allowed values in alternatives');

Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/alternatives/schemas/OneSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ export const AlternativesConditionalSchema = Joi.object({
otherwise: Joi.forbidden()
})
}).meta({ className: 'SomeSchema' });

export const AlternativesWithFunctionSchema = Joi.alternatives([
Joi.function().minArity(2),
Joi.object({
json: Joi.any().required()
}),
Joi.object({
raw: Joi.string().required()
})
]).meta({ className: 'AlternativesWithFunctionInterface' });
6 changes: 3 additions & 3 deletions src/__tests__/joiTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ describe('`Joi.types()`', () => {
expect(result?.content).toBe(
[
'export interface Test {',
' doStuff?: (...args: any[]) => any;',
' moreThings?: (...args: any[]) => any;',
' stuff: (...args: any[]) => any;',
' doStuff?: ((...args: any[]) => any);',
' moreThings?: ((...args: any[]) => any);',
' stuff: ((...args: any[]) => any);',
'}'
].join('\n')
);
Expand Down
2 changes: 1 addition & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export function parseSchema(
if (typeToUse === undefined) {
switch (details.type as string) {
case 'function':
typeToUse = '(...args: any[]) => any';
typeToUse = '((...args: any[]) => any)';
break;

case 'symbol':
Expand Down