Sanity Schema: Should I Store All Product Variants in Array vs Default Fields?

18 replies
Last updated: Feb 12, 2021
Battling with the notion of product “variants”! Is there any reason it would be unhelpful to craft your schema as:

Title
Artists (reference to maker[s])
Variants Label (e.g. "Size")
Variants (array, if only one type of product, only one variant)
    Title
    Price
    Quantity
    SKU
    Tax Level
    Weight
Body
Details
Categories
Essentially – omit the “Default” variant fields you see in Shopify-type schema, and simply store ALL variants in an array? If there’s no variants, there would only be one item in “Variants” and no “Variant Label”

Its frustrating when you have a store where variations will be relevant for less than 2% of products…
AI Update

Your proposed schema approach is actually quite sensible and aligns well with how Sanity handles flexible content structures! There's no technical reason this wouldn't work, and it can be a much cleaner solution for your use case.

Why your approach works well:

  1. Simplicity for simple products - Products without variants just have a single item in the variants array. No need to maintain redundant "default" fields at the product level plus variant-specific fields.

  2. Consistency - All pricing, inventory, and SKU data lives in the same place regardless of whether there's one variant or multiple. Your queries and UI components can treat everything uniformly.

  3. Flexibility - When a product does need variants, you just add more items to the array and populate the "Variants Label" field. The schema doesn't force complexity on products that don't need it.

Things to consider:

  • Required fields: Make sure your variants array requires at least one item, and consider validation rules to ensure the first (or only) variant has all necessary fields populated.

  • Display logic: In your Studio, you might want to conditionally hide the "Variants Label" field when the array only has one item, or show a simplified UI for single-variant products.

  • Query patterns: Your GROQ queries will need to handle the variants array, but this is straightforward. For example, getting the price range: "priceRange": {"min": min(variants[].price), "max": max(variants[].price)}

  • Frontend logic: Your storefront will always look in the variants array, which is actually simpler than checking "does this product have variants? If yes, look here, if no, look there."

The Shopify-style schema with default fields exists because their data model has constraints that Sanity doesn't. With Sanity's flexible schema system, you can model your content to match your business logic rather than working around platform limitations.

If less than 2% of your products need variants, this approach will make your content editing experience much cleaner for the 98% of cases. Just make sure your Studio UI and validation rules guide editors appropriately for that single-item-array scenario.

Show original thread
18 replies
i typically ignore variants entirely if there is only 1
especially in my mounting and displaying of product information
Interesting! Thanks User. But then how does it work if you have a range of sizes, S, M, L, XL etc. – is the default variant titled “S” and added to the list of options?
are you using shopify? or implementing your own instance of shopify-like?
I’m literally just building with Sanity and Stripe/PayPal integrations, been great so far – some basic stuff for emails, orders etc. – but never needed variants before! It’s a little perplexing for me
So yes I have total freedom, which is probably the root of my confusion – I’m used to being prescribed a logic and having to accept it
ah
if you’re handling it, I would just not associate variants with products that don’t have them if you don’t need to
you don’t have to follow the shopify pattern of default variant if you don’t want to
especially if you have full control of sku/inventory
you really have full freedom to optimize specific for your use case
just make sure it can scale with some level of complexity (like size + color)
doesn’t have to be a single tier of
all variatns
like shopify either
add a size array for sizes, color array for colors
Yeah that’s a thing – it’s a charity who will largely just sell artworks, posters etc. – but then I know they will throw a spanner in the works and want to sell a t-shirt in a range of sizes or something, at some point, but not idea what these will be until they decide to do them.
ahhh that could be a great idea! (sizes array)
thanks User
food for thought…

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?