Skip to content

Instantly share code, notes, and snippets.

@deviationist
Last active February 9, 2024 23:16
Show Gist options
  • Save deviationist/f42ecce164e106863809ef2f35873437 to your computer and use it in GitHub Desktop.
Save deviationist/f42ecce164e106863809ef2f35873437 to your computer and use it in GitHub Desktop.
Sanity unique e-mail field
defineField({
name: 'email',
title: 'Email',
type: 'string',
readOnly: ({document}) => document?.origin === 'Checkin',
description: 'Can only be changed in Checkin if the origin is "Checkin".',
validation: Rule => Rule.required().email().custom(async (email: any, context: any) => {
const response = await client.fetch(`*[_type == "user" && email == $email && !(_id in [$draftId, $id])]`, {email, draftId: context.document._id, id: context.document._id.replace('drafts.', '')});
if (response?.length > 0) {
return 'Email already in use';
}
return true;
}),
group: 'userInformation',
}),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment