@@ -30,6 +30,8 @@ import {
3030 compareStringByKey ,
3131 isReference ,
3232 ADDITIONAL_PROPERTIES_KEY ,
33+ accessProp ,
34+ guardFn ,
3335} from './helper' ;
3436
3537interface Parameters {
@@ -325,7 +327,7 @@ function parseInterfaceProperties(
325327) : Property [ ] {
326328 return Object . entries < Schema > ( properties )
327329 . map ( ( [ propName , propSchema ] : [ string , Schema ] ) => {
328- const isArray = / ^ a r r a y $ / i . test ( propSchema . type || '' ) ;
330+ const isArray = propSchema . type === 'array' ;
329331 const ref =
330332 propSchema . additionalProperties &&
331333 typeof propSchema . additionalProperties !== 'boolean'
@@ -364,86 +366,48 @@ function parseInterfaceProperties(
364366 ) ;
365367 const allOfImports = propertyAllOf . map ( prop => `${ prop . typescriptType } ` ) ;
366368 const type = typescriptType . replace ( '[]' , '' ) ;
367- const isPrimitiveType = BASIC_TS_TYPE_REGEX . test ( type ) ;
368369 const name =
369370 / ^ [ A - Z a - z _ $ ] [ \w $ ] * $ / . test ( propName ) ||
370371 propName === ADDITIONAL_PROPERTIES_KEY
371372 ? propName
372373 : `'${ propName } '` ;
373- const isRequired = requiredProps . includes ( propName ) ;
374-
375- const accessProp = ( arg : string ) : string =>
376- arg . startsWith ( "'" ) ? `arg[${ arg } ]` : `arg.${ arg } ` ;
377-
378- const guardFn = ( fn : ( ) => string , prop : Property = { } ) => `
379- ${
380- prop . hasOwnProperty ( 'isRequired' ) || isRequired
381- ? ''
382- : `typeof ${ accessProp ( name ) } === 'undefined' ||`
383- }
384- ${
385- prop . isArray || isArray
386- ? `(Array.isArray(${ accessProp ( name ) } ) && ${ accessProp (
387- name ,
388- ) } .every((item: unknown) => ${
389- prop . isPrimitiveType || isPrimitiveType
390- ? `typeof item === '${ prop . type || type } '`
391- : ( prop . typescriptType || typescriptType ) . endsWith ( '[]' ) // checks if item is nested array type
392- ? `(Array.isArray(item) && item.every((itemItem: unknown) => ${ ( prop . isPrimitiveType ||
393- isPrimitiveType
394- ? `typeof itemItem === '${ prop . type || type } '`
395- : `is${ prop . typescriptType || typescriptType } (itemItem)`
396- ) . replace ( '[]' , '' ) } ))`
397- : `is${ prop . typescriptType || typescriptType } (item)`
398- } ))`
399- : name === ADDITIONAL_PROPERTIES_KEY
400- ? `Object.values(arg).every((item: unknown) => ${
401- isPrimitiveType
402- ? `typeof item === '${ type } '`
403- : `is${ typescriptType } (item)`
404- } )`
405- : `${
406- prop . isPrimitiveType || isPrimitiveType
407- ? `typeof ${ accessProp ( prop . name || name ) } === '${ prop . type ||
408- type } '`
409- : fn ( )
410- } `
411- }
412- ` ;
413-
414- const guard = `(${ guardFn ( ( ) =>
415- propertyAllOf . length
416- ? `(${ propertyAllOf
417- . map ( prop =>
418- guardFn (
419- ( ) => `is${ prop . typescriptType } (${ accessProp ( name ) } )` ,
420- prop ,
421- ) ,
422- )
423- . join ( ' && ' ) } )`
424- : 'enum' in propSchema
425- ? `[${ ( typescriptType || '' ) . replace (
426- / \| / g,
427- ', ' ,
428- ) } ].includes(${ accessProp ( name ) } )`
429- : `is${ typescriptType } (${ accessProp ( name ) } )` ,
430- ) . replace ( / \s + / g, ' ' ) } ) &&`;
431374
432- return {
375+ const property : Property = {
433376 isArray,
434- isDictionary : propSchema . additionalProperties ,
377+ isDictionary : ! ! propSchema . additionalProperties ,
435378 isRef,
436- isPrimitiveType,
437- isRequired,
379+ isPrimitiveType : BASIC_TS_TYPE_REGEX . test ( type ) ,
380+ isRequired : requiredProps . includes ( propName ) ,
438381 name,
439382 description : replaceNewLines ( propSchema . description ) ,
440383 type : type ,
441- guard,
442384 typescriptType : allOfParsed . length
443385 ? allOfParsed . join ( ' & ' )
444386 : typescriptType ,
445387 imports : isRef ? [ type , ...allOfImports ] : allOfImports ,
446388 } ;
389+
390+ const guard = `(${ guardFn (
391+ ( ) =>
392+ propertyAllOf . length
393+ ? `(${ propertyAllOf
394+ . map ( prop =>
395+ guardFn (
396+ ( ) => `is${ prop . typescriptType } (${ accessProp ( name ) } )` ,
397+ { ...prop , name, isRequired : true } ,
398+ ) ,
399+ )
400+ . join ( ' && ' ) } )`
401+ : 'enum' in propSchema
402+ ? `[${ ( typescriptType || '' ) . replace (
403+ / \| / g,
404+ ', ' ,
405+ ) } ].includes(${ accessProp ( name ) } )`
406+ : `is${ typescriptType } (${ accessProp ( name ) } )` ,
407+ property ,
408+ ) . replace ( / \s + / g, ' ' ) } ) &&`;
409+
410+ return { ...property , guard } ;
447411 } )
448412 . sort ( compareStringByKey ( 'name' ) ) ; // tslint:disable-line:no-array-mutation
449413}
@@ -617,7 +581,8 @@ function transformParameters(
617581 const paramRef : Partial < SwaggerParameter > = derefName
618582 ? allParams [ derefName ] || { }
619583 : { } ;
620- const name = 'name' in paramRef ? paramRef . name : ( param as Parameter ) . name ;
584+ const name =
585+ 'name' in paramRef ? paramRef . name || '' : ( param as Parameter ) . name ;
621586 const type =
622587 ( 'type' in param && param . type ) ||
623588 ( paramRef && 'type' in paramRef && paramRef . type ) ||
@@ -660,7 +625,8 @@ function transformParameters(
660625 isArray,
661626 isRequired :
662627 ( param as Parameter ) . isRequired ||
663- ( param as { required : boolean } ) . required ||
628+ // tslint:disable-next-line:no-any
629+ ( < any > param ) . required ||
664630 paramRef . required ,
665631 name,
666632 typescriptType,
0 commit comments