The response object to validate
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;
}
Type guard to validate that a response from the Magento 2 GraphQL
productsquery matches the expectedMagento2SearchResponseshape. Logs the parse failure viaconsole.warnto aid debugging mismatched schemas.