@@ -411,40 +411,44 @@ function generateSampleData({
411411
412412export function createUploadTemplateCSV ( instrument : AnyUnilingualFormInstrument ) {
413413 // TODO - type validationSchema as object
414- const instrumentSchema = instrument . validationSchema as z . AnyZodObject ;
414+ try {
415+ const instrumentSchema = instrument . validationSchema as z . AnyZodObject ;
415416
416- const instrumentSchemaDef : unknown = instrument . validationSchema . _def ;
417+ const instrumentSchemaDef : unknown = instrument . validationSchema . _def ;
417418
418- let shape : { [ key : string ] : z . ZodTypeAny } = { } ;
419+ let shape : { [ key : string ] : z . ZodTypeAny } = { } ;
419420
420- if ( isZodTypeDef ( instrumentSchemaDef ) && isZodEffectsDef ( instrumentSchemaDef ) ) {
421- const innerSchema : unknown = instrumentSchemaDef . schema . _def ;
422- if ( isZodTypeDef ( innerSchema ) && isZodObjectDef ( innerSchema ) ) {
423- shape = innerSchema . shape ( ) as { [ key : string ] : z . ZodTypeAny } ;
421+ if ( isZodTypeDef ( instrumentSchemaDef ) && isZodEffectsDef ( instrumentSchemaDef ) ) {
422+ const innerSchema : unknown = instrumentSchemaDef . schema . _def ;
423+ if ( isZodTypeDef ( innerSchema ) && isZodObjectDef ( innerSchema ) ) {
424+ shape = innerSchema . shape ( ) as { [ key : string ] : z . ZodTypeAny } ;
425+ }
426+ } else {
427+ shape = instrumentSchema . shape as { [ key : string ] : z . ZodTypeAny } ;
424428 }
425- } else {
426- shape = instrumentSchema . shape as { [ key : string ] : z . ZodTypeAny } ;
427- }
428429
429- const columnNames = Object . keys ( shape ) ;
430+ const columnNames = Object . keys ( shape ) ;
430431
431- const csvColumns = INTERNAL_HEADERS . concat ( columnNames ) ;
432+ const csvColumns = INTERNAL_HEADERS . concat ( columnNames ) ;
432433
433- const sampleData = [ ...INTERNAL_HEADERS_SAMPLE_DATA ] ;
434- for ( const col of columnNames ) {
435- const typeNameResult = getZodTypeName ( shape [ col ] ! ) ;
436- if ( ! typeNameResult . success ) {
437- throw new Error ( typeNameResult . message ) ;
434+ const sampleData = [ ...INTERNAL_HEADERS_SAMPLE_DATA ] ;
435+ for ( const col of columnNames ) {
436+ const typeNameResult = getZodTypeName ( shape [ col ] ! ) ;
437+ if ( ! typeNameResult . success ) {
438+ throw new Error ( typeNameResult . message ) ;
439+ }
440+ sampleData . push ( generateSampleData ( typeNameResult ) ) ;
438441 }
439- sampleData . push ( generateSampleData ( typeNameResult ) ) ;
440- }
441442
442- unparse ( [ csvColumns , sampleData ] ) ;
443+ unparse ( [ csvColumns , sampleData ] ) ;
443444
444- return {
445- content : unparse ( [ csvColumns , sampleData ] ) ,
446- fileName : `${ instrument . internal . name } _${ instrument . internal . edition } _template.csv`
447- } ;
445+ return {
446+ content : unparse ( [ csvColumns , sampleData ] ) ,
447+ fileName : `${ instrument . internal . name } _${ instrument . internal . edition } _template.csv`
448+ } ;
449+ } catch {
450+ throw new Error ( 'Error generating Sample CSV template' ) ;
451+ }
448452}
449453
450454export async function processInstrumentCSV (
0 commit comments