Skip to content

Commit

Permalink
chore: account for nested folders while validating requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorge007 committed Apr 16, 2024
1 parent c5d8aee commit b20e0bb
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions packages/hoppscotch-cli/src/utils/mutators.ts
Expand Up @@ -7,6 +7,38 @@ import { error } from "../types/errors";
import { FormDataEntry } from "../types/request";
import { isHoppErrnoException } from "./checks";

const validateCollectionRequests = (
collections: HoppCollection[],
collectionFilePath: string
) => {
return collections.map((collection) => {
// Validate requests using zod schema
const requestSchemaParsedResult = z
.array(entityReference(HoppRESTRequest))
.safeParse(collection.requests);

// Handle validation errors
if (!requestSchemaParsedResult.success) {
throw error({
code: "MALFORMED_COLLECTION",
path: collectionFilePath,
data: "Please check the collection data.",
});
}

// Recursively validate requests in nested folders
if (collection.folders.length > 0) {
validateCollectionRequests(collection.folders, collectionFilePath);
}

// Return validated collection
return {
...collection,
requests: requestSchemaParsedResult.data,
};
});
};

/**
* Parses array of FormDataEntry to FormData.
* @param values Array of FormDataEntry.
Expand Down Expand Up @@ -82,22 +114,5 @@ export async function parseCollectionData(
});
}

return collectionSchemaParsedResult.data.map((collection) => {
const requestSchemaParsedResult = z
.array(entityReference(HoppRESTRequest))
.safeParse(collection.requests);

if (!requestSchemaParsedResult.success) {
throw error({
code: "MALFORMED_COLLECTION",
path,
data: "Please check the collection data.",
});
}

return {
...collection,
requests: requestSchemaParsedResult.data,
};
});
return validateCollectionRequests(collectionSchemaParsedResult.data, path);
}

0 comments on commit b20e0bb

Please sign in to comment.