1- import { isNumberLike , isPlainObject , parseNumber } from '@douglasneuroinformatics/libjs' ;
1+ import { isNumberLike , isObjectLike , isPlainObject , parseNumber } from '@douglasneuroinformatics/libjs' ;
22import type { AnyUnilingualFormInstrument , FormTypes , Json } from '@opendatacapture/runtime-core' ;
33import type { Group } from '@opendatacapture/schemas/group' ;
44import type { UnilingualInstrumentInfo } from '@opendatacapture/schemas/instrument' ;
@@ -60,6 +60,11 @@ type AnyZodTypeDef = z.ZodTypeDef & { typeName: ZodTypeName };
6060
6161type AnyZodArrayDef = z . ZodArrayDef & { type : z . AnyZodObject } ;
6262
63+ //check for edge cases since the were using reversed hierachical logic (if object has _def then object is AnyZodObject)
64+ function isZodObject ( value : unknown ) : value is z . AnyZodObject {
65+ return isObjectLike ( value ) && isZodTypeDef ( ( value as { [ key : string ] : unknown } ) . _def ) ;
66+ }
67+
6368function isZodTypeDef ( value : unknown ) : value is AnyZodTypeDef {
6469 return isPlainObject ( value ) && ZOD_TYPE_NAMES . includes ( value . typeName as ZodTypeName ) ;
6570}
@@ -423,9 +428,12 @@ function generateSampleData({
423428}
424429
425430export function createUploadTemplateCSV ( instrument : AnyUnilingualFormInstrument ) {
426- // TODO - type validationSchema as object
427431 try {
428- const instrumentSchema = instrument . validationSchema as z . AnyZodObject ;
432+ const instrumentSchema = instrument . validationSchema ;
433+
434+ if ( ! isZodObject ( instrumentSchema ) ) {
435+ throw new Error ( 'Validation schema for this instrument is invalid' ) ;
436+ }
429437
430438 const instrumentSchemaDef : unknown = instrument . validationSchema . _def ;
431439
@@ -460,12 +468,10 @@ export function createUploadTemplateCSV(instrument: AnyUnilingualFormInstrument)
460468 fileName : `${ instrument . internal . name } _${ instrument . internal . edition } _template.csv`
461469 } ;
462470 } catch ( e : unknown ) {
463- if ( e instanceof Error && e . message === 'Unsuccessful input data transfer or undefined data' ) {
464- throw e ;
465- }
466- if ( e instanceof Error && e . message === 'Error in validation schema type' ) {
471+ if ( e instanceof Error && e . message ) {
467472 throw e ;
468473 }
474+
469475 throw new Error ( 'Error generating Sample CSV template' ) ;
470476 }
471477}
@@ -484,8 +490,10 @@ export async function processInstrumentCSV(
484490 . regex ( / ^ [ ^ $ \s ] + $ / , 'Subject ID has to be at least 1 character long, without a $ and no whitespaces' ) ;
485491
486492 if ( isZodTypeDef ( instrumentSchemaDef ) && isZodEffectsDef ( instrumentSchemaDef ) ) {
487- //TODO make this type safe without having to cast z.AnyZodObject
488- instrumentSchemaWithInternal = ( instrumentSchemaDef . schema as z . AnyZodObject ) . extend ( {
493+ if ( ! isZodObject ( instrumentSchemaDef . schema ) ) {
494+ return { message : 'Invalid instrument schema' , success : false } ;
495+ }
496+ instrumentSchemaWithInternal = instrumentSchemaDef . schema . extend ( {
489497 date : z . coerce . date ( ) ,
490498 subjectID : $SubjectIdValidation
491499 } ) ;
0 commit comments