@@ -122,18 +122,11 @@ export function BlueprintPlayground(props: {
122
122
123
123
function modifyParametersForPlayground ( _parameters : BlueprintParameter [ ] ) {
124
124
const parameters = [ ..._parameters ] ;
125
- // if chain parameter is not already mentioned - add it, its required
125
+
126
+ // make chain query param required - its not required in open api spec - because it either has to be set in subdomain or as a query param
126
127
const chainIdParameter = parameters . find ( ( p ) => p . name === "chain" ) ;
127
- if ( ! chainIdParameter ) {
128
- parameters . unshift ( {
129
- name : "chain" ,
130
- in : "query" ,
131
- required : true ,
132
- schema : {
133
- type : "integer" ,
134
- description : "Chain ID of the blockchain" ,
135
- } ,
136
- } ) ;
128
+ if ( chainIdParameter ) {
129
+ chainIdParameter . required = true ;
137
130
}
138
131
139
132
// remove the client id parameter if it is present - we will always replace the parameter with project's client id
@@ -814,31 +807,46 @@ function ResponseSection(props: {
814
807
) ;
815
808
}
816
809
817
- function openAPIV3ParamToZodFormSchema ( param : BlueprintParameter ) {
818
- if ( ! param . schema ) {
810
+ function openAPIV3ParamToZodFormSchema (
811
+ schema : BlueprintParameter [ "schema" ] ,
812
+ isRequired : boolean ,
813
+ ) : z . ZodTypeAny | undefined {
814
+ if ( ! schema ) {
819
815
return ;
820
816
}
821
817
822
- if ( ! ( "type" in param . schema ) ) {
818
+ if ( "anyOf" in schema ) {
819
+ const anyOf = schema . anyOf ;
820
+ if ( ! anyOf ) {
821
+ return ;
822
+ }
823
+ const anySchemas = anyOf
824
+ . map ( ( s ) => openAPIV3ParamToZodFormSchema ( s , isRequired ) )
825
+ . filter ( ( x ) => ! ! x ) ;
826
+ // @ts -expect-error - Its ok, z.union is expecting tuple type but we have array
827
+ return z . union ( anySchemas ) ;
828
+ }
829
+
830
+ if ( ! ( "type" in schema ) ) {
823
831
return ;
824
832
}
825
833
826
834
// if enum values
827
- const enumValues = param . schema . enum ;
835
+ const enumValues = schema . enum ;
828
836
if ( enumValues ) {
829
837
const enumSchema = z . enum (
830
838
// @ts -expect-error - Its correct
831
839
enumValues ,
832
840
) ;
833
841
834
- if ( param . required ) {
842
+ if ( isRequired ) {
835
843
return enumSchema ;
836
844
}
837
845
838
846
return enumSchema . or ( z . literal ( "" ) ) ;
839
847
}
840
848
841
- switch ( param . schema . type ) {
849
+ switch ( schema . type ) {
842
850
case "integer" : {
843
851
const intSchema = z . coerce
844
852
. number ( {
@@ -847,7 +855,7 @@ function openAPIV3ParamToZodFormSchema(param: BlueprintParameter) {
847
855
. int ( {
848
856
message : "Must be an integer" ,
849
857
} ) ;
850
- return param . required
858
+ return isRequired
851
859
? intSchema . min ( 1 , {
852
860
message : "Required" ,
853
861
} )
@@ -856,7 +864,7 @@ function openAPIV3ParamToZodFormSchema(param: BlueprintParameter) {
856
864
857
865
case "number" : {
858
866
const numberSchema = z . coerce . number ( ) ;
859
- return param . required
867
+ return isRequired
860
868
? numberSchema . min ( 1 , {
861
869
message : "Required" ,
862
870
} )
@@ -865,13 +873,13 @@ function openAPIV3ParamToZodFormSchema(param: BlueprintParameter) {
865
873
866
874
case "boolean" : {
867
875
const booleanSchema = z . coerce . boolean ( ) ;
868
- return param . required ? booleanSchema : booleanSchema . optional ( ) ;
876
+ return isRequired ? booleanSchema : booleanSchema . optional ( ) ;
869
877
}
870
878
871
879
// everything else - just accept it as a string;
872
880
default : {
873
881
const stringSchema = z . string ( ) ;
874
- return param . required
882
+ return isRequired
875
883
? stringSchema . min ( 1 , {
876
884
message : "Required" ,
877
885
} )
@@ -883,11 +891,16 @@ function openAPIV3ParamToZodFormSchema(param: BlueprintParameter) {
883
891
function createParametersFormSchema ( parameters : BlueprintParameter [ ] ) {
884
892
const shape : z . ZodRawShape = { } ;
885
893
for ( const param of parameters ) {
886
- const paramSchema = openAPIV3ParamToZodFormSchema ( param ) ;
894
+ const paramSchema = openAPIV3ParamToZodFormSchema (
895
+ param . schema ,
896
+ ! ! param . required ,
897
+ ) ;
887
898
if ( paramSchema ) {
888
899
shape [ param . name ] = paramSchema ;
889
900
} else {
890
- shape [ param . name ] = z . string ( ) ;
901
+ shape [ param . name ] = param . required
902
+ ? z . string ( ) . min ( 1 , { message : "Required" } )
903
+ : z . string ( ) ;
891
904
}
892
905
}
893
906
0 commit comments