Skip to content

Limit Upload Max Size on Image Fields With Custom Validation

4repliesLast updated: Nov 13, 2026

So really no body can tell me how to limit upload max size on image fields with the custom Validation?
AI Update

Image fields can't stop an upload by file size. What you can do is add an async custom validator that checks the uploaded asset's size and returns an error when it's too large. An error blocks publishing, so editors have to replace the image before the document can go live.

Why you need a lookup

An image field doesn't hold the file itself. It holds a reference to an asset document (_type: "sanity.imageAsset"), and that asset document has a size field along with mimeType, extension, originalFilename and metadata.dimensions. The validator only sees the reference, so it has to fetch the asset to read the size (Image type).

The validator

Custom validators get (value, context). They can be async, and context.getClient() returns a client you can query with. Return true when the value is valid, or a message string when it isn't. Custom validators also run when the field is empty, so check for a missing value first (Validation).

import {defineField} from 'sanity'

const MAX_SIZE = 2 * 1024 * 1024 // 2 MB, if `size` is in bytes (see note below)

export const heroImage = defineField({
  name: 'heroImage',
  title: 'Hero image',
  type: 'image',
  options: {
    accept: 'image/jpeg,image/png,image/webp', // optional: limit file types too
  },
  validation: (rule) =>
    rule.custom(async (value, context) => {
      const ref = value?.asset?._ref
      if (!ref) return true // nothing uploaded yet; add rule.required() separately if needed

      const client = context.getClient({apiVersion: '2026-03-25'})
      const size = await client.fetch<number | null>(
        `*[_id == $id][0].size`,
        {id: ref},
      )

      if (typeof size === 'number' && size > MAX_SIZE) {
        return `Image is too large (${(size / 1024 / 1024).toFixed(1)} MB). Maximum is 2 MB.`
      }
      return true
    }).error(),
})

Notes:

What this doesn't do

Validation runs after the file is uploaded. A file that's too large is still stored as an asset in the Content Lake. The validator only stops the document from being published while that image is attached. Blocking the upload before it happens isn't covered by the validation API. You'd need a custom input component or asset source, and the pages used here don't describe one for this.

Platform-level upload limits are a separate thing, for example the 50 MiB limit on uploading from a URL through the Assets API. They're far higher than any limit you'd set for editors.

Show original thread
4 replies

Was this answer helpful?

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.

Related contributions