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 = v.safeParse(magento2SearchResponseSchema, response);
if (!parsed.success) {
console.warn('isValidMagento2SearchResponse: response is not a valid Magento2SearchResponse', {
response,
parsed,
issues: addActualValueToIssues(parsed.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.