@@ -685,7 +685,7 @@ export const introSequencesSchema = altTemplateContext(
685685 } ) . nonempty ( )
686686) ;
687687
688- export const treatmentSchema = altTemplateContext (
688+ export const baseTreatmentSchema =
689689 z . object ( {
690690 name : nameSchema ,
691691 desc : descriptionSchema . optional ( ) ,
@@ -697,10 +697,18 @@ export const treatmentSchema = altTemplateContext(
697697 exitSequence : introExitStepsSchema . optional ( ) ,
698698 } )
699699 . strict ( )
700+
701+
702+ export const treatmentSchema = altTemplateContext (
703+ baseTreatmentSchema
704+ //works currently for the case where playerSchema always occurs within a treatmentSchema
705+ //However if a playerSchema is used outside of a treatmentSchema, this will not work, as playerCount will not be defined in its scope
706+ //With the current structure of templateSchema, this is hypothetically possible, but unlikely
700707 . superRefine ( ( treatment , ctx ) => {
701- //works currently for the case where playerSchema always occurs within a treatmentSchema
702- //However if a playerSchema is used outside of a treatmentSchema, this will not work, as playerCount will not be defined in its scope
703- //With the current structure of templateSchema, this is hypothetically possible, but unlikely
708+ const baseResult = baseTreatmentSchema . safeParse ( treatment ) ;
709+ if ( ! baseResult . success ) {
710+ return ;
711+ }
704712 const { playerCount, groupComposition, gameStages } = treatment ;
705713 groupComposition ?. forEach ( ( player , index ) => {
706714 if ( typeof player . position === "number" && player . position >= playerCount ) {
@@ -711,12 +719,12 @@ export const treatmentSchema = altTemplateContext(
711719 } ) ;
712720 }
713721 } ) ;
714- gameStages . forEach ( ( stage : { elements : any [ ] ; name : any ; } , stageIndex : string | number ) => {
715- stage . elements . forEach ( ( element : any , elementIndex : string | number ) => {
722+ gameStages ? .forEach ( ( stage : { elements : any [ ] ; name : any ; } , stageIndex : string | number ) => {
723+ stage ? .elements ? .forEach ( ( element : any , elementIndex : string | number ) => {
716724 [ "showToPositions" , "hideFromPositions" ] . forEach ( ( key ) => {
717725 const positions = ( element as any ) [ key ] ;
718726 if ( Array . isArray ( positions ) ) {
719- positions . forEach ( ( pos , posIndex ) => {
727+ positions ? .forEach ( ( pos , posIndex ) => {
720728 if ( typeof pos === "number" && pos >= playerCount ) {
721729 ctx . addIssue ( {
722730 code : z . ZodIssueCode . custom ,
0 commit comments