The item object to validate
Type predicate indicating if the object is a valid ItemListing
// Valid item listing
const validItem = {
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: [
{
sku: "CHEM-001-500G",
price: "29.99",
link: "/products/nacl?variant=1",
variant_id: "1",
quantity_total: "100",
options: { Model: "500g" }
},
{
sku: "CHEM-001-1KG",
price: "49.99",
link: "/products/nacl?variant=2",
variant_id: "2",
quantity_total: "50",
options: { Model: "1kg" }
}
]
};
if (isItemListing(validItem)) {
console.log("Valid item listing:", validItem.title);
console.log("Number of variants:", validItem.shopify_variants.length);
console.log("Vendor:", validItem.vendor);
}
// Valid item with numeric price
const numericPriceItem = {
title: "Potassium Chloride",
price: 39.99, // Can be number or string
link: "/products/kcl",
product_id: "12346",
product_code: "CHEM-002",
quantity: "1kg",
vendor: "Chemical Supplier",
original_product_id: "12346",
list_price: "49.99",
shopify_variants: [
{
sku: "CHEM-002-1KG",
price: "39.99",
link: "/products/kcl?variant=1",
variant_id: "1",
quantity_total: "50",
options: { Model: "1kg" }
}
]
};
if (isItemListing(numericPriceItem)) {
console.log("Valid item with numeric price");
}
// Invalid item (missing required properties)
const invalidItem = {
title: "Sodium Chloride",
price: "29.99",
link: "/products/nacl"
// Missing other required properties
};
if (!isItemListing(invalidItem)) {
console.error("Invalid item - missing required properties");
}
// Invalid item (wrong types)
const wrongTypes = {
title: 12345, // Should be string
price: true, // Should be string or number
link: 123, // Should be string
product_id: 12345, // Should be string
product_code: 123, // Should be string
quantity: 500, // Should be string
vendor: 123, // Should be string
original_product_id: 12345, // Should be string
list_price: 39.99, // Should be string
shopify_variants: "not an array" // Should be array
};
if (!isItemListing(wrongTypes)) {
console.error("Invalid item - wrong property types");
}
// Invalid item (invalid variants)
const invalidVariants = {
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: [
{
sku: "CHEM-001-500G",
price: "29.99"
// Missing required variant properties
}
]
};
if (!isItemListing(invalidVariants)) {
console.error("Invalid item - contains invalid variants");
}
Type guard to validate if an object is a valid Shopify item listing. Checks for the presence and correct types of all required properties including product details, pricing, and an array of valid Shopify variants.