Sanity plugin for validating unique string fields across documents.
npm install sanity-plugin-uniqueemail, username, or any other string are globally unique across documents.
type: "unique:string".
This [fieldName] is already taken.
bash
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:
`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
`ts
// 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",
},
],
});
``