Skip to content

Commit

Permalink
[TOOL-3021] Insight Playground: Enforce chain query param to be requi…
Browse files Browse the repository at this point in the history
…red + support anyOf schema
  • Loading branch information
MananTank committed Jan 10, 2025
1 parent 7fb5ce1 commit ff591b9
Showing 1 changed file with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,11 @@ export function BlueprintPlayground(props: {

function modifyParametersForPlayground(_parameters: BlueprintParameter[]) {
const parameters = [..._parameters];
// if chain parameter is not already mentioned - add it, its required

// 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
const chainIdParameter = parameters.find((p) => p.name === "chain");
if (!chainIdParameter) {
parameters.unshift({
name: "chain",
in: "query",
required: true,
schema: {
type: "integer",
description: "Chain ID of the blockchain",
},
});
if (chainIdParameter) {
chainIdParameter.required = true;
}

// remove the client id parameter if it is present - we will always replace the parameter with project's client id
Expand Down Expand Up @@ -814,31 +807,46 @@ function ResponseSection(props: {
);
}

function openAPIV3ParamToZodFormSchema(param: BlueprintParameter) {
if (!param.schema) {
function openAPIV3ParamToZodFormSchema(
schema: BlueprintParameter["schema"],
isRequired: boolean,
): z.ZodTypeAny | undefined {
if (!schema) {
return;
}

if (!("type" in param.schema)) {
if ("anyOf" in schema) {
const anyOf = schema.anyOf;
if (!anyOf) {
return;
}
const anySchemas = anyOf
.map((s) => openAPIV3ParamToZodFormSchema(s, isRequired))
.filter((x) => !!x);
// @ts-expect-error - Its ok, z.union is expecting tuple type but we have array
return z.union(anySchemas);
}

if (!("type" in schema)) {
return;
}

// if enum values
const enumValues = param.schema.enum;
const enumValues = schema.enum;
if (enumValues) {
const enumSchema = z.enum(
// @ts-expect-error - Its correct
enumValues,
);

if (param.required) {
if (isRequired) {
return enumSchema;
}

return enumSchema.or(z.literal(""));
}

switch (param.schema.type) {
switch (schema.type) {
case "integer": {
const intSchema = z.coerce
.number({
Expand All @@ -847,7 +855,7 @@ function openAPIV3ParamToZodFormSchema(param: BlueprintParameter) {
.int({
message: "Must be an integer",
});
return param.required
return isRequired
? intSchema.min(1, {
message: "Required",
})
Expand All @@ -856,7 +864,7 @@ function openAPIV3ParamToZodFormSchema(param: BlueprintParameter) {

case "number": {
const numberSchema = z.coerce.number();
return param.required
return isRequired
? numberSchema.min(1, {
message: "Required",
})
Expand All @@ -865,13 +873,13 @@ function openAPIV3ParamToZodFormSchema(param: BlueprintParameter) {

case "boolean": {
const booleanSchema = z.coerce.boolean();
return param.required ? booleanSchema : booleanSchema.optional();
return isRequired ? booleanSchema : booleanSchema.optional();
}

// everything else - just accept it as a string;
default: {
const stringSchema = z.string();
return param.required
return isRequired
? stringSchema.min(1, {
message: "Required",
})
Expand All @@ -883,11 +891,16 @@ function openAPIV3ParamToZodFormSchema(param: BlueprintParameter) {
function createParametersFormSchema(parameters: BlueprintParameter[]) {
const shape: z.ZodRawShape = {};
for (const param of parameters) {
const paramSchema = openAPIV3ParamToZodFormSchema(param);
const paramSchema = openAPIV3ParamToZodFormSchema(
param.schema,
!!param.required,
);
if (paramSchema) {
shape[param.name] = paramSchema;
} else {
shape[param.name] = z.string();
shape[param.name] = param.required
? z.string().min(1, { message: "Required" })
: z.string();
}
}

Expand Down

0 comments on commit ff591b9

Please sign in to comment.