Skip to content

Commit 25df534

Browse files
edbznvmasek
authored andcommitted
fix: correct isRequired guard + use named parameters
1 parent 8873d16 commit 25df534

File tree

2 files changed

+51
-34
lines changed

2 files changed

+51
-34
lines changed

src/helper.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,19 @@ export function accessProp(name: string): string {
8686
return name.startsWith("'") ? `arg[${name}]` : `arg.${name}`;
8787
}
8888

89-
export function guardOptional(
90-
name: string,
91-
isRequired: boolean | undefined,
92-
nullable: boolean | undefined,
93-
guard: (name: string) => string,
94-
): string {
89+
export function guardOptional({
90+
name,
91+
isRequired = false,
92+
nullable = false,
93+
guard,
94+
}: {
95+
name: string;
96+
isRequired?: boolean;
97+
nullable?: boolean;
98+
guard: (name: string) => string;
99+
}): string {
95100
const guards = [
96-
...(isRequired ? [`typeof ${name} === 'undefined'`] : []),
101+
...(isRequired ? [] : [`typeof ${name} === 'undefined'`]),
97102
...(nullable ? [`${name} === null`] : []),
98103
guard(name),
99104
];

src/parser.ts

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -403,16 +403,16 @@ function parseInterfaceProperties(
403403
const propGuard = global.GLOBAL_OPTIONS.skipGuards
404404
? undefined
405405
: propertyAllOf.length
406-
? guardOptional(
407-
accessProp(name),
408-
false,
406+
? guardOptional({
407+
name: accessProp(name),
408+
isRequired: false,
409409
nullable,
410-
() =>
410+
guard: () =>
411411
`( ${propertyAllOf
412412
.map(({ guard }) => guard?.('this can be anything'))
413413
.filter((type): type is string => !!type)
414414
.join(' && ')} )`,
415-
)
415+
})
416416
: parsedSchema.guard?.('this can be anything') || ''; // todo: check the typing on the guards as the name probably should not be passed here (it does not seem to have any effect)
417417

418418
return {
@@ -453,13 +453,13 @@ function parseSchema(
453453
guard: global.GLOBAL_OPTIONS.skipGuards
454454
? undefined
455455
: () =>
456-
guardOptional(
456+
guardOptional({
457457
name,
458458
isRequired,
459459
nullable,
460-
(iterName: string) =>
460+
guard: (iterName: string) =>
461461
`[${enumValues.join(', ')}].includes(${iterName})`,
462-
),
462+
}),
463463
};
464464
}
465465

@@ -470,12 +470,12 @@ function parseSchema(
470470
guard: global.GLOBAL_OPTIONS.skipGuards
471471
? undefined
472472
: () =>
473-
guardOptional(
473+
guardOptional({
474474
name,
475475
isRequired,
476476
nullable,
477-
(iterName: string) => `typeof ${iterName} === 'object'`,
478-
),
477+
guard: (iterName: string) => `typeof ${iterName} === 'object'`,
478+
}),
479479
}; // type occurrence of inlined properties as object instead of any (TODO: consider supporting inlined properties)
480480
}
481481

@@ -488,13 +488,13 @@ function parseSchema(
488488
guard: global.GLOBAL_OPTIONS.skipGuards
489489
? undefined
490490
: () =>
491-
guardOptional(
491+
guardOptional({
492492
name,
493493
isRequired,
494494
nullable,
495-
(iterName: string) =>
495+
guard: (iterName: string) =>
496496
`${prefixGuards ? 'guards.' : ''}is${refType}(${iterName})`,
497-
),
497+
}),
498498
};
499499
}
500500

@@ -512,9 +512,13 @@ function parseSchema(
512512
global.GLOBAL_OPTIONS.skipGuards || !parsedArrayItemsSchema.guard
513513
? undefined
514514
: () =>
515-
guardOptional(name, isRequired, nullable, (iterName: string) =>
516-
guardArray(iterName, parsedArrayItemsSchema.guard!),
517-
),
515+
guardOptional({
516+
name,
517+
isRequired,
518+
nullable,
519+
guard: (iterName: string) =>
520+
guardArray(iterName, parsedArrayItemsSchema.guard!),
521+
}),
518522
};
519523
}
520524

@@ -535,11 +539,15 @@ function parseSchema(
535539
global.GLOBAL_OPTIONS.skipGuards || !parsedDictionarySchema.guard
536540
? undefined
537541
: () =>
538-
guardOptional(name, isRequired, nullable, (iterName: string) =>
539-
isJustObject
540-
? `typeof ${iterName} === 'object'`
541-
: guardDictionary(iterName, parsedDictionarySchema.guard!),
542-
),
542+
guardOptional({
543+
name,
544+
isRequired,
545+
nullable,
546+
guard: (iterName: string) =>
547+
isJustObject
548+
? `typeof ${iterName} === 'object'`
549+
: guardDictionary(iterName, parsedDictionarySchema.guard!),
550+
}),
543551
};
544552
}
545553

@@ -554,11 +562,15 @@ function parseSchema(
554562
: () =>
555563
type === 'any'
556564
? ''
557-
: guardOptional(name, isRequired, nullable, (iterName: string) =>
558-
type === 'File'
559-
? `${iterName} instanceof File`
560-
: `typeof ${iterName} === '${type}'`,
561-
),
565+
: guardOptional({
566+
name,
567+
isRequired,
568+
nullable,
569+
guard: (iterName: string) =>
570+
type === 'File'
571+
? `${iterName} instanceof File`
572+
: `typeof ${iterName} === '${type}'`,
573+
}),
562574
};
563575
}
564576

0 commit comments

Comments
 (0)