ChemPal Documentation - v0.0.13-beta.5
    Preparing search index...

    Function isValidMagento2SearchResponse

    • Type guard to validate that a response from the Magento 2 GraphQL products query matches the expected Magento2SearchResponse shape. Logs the parse failure via console.warn to aid debugging mismatched schemas.

      Parameters

      • response: unknown

        The response object to validate

      Returns response is Magento2SearchResponse

      Type predicate indicating if the response is a valid Magento2SearchResponse

      const response = await fetch("https://example.com/graphql", { method: "POST", body: query });
      const json = await response.json();
      if (isValidMagento2SearchResponse(json)) {
      console.log(`Found ${json.data.products.items.length} items`);
      }
      export function isValidMagento2SearchResponse(
      response: unknown,
      ): response is Magento2SearchResponse {
      const parsed = magento2SearchResponseSchema.safeParse(response);
      if (!parsed.success) {
      console.warn("isValidMagento2SearchResponse: response is not a valid Magento2SearchResponse", {
      response,
      parsed,
      error: parsed.error,
      issues: zodAddActualValueToIssues(parsed.error.issues, response),
      });
      }
      return parsed.success;
      }