@@ -693,21 +693,50 @@ protected function getArguments(Hook $hook, array $values, array $requestParams)
693693
694694 $ value = $ existsInValues ? $ values [$ key ] : $ arg ;
695695
696- if (!$ param ['skipValidation ' ]) {
697- if (!$ paramExists && !$ param ['optional ' ]) {
698- throw new Exception ('Param " ' . $ key . '" is not optional. ' , 400 );
699- }
696+ if (!$ param ['skipValidation ' ]&& !$ paramExists && !$ param ['optional ' ]) {
697+ throw new Exception ('Param " ' . $ key . '" is not optional. ' , 400 );
698+ }
700699
701- if ($ paramExists ) {
702- $ this ->validate ($ key , $ param , $ value );
700+ if ($ param ['model ' ] && $ paramExists ) {
701+ $ model = $ param ['model ' ];
702+
703+ if (!\is_string ($ model ) || !\class_exists ($ model )) {
704+ throw new Exception ('Model class does not exist: ' . $ model , 500 );
705+ }
706+ if (!\is_a ($ model , Model::class, true )) {
707+ throw new Exception ('Model class is not an instance of Utopia \\Model ' , 500 );
708+ }
709+ if (($ value === null || $ value === '' ) && $ param ['optional ' ]) {
710+ $ value = null ;
711+ }
712+ if (\is_string ($ value ) && $ value !== '' ) {
713+ try {
714+ $ value = \json_decode ($ value , true , flags: JSON_THROW_ON_ERROR );
715+ } catch (\Throwable $ e ) {
716+ throw new Exception ('Failed to parse JSON for model param " ' . $ key . '": ' . $ e ->getMessage (), 400 );
717+ }
718+ }
719+ if (!\is_array ($ value ) && $ value !== null && $ value !== '' ) {
720+ throw new Exception ('Model param " ' . $ key . '" must be a JSON string, or an array ' , 400 );
721+ }
722+ if (\is_array ($ value )) {
723+ try {
724+ $ value = $ model ::fromArray ($ value );
725+ } catch (\Throwable $ e ) {
726+ throw new Exception ('Failed to create model instance for param " ' . $ key . '": ' . $ e ->getMessage (), 400 );
727+ }
703728 }
704729 }
705730
731+ if (!$ param ['skipValidation ' ] && $ paramExists && $ value !== null && $ value !== '' ) {
732+ $ this ->validate ($ key , $ param , $ value );
733+ }
734+
706735 $ hook ->setParamValue ($ key , $ value );
707736 $ arguments [$ param ['order ' ]] = $ value ;
708737 }
709738
710- foreach ($ hook ->getInjections () as $ key => $ injection ) {
739+ foreach ($ hook ->getInjections () as $ injection ) {
711740 $ arguments [$ injection ['order ' ]] = $ this ->getResource ($ injection ['name ' ]);
712741 }
713742
0 commit comments