ChemPare Documentation - v0.0.0
    Preparing search index...
    • Type guard to validate if an object is a valid Shopify product variant. Checks for the presence and correct types of all required variant properties including SKU, price, link, variant ID, quantity, and options.

      Parameters

      • variant: unknown

        The variant object to validate

      Returns variant is ShopifyVariant

      Type predicate indicating if the object is a valid ShopifyVariant

      // Valid Shopify variant
      const validVariant = {
      sku: "CHEM-001-500G",
      price: "29.99",
      link: "/products/nacl?variant=1",
      variant_id: "1",
      quantity_total: "100",
      options: {
      Model: "500g",
      Size: "Standard"
      }
      };

      if (isShopifyVariant(validVariant)) {
      console.log("Valid variant:", validVariant.sku);
      console.log("Price:", validVariant.price);
      console.log("Options:", validVariant.options);
      }

      // Valid variant with numeric quantity
      const numericQuantityVariant = {
      sku: "CHEM-001-1KG",
      price: "49.99",
      link: "/products/nacl?variant=2",
      variant_id: "2",
      quantity_total: 100, // Can be number or string
      options: {
      Model: "1kg"
      }
      };

      if (isShopifyVariant(numericQuantityVariant)) {
      console.log("Valid variant with numeric quantity");
      }

      // Invalid variant (missing required properties)
      const invalidVariant = {
      sku: "CHEM-001",
      price: "29.99"
      // Missing other required properties
      };
      if (!isShopifyVariant(invalidVariant)) {
      console.error("Invalid variant - missing required properties");
      }

      // Invalid variant (wrong types)
      const wrongTypes = {
      sku: 12345, // Should be string
      price: 29.99, // Should be string
      link: 123, // Should be string
      variant_id: 1, // Should be string
      quantity_total: true, // Should be string or number
      options: "500g" // Should be object
      };
      if (!isShopifyVariant(wrongTypes)) {
      console.error("Invalid variant - wrong property types");
      }