Value to validate
Type predicate indicating if the value is a valid SearchResponse
// Valid search response
const validResponse = [
{
id: 123,
name: "Sodium Chloride",
type: "simple",
// Other required SearchResponseItem properties would go here
prices: {
price: "29.99",
regular_price: "34.99",
sale_price: "29.99",
currency_code: "USD",
currency_symbol: "$",
currency_minor_unit: 2,
currency_decimal_separator: ".",
currency_thousand_separator: ",",
currency_prefix: "$",
currency_suffix: ""
}
},
{
id: 124,
name: "Potassium Chloride",
type: "simple",
// Other required SearchResponseItem properties would go here
prices: {
price: "39.99",
regular_price: "44.99",
sale_price: "39.99",
currency_code: "USD",
currency_symbol: "$",
currency_minor_unit: 2,
currency_decimal_separator: ".",
currency_thousand_separator: ",",
currency_prefix: "$",
currency_suffix: ""
}
}
];
if (isSearchResponse(validResponse)) {
console.log('Valid search response with', validResponse.length, 'items');
validResponse.forEach(item => console.log(item.name));
}
// Invalid search response (not an array)
const notArray = { items: [] };
if (!isSearchResponse(notArray)) {
console.log('Invalid response - not an array');
}
// Invalid search response (array with invalid items)
const invalidItems = [
{ id: 123, name: "Sodium Chloride" }, // Missing required properties
{ id: 124, name: "Potassium Chloride" } // Missing required properties
];
if (!isSearchResponse(invalidItems)) {
console.log('Invalid response - contains invalid items');
}
Type guard to validate if an unknown value is a valid WooCommerce SearchResponse. Checks if the value is an array and all items are valid SearchResponseItems.