Sub topic 14
Validating a Feed Before You Submit It
What validation should catch
Assume you can read simple XML and run a short Python script. A feed validator is a pre-submission check for errors that could cause rejected products, incorrect offers, or mismatches with landing pages.
Validation works in layers:
- Syntax: The file must be valid XML, JSON, or tab-separated data.
- Required attributes: Each item needs fields such as
id,title,link,price, andavailability. - Value formats: Prices need a number and ISO currency code, such as
89.00 USD. Availability must use an accepted value. - Business rules: A variant should have a stable
item_group_id, and its page should show the same color and size. - Cross-source consistency: The feed, product page, and structured data must agree.
A validator catches mechanical errors before submission. It cannot decide whether a title is useful or whether a product violates every marketplace policy.
| Check | Example failure | Corrective action |
|---|---|---|
| Required field | Missing `g:link` | Add a crawlable product URL |
| Format | `89 dollars` | Use `89.00 USD` |
| Allowed value | `available` | Use `in_stock` |
| Identifier | Invalid GTIN check digit | Correct or remove the GTIN |
Build a small preflight checker
The following script checks required fields, price syntax, availability values, and GTIN length and check digits. Save it as check_feed.py.
import re
import sys
import xml.etree.ElementTree as ET
NS = {"g": "http://base.google.com/ns/1.0"}
REQUIRED = ["id", "title", "description", "link", "image_link",
"price", "availability"]
ALLOWED_AVAILABILITY = {"in_stock", "out_of_stock", "preorder", "backorder"}
PRICE = re.compile(r"^\d+(\.\d{2})? [A-Z]{3}$")
def valid_gtin(value):
if not value.isdigit() or len(value) not in (8, 12, 13, 14):
return False
body, check_digit = value[:-1], int(value[-1])
total = sum(
int(digit) * (3 if index % 2 == 0 else 1)
for index, digit in enumerate(reversed(body))
)
return (10 - total % 10) % 10 == check_digit
def check_item(item):
problems = []
values = {}
for name in REQUIRED + ["gtin"]:
node = item.find("g:" + name, NS)
values[name] = node.text.strip() if node is not None and node.text else ""
for name in REQUIRED:
if not values[name]:
problems.append("missing " + name)
if values["price"] and not PRICE.fullmatch(values["price"]):
problems.append("invalid price format")
if values["availability"] not in ALLOWED_AVAILABILITY:
problems.append("unknown availability")
if values["gtin"] and not valid_gtin(values["gtin"]):
problems.append("invalid GTIN")
return problems
def main(path):
problem_count = 0
root = ET.parse(path).getroot()
for item in root.iter("item"):
node = item.find("g:id", NS)
item_id = node.text.strip() if node is not None and node.text else "(no id)"
for problem in check_item(item):
problem_count += 1
print(f"{item_id}: {problem}")
print(f"problems found: {problem_count}")
return 1 if problem_count else 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1]))Run it against your feed:
python check_feed.py products.xmlA nonzero exit status can fail a build or scheduled export. Add checks for your own rules, such as HTTPS URLs, nonnegative prices, valid image extensions, and unique IDs.
Trace failures to their source
Use the item ID in every error so that an export job can locate the source record. Then compare the failing item across the feed, product page, and JSON-LD. Fix the catalog source rather than editing only the generated feed.
Exercise
A product has price set to 89 USD and availability set to available. What should the preflight checker report?
Show answer
It should report an invalid price format and an unknown availability value. Change them to a permitted format such as 89.00 USD and a permitted value such as in_stock.