sanity-plugin-unique

Add simple uniqueness validation for string fields (e.g. email, username, slug) in Sanity Studio v3.

Install command

npm i sanity-plugin-unique

πŸ”‘ sanity-plugin-unique

A simple Sanity Studio v3 plugin that provides a reusable unique string field type.
It ensures fields like email, username, or any other string are globally unique across documents.

✨ Features

  • βœ… Easy to use β†’ Just set type: "unique:string".
  • βœ… Automatically detects field name (no manual config needed).
  • βœ… Skips validation if the field is optional/empty.
  • βœ… Works across all documents of the same schema type.
  • βœ… Clear error messages like: This [fieldName] is already taken.

πŸ“¦ Installation

npm install sanity-plugin-unique
# or
yarn add sanity-plugin-unique
# or
pnpm add sanity-plugin-unique

πŸš€ Usage

  1. Add the plugin in your sanity.config.ts:

    import { defineConfig } from "sanity";
    import { uniquePlugin } from "sanity-plugin-unique";
    
    export default defineConfig({
      name: "default",
      title: "My Sanity Project",
      plugins: [uniquePlugin()],
    });
  2. Use in your schema

    // schema/user.ts
    import { defineType } from "sanity";
    
    export const user = defineType({
      name: "user",
      title: "User",
      type: "document",
      fields: [
        {
          name: "email",
          title: "Email",
          type: "unique:string",
        },
        {
          name: "username",
          title: "Username",
          type: "unique:string",
        },
      ],
    });

🀝 Contributing

Want to contribute to this project? Awesome! πŸŽ‰ Please read our Contributing Guide for details on how to get started, development setup, coding guidelines, and the release process.

Related contributions