Skip to content

Commit 6953d06

Browse files
committed
[TOOL-3021] Insight Playground: Enforce chain query param to be required + support anyOf schema (#5925)
1 parent 4b5661b commit 6953d06

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

apps/dashboard/src/app/team/[team_slug]/[project_slug]/insight/[blueprint_slug]/blueprint-playground.client.tsx

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,11 @@ export function BlueprintPlayground(props: {
122122

123123
function modifyParametersForPlayground(_parameters: BlueprintParameter[]) {
124124
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
126127
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;
137130
}
138131

139132
// 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: {
814807
);
815808
}
816809

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) {
819815
return;
820816
}
821817

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)) {
823831
return;
824832
}
825833

826834
// if enum values
827-
const enumValues = param.schema.enum;
835+
const enumValues = schema.enum;
828836
if (enumValues) {
829837
const enumSchema = z.enum(
830838
// @ts-expect-error - Its correct
831839
enumValues,
832840
);
833841

834-
if (param.required) {
842+
if (isRequired) {
835843
return enumSchema;
836844
}
837845

838846
return enumSchema.or(z.literal(""));
839847
}
840848

841-
switch (param.schema.type) {
849+
switch (schema.type) {
842850
case "integer": {
843851
const intSchema = z.coerce
844852
.number({
@@ -847,7 +855,7 @@ function openAPIV3ParamToZodFormSchema(param: BlueprintParameter) {
847855
.int({
848856
message: "Must be an integer",
849857
});
850-
return param.required
858+
return isRequired
851859
? intSchema.min(1, {
852860
message: "Required",
853861
})
@@ -856,7 +864,7 @@ function openAPIV3ParamToZodFormSchema(param: BlueprintParameter) {
856864

857865
case "number": {
858866
const numberSchema = z.coerce.number();
859-
return param.required
867+
return isRequired
860868
? numberSchema.min(1, {
861869
message: "Required",
862870
})
@@ -865,13 +873,13 @@ function openAPIV3ParamToZodFormSchema(param: BlueprintParameter) {
865873

866874
case "boolean": {
867875
const booleanSchema = z.coerce.boolean();
868-
return param.required ? booleanSchema : booleanSchema.optional();
876+
return isRequired ? booleanSchema : booleanSchema.optional();
869877
}
870878

871879
// everything else - just accept it as a string;
872880
default: {
873881
const stringSchema = z.string();
874-
return param.required
882+
return isRequired
875883
? stringSchema.min(1, {
876884
message: "Required",
877885
})
@@ -883,11 +891,16 @@ function openAPIV3ParamToZodFormSchema(param: BlueprintParameter) {
883891
function createParametersFormSchema(parameters: BlueprintParameter[]) {
884892
const shape: z.ZodRawShape = {};
885893
for (const param of parameters) {
886-
const paramSchema = openAPIV3ParamToZodFormSchema(param);
894+
const paramSchema = openAPIV3ParamToZodFormSchema(
895+
param.schema,
896+
!!param.required,
897+
);
887898
if (paramSchema) {
888899
shape[param.name] = paramSchema;
889900
} else {
890-
shape[param.name] = z.string();
901+
shape[param.name] = param.required
902+
? z.string().min(1, { message: "Required" })
903+
: z.string();
891904
}
892905
}
893906

0 commit comments

Comments
 (0)