@@ -885,22 +885,53 @@ export function testFile( folderName: string, fileName: string, failOnWarning :b
885885 validate = false
886886 }
887887 }
888- if ( validate ) {
889- test ( 'FHIR Validation' , async ( ) => {
890- const response = await client . post ( '/$validate' , resource ) . catch ( function ( error ) {
891- return error . response
892- } )
893- expect ( response . status === 200 || response . status === 400 ) . toBeTruthy ( )
894-
895- //we can ignore warnings on retired resources - these would not be in a balloted package
896- if ( json . status == 'retired' ) {
897- resourceChecks ( response , false )
898- } else {
899- resourceChecks ( response , failOnWarning )
900- }
901- expect ( response . status ) . toEqual ( 200 )
888+ if ( validate ) {
889+ test ( 'FHIR Validation' , async ( ) => {
890+ try {
891+ const response = await client . post ( '/$validate' , resource ) ;
892+
893+ // Accept either valid ( 200) or validation failed (400 )
894+ expect ( [ 200 , 400 ] ) . toContain ( response . status ) ;
895+
896+ // Log validation issues if status is 400
897+ if ( response . status === 400 && response . data ?. issue ) {
898+ console . warn ( '🔍 FHIR Validation Issues:' ) ;
899+ response . data . issue . forEach ( ( issue , index ) => {
900+ const location = issue . expression ?. join ( '.' ) || '[unknown location]' ;
901+ console . warn ( ` ${ index + 1 } . [ ${ issue . severity } ] ${ location } : ${ issue . diagnostics } ` ) ;
902902 } ) ;
903903 }
904+
905+ // Extract resource status (fallback if not defined)
906+ const resourceStatus = resource . status || 'unknown' ;
907+
908+ //we can ignore warnings on retired resources - these would not be in a balloted package
909+ if ( resourceStatus === 'retired' ) {
910+ resourceChecks ( response , false ) ; // Ignore warnings for retired resources
911+ } else {
912+ resourceChecks ( response , failOnWarning ) ; // Apply stricter checks for active resources
913+ }
914+
915+ // Only expect success if not retired
916+ if ( resourceStatus !== 'retired' ) {
917+ expect ( response . status ) . toEqual ( 200 ) ;
918+ }
919+
920+ } catch ( error ) {
921+ // Handle unexpected validation errors directly in the catch block
922+ console . error ( '❌ Unexpected Validation Error:' , {
923+ message : error . message ,
924+ status : error . response ?. status ,
925+ headers : error . response ?. headers ,
926+ data : error . response ?. data ,
927+ } ) ;
928+
929+ throw new Error (
930+ `FHIR Validation failed unexpectedly.\nStatus: ${ error . response ?. status } \nMessage: ${ error . message } \nData: ${ JSON . stringify ( error . response ?. data , null , 2 ) } `
931+ ) ;
932+ }
933+ } ) ;
934+ }
904935 }
905936 )
906937}
0 commit comments