The response object to validate
Type predicate indicating if the response is a valid SearchResponse
// Valid search response
const validResponse = {
totalItems: 100,
startIndex: 0,
itemsPerPage: 20,
currentItemCount: 20,
items: [
{
title: "Sodium Chloride",
price: "29.99",
link: "/products/nacl",
product_id: "12345",
product_code: "CHEM-001",
quantity: "500g",
vendor: "Chemical Supplier",
original_product_id: "12345",
list_price: "39.99",
shopify_variants: []
}
]
};
if (isValidSearchResponse(validResponse)) {
console.log(`Found ${validResponse.items.length} items`);
} else {
console.error("Invalid search response structure");
}
export function isValidSearchResponse(response: unknown): response is SearchResponse {
if (!validSearchResponseSchema.safeParse(response).success) {
return false;
}
return (response as { items: unknown[] }).items.every((item) => isItemListing(item));
}
Type guard to validate if a response from the Searchanise search API is a valid SearchResponse object. Checks for the presence and correct types of all required properties including pagination info, suggestions, and a valid array of item listings.