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
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()], });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.