Object to validate as PriceObject
Type predicate indicating if price is a valid PriceObject
// Valid price object (regular price)
const regularPrice = {
price: 29.99,
price_incl: 29.99,
price_excl: 24.79,
price_old: 29.99,
price_old_incl: 29.99,
price_old_excl: 24.79
};
if (isPriceObject(regularPrice)) {
console.log("Valid regular price:", regularPrice.price);
}
// Valid price object (sale price)
const salePrice = {
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 (isPriceObject(salePrice)) {
console.log("Valid sale price:", salePrice.price);
console.log("Original price:", salePrice.price_old);
}
// Invalid price object (missing properties)
const missingProps = {
price: 29.99,
price_incl: 29.99
// Missing other required properties
};
if (!isPriceObject(missingProps)) {
console.error("Invalid price - missing required properties");
}
// Invalid price object (wrong types)
const wrongTypes = {
price: "29.99", // Should be number
price_incl: "29.99", // Should be number
price_excl: "24.79", // Should be number
price_old: "39.99", // Should be number
price_old_incl: "39.99", // Should be number
price_old_excl: "33.05" // Should be number
};
if (!isPriceObject(wrongTypes)) {
console.error("Invalid price - wrong property types");
}
Type guard to validate if an object has the correct structure for a Laboratorium Discounter price object. Checks for the presence and correct types of all required price properties including regular prices and old prices (for items on sale).