Object to validate as SearchResponseProduct
Type predicate indicating if product is a valid SearchResponseProduct
// Valid search response product
const validProduct = {
id: 12345,
vid: 67890,
image: 1,
brand: false,
code: "CHEM-001",
ean: "1234567890123",
sku: "SKU-001",
score: 1.0,
available: true,
unit: true,
url: "/products/chemical-1",
title: "Sodium Chloride",
fulltitle: "Sodium Chloride 500g",
variant: "500g",
description: "High purity sodium chloride",
data_01: "Additional info",
price: {
price: 29.99,
price_incl: 29.99,
price_excl: 24.79,
price_old: 39.99,
price_old_incl: 39.99,
price_old_excl: 33.05
}
};
if (isSearchResponseProduct(validProduct)) {
console.log("Valid product:", validProduct.title);
console.log("Price:", validProduct.price.price);
console.log("Available:", validProduct.available);
}
// Invalid product (missing required properties)
const missingProps = {
id: 12345,
title: "Sodium Chloride",
price: {
price: 29.99,
price_incl: 29.99,
price_excl: 24.79,
price_old: 39.99,
price_old_incl: 39.99,
price_old_excl: 33.05
}
// Missing other required properties
};
if (!isSearchResponseProduct(missingProps)) {
console.error("Invalid product - missing required properties");
}
// Invalid product (wrong types)
const wrongTypes = {
id: "12345", // Should be number
vid: "67890", // Should be number
image: "1", // Should be number
brand: "false", // Should be boolean
code: 123, // Should be string
ean: 1234567890123, // Should be string
sku: 123, // Should be string
score: "1.0", // Should be number
available: "true", // Should be boolean
unit: "true", // Should be boolean
url: 123, // Should be string
title: 123, // Should be string
fulltitle: 123, // Should be string
variant: 500, // Should be string
description: 123, // Should be string
data_01: 123, // Should be string
price: "29.99" // Should be PriceObject
};
if (!isSearchResponseProduct(wrongTypes)) {
console.error("Invalid product - wrong property types");
}
// Invalid product (invalid price object)
const invalidPrice = {
id: 12345,
vid: 67890,
image: 1,
brand: false,
code: "CHEM-001",
ean: "1234567890123",
sku: "SKU-001",
score: 1.0,
available: true,
unit: true,
url: "/products/chemical-1",
title: "Sodium Chloride",
fulltitle: "Sodium Chloride 500g",
variant: "500g",
description: "High purity sodium chloride",
data_01: "Additional info",
price: {
price: "29.99" // Invalid price object
}
};
if (!isSearchResponseProduct(invalidPrice)) {
console.error("Invalid product - invalid price object");
}
Type guard to validate if an object has the correct structure for a Laboratorium Discounter search response product. Checks for the presence and correct types of all required product properties including basic info, availability, and a valid price object.