Object to validate as ProductObject
Type predicate indicating if product is a valid ProductObject
// Valid product object
const validProduct = {
id: "prod_123",
name: "Sodium Chloride",
description: "ACS reagent grade",
sku: "NACL-500",
urlPart: "sodium-chloride",
price: 29.99,
formattedPrice: "$29.99",
productItems: [
{
id: "item_123",
formattedPrice: "$29.99",
price: 29.99,
optionsSelections: [1]
}
],
options: [
{
selections: [
{ id: "opt_1", value: "500g", description: "500g Bottle", key: "size", inStock: true }
]
}
]
};
if (isWixProduct(validProduct)) {
console.log("Valid product:", validProduct.name);
console.log("Price:", validProduct.formattedPrice);
}
export function isWixProduct(product: unknown): product is ProductObject {
const parsed = v.safeParse(wixProductSchema, product);
if (!parsed.success) {
return false;
}
const p = parsed.output;
if (!p.productItems.every((item) => isProductItem(item))) {
return false;
}
if (p.options.length > 0) {
const firstOption = p.options[0] as { selections?: unknown[] };
if (!firstOption?.selections?.every((s) => isProductSelection(s))) {
return false;
}
}
return true;
}
Type guard to validate if an object is a valid Wix ProductObject. Checks for the presence and correct types of all required product properties including price, name, URL, and validates all product items and options.