Cart Validation Function not running in AJAX API in Production

Topic summary

A cart validation function designed to limit total items to 10 is behaving inconsistently across environments:

The Issue:

  • In development (shopify app dev), the function correctly returns a 422 error via the cart/update AJAX endpoint when attempting to add more than 10 items
  • In production, the same endpoint fails to display the error message and allows exceeding the 10-item limit
  • The validation does work at the actual checkout page in production

Technical Details:

  • The function checks if total cart quantity exceeds 10 using reduce on cart lines
  • When triggered, it should return an error with message “Not possible to order more than 10 in total” targeted at “cart”
  • The user is sending a payload with 11 items across multiple cart sections, expecting a 422 response

Current Status:
The discrepancy between development and production behavior for the AJAX API endpoint remains unresolved, though checkout-level validation functions correctly.

Summarized with AI on November 10. AI used: claude-sonnet-4-5-20250929.

Hello. I have a function to limit the total items in the cart to 10.

When I run shopify app dev and use the function in my Development Theme, the cart/update endpoint properly returns a 422 response with the error message from the function when I try to add more than 10 items to my cart.

However, in Production, the cart/update endpoint doesn’t display the message and allows more than 10.

When I’m in the actual checkout, I see the error message though.

Here’s the function:

// @ts-check

/**
 * @typedef {import("../generated/api").RunInput} RunInput
 * @typedef {import("../generated/api").FunctionRunResult} FunctionRunResult
 */

/**
 * @param {RunInput} input
 * @returns {FunctionRunResult}
 */
export function run(input) {
	const errors = [];

	// If the total quantity is more than 10, we add an error to the cart
	if (input.cart.lines.reduce((acc, { quantity }) => acc + quantity, 0) > 10) {
		errors.push({
			localizedMessage: "Not possible to order more than 10 in total",
			target: "cart",
		});
	}

	return {
		errors,
	};
}

This is the payload I’m sending that I’m expecting to return a 422 error response:

{
	"line": "1",
	"quantity": "11",
	"sections": [
		"template--17476154130659__cart-items",
		"cart-icon-bubble",
		"cart-live-region-text",
		"template--17476154130659__cart-footer"
	],
	"sections_url": "/cart"
}