Skip to content

Commit 4f23f92

Browse files
committed
fix(guards): array nested in additionalProperties
1 parent 1064b93 commit 4f23f92

File tree

7 files changed

+68
-8
lines changed

7 files changed

+68
-8
lines changed

src/helper.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,14 @@ export function toTypescriptType(type: string | undefined): string {
9090
return typeName(type);
9191
}
9292

93+
const PARENT_PROP_PLACEHOLDER = '<-';
94+
9395
export function accessProp(arg: string): string {
94-
return arg.startsWith("'") ? `arg[${arg}]` : `arg.${arg}`;
96+
return arg.startsWith(PARENT_PROP_PLACEHOLDER)
97+
? arg.replace(PARENT_PROP_PLACEHOLDER, '')
98+
: arg.startsWith("'")
99+
? `arg[${arg}]`
100+
: `arg.${arg}`;
95101
}
96102

97103
function guardArray(prop: Property): string {
@@ -121,10 +127,15 @@ export function guardFn(fn: () => string, prop: Property): string {
121127
}
122128
${
123129
prop.name === ADDITIONAL_PROPERTIES_KEY
124-
? `Object.values(arg).every((item: unknown) => ${
125-
prop.isPrimitiveType
126-
? `typeof item === '${prop.type}'`
127-
: `is${prop.typescriptType}(item)`
130+
? `Object.values(arg).every((value: unknown) => ${
131+
prop.isArray
132+
? guardArray({
133+
...prop,
134+
name: `${PARENT_PROP_PLACEHOLDER}value`,
135+
})
136+
: prop.isPrimitiveType
137+
? `typeof value === '${prop.type}'`
138+
: `is${prop.typescriptType}(value)`
128139
})`
129140
: prop.isArray
130141
? guardArray(prop)

tests/custom/api/guards/index.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export function isDictionary(arg: any): arg is models.Dictionary {
9999
arg != null &&
100100
typeof arg === 'object' &&
101101
// [key: string]: DictionaryItem
102-
( Object.values(arg).every((item: unknown) => isDictionaryItem(item)) ) &&
102+
( Object.values(arg).every((value: unknown) => isDictionaryItem(value)) ) &&
103103

104104
true
105105
);
@@ -110,7 +110,7 @@ export function isDictionaryItem(arg: any): arg is models.DictionaryItem {
110110
arg != null &&
111111
typeof arg === 'object' &&
112112
// [key: string]: number
113-
( Object.values(arg).every((item: unknown) => typeof item === 'number') ) &&
113+
( Object.values(arg).every((value: unknown) => typeof value === 'number') ) &&
114114

115115
true
116116
);
@@ -200,6 +200,28 @@ export function isMyInterface(arg: any): arg is models.MyInterface {
200200
);
201201
}
202202

203+
export function isPageData(arg: any): arg is models.PageData {
204+
return (
205+
arg != null &&
206+
typeof arg === 'object' &&
207+
// appliedIf?: PageDataRules
208+
( typeof arg.appliedIf === 'undefined' || isPageDataRules(arg.appliedIf) ) &&
209+
210+
true
211+
);
212+
}
213+
214+
export function isPageDataRules(arg: any): arg is models.PageDataRules {
215+
return (
216+
arg != null &&
217+
typeof arg === 'object' &&
218+
// [key: string]: string[]
219+
( Object.values(arg).every((value: unknown) => (Array.isArray(value) && value.every((item: unknown) => typeof item === 'string'))) ) &&
220+
221+
true
222+
);
223+
}
224+
203225
export function isPet(arg: any): arg is models.Pet {
204226
return (
205227
arg != null &&

tests/custom/api/models/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export { ItemModelList } from './item-model-list.model';
1212
export { Model } from './model.model';
1313
export { Mouse } from './mouse.model';
1414
export { MyInterface } from './my-interface.model';
15+
export { PageData } from './page-data.model';
16+
export { PageDataRules } from './page-data-rules.model';
1517
export { Pet } from './pet.model';
1618
export { Possition } from './possition.enum';
1719
export { Right } from './right.enum';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* tslint:disable */
2+
3+
export interface PageDataRules {
4+
[key: string]: string[];
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* tslint:disable */
2+
import {
3+
PageDataRules,
4+
} from '.';
5+
6+
export interface PageData {
7+
appliedIf?: PageDataRules;
8+
}

tests/custom/swagger.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,15 @@ definitions:
342342
type: object
343343
additionalProperties:
344344
type: number
345+
346+
PageDataRules:
347+
type: object
348+
additionalProperties:
349+
type: array
350+
items:
351+
type: string
352+
PageData:
353+
type: object
354+
properties:
355+
appliedIf:
356+
$ref: '#/definitions/PageDataRules'

tests/github/api/guards/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ export function isLanguages(arg: any): arg is models.Languages {
922922
arg != null &&
923923
typeof arg === 'object' &&
924924
// [key: string]: number
925-
( Object.values(arg).every((item: unknown) => typeof item === 'number') ) &&
925+
( Object.values(arg).every((value: unknown) => typeof value === 'number') ) &&
926926

927927
true
928928
);

0 commit comments

Comments
 (0)