Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TOOL-3021] Insight Playground: Enforce chain query param to be required + support anyOf schema #5925

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading